Quadratic Equation using Structures

Create a structure named quad that has three data numbers of the type double for the a, b, and c coefficients of a quadratic polynomial: f(x) = ax^2 + bx + c
Write a main() function that will declare and initialize a quad structure and display the values of the coefficient. Now write and test the following functions:

1) Function readValue() that takes a single reference argument to a quad structure and a void return type. It should ask the user to enter values for the a, b, and c.

2) Function showValue() that takes a single reference argument to a quad structure and a void return type. It should display the quadratic on the terminal in this form: -2x^2 - 4x +35 3) Function addQuad() that takes references to two quad structures as parameters and returns a quad structure that is the sum of the two input quad structures. For quadratic functions f(x) = ax^2 + bx + c and g(x) = dx^2 + ex + f the sum is given by f(x) + g(x) = (a+d)x^2 + (b+e)x + c + f

Demonstrate these functions in a main() program by doing the following: 1) Enter the following 2 quadratic functions using the readValue() function: f(x) = 5x^2 - 2x + 3 and g(x) = 4x^2 + 3x +7 2) Determine the sum of f(x) + g(x) using teh addQuad() function.

3) Display the resulting polynomial using the showValue() function.
Do your own homework.
I started it but can you explain how i would do part 2)? What would the addQuad() function look like and how do i call these functions in the main program?
Last edited on
Does this look right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
using namespace std;


struct quad{
double a,b,c;
};

void readValue(quad&);
void showValue(quad&);
quad addQuad(quad&,quad&);

int main(){
quad One; // declare
One.a=1; //initialize a
One.b=2; //initialize b
One.c=3; //initialize c
cout<<"At initialization, the first equation is: ";
showValue(One);

quad Two;
quad Three;
cout<<"\nFor the first equation..."<<endl;
readValue(One);
cout<<"For the second equation..."<<endl;
readValue(Two);
cout<<"\nThe first equation is: ";
showValue(One);
cout<<"\nThe second equation is: ";
showValue(Two);
cout<<"\nAdding the two equation is: ";
Three=addQuad(One,Two);
showValue(Three);

cout<<"\nEnter any key to exit...";
cin.get();
return 0;
}


void readValue(quad& q) {
cout<<"Enter a: ";
cin>>q.a;
cout<<"Enter b: ";
cin>>q.b;
cout<<"Enter c: ";
cin>>q.c;
return;
}

void showValue(quad& q){
cout<<q.a<<"x^2";
if(q.b>-1)
cout<<"+";
cout<<q.b<<"x";
if(q.c>-1)
cout<<"+";
cout<<q.c;
return;
}

quad addQuad(quad& X,quad& Y){
quad Z;
Z.a=X.a+Y.a;
Z.b=X.b+Y.b;
Z.c=X.c+Y.c;
return Z;
}
A quick once-over and it looks fine to me. The parameters to showValue() and to addQuad() should be const references though. Is it working the way you intend it to?
I don't have a compiler or program to run it on my laptop so I have no way of knowing until I can manage to go to the computer labs but it's due in two days so I wanted someone's opinion to see if I was on track.
Topic archived. No new replies allowed.