C++ structure help

I am completely lost on the whole structure thing. My teacher only taught it to us for less than an hour and then gave us this. Anything will help right now as I don't even know where to begin. Thanks in advance.

//re-write the program below using structure types
//make two structures, one for cefficient, and the other for roots
//modify the functions so that reference parameters are not used

#include <iostream>
#include <conio.h>
#include <cmath> // for sqrt function
#include <iomanip> // for setprecision(2) etc
using namespace std;

void getInput(float&, float&, float&);
void solveEquation(float, float, float, float&, float&);
void displayResult(float, float, float, float, float);


int main()
{
float a, b, c, // coefficients for a quardratic equation
d, // temp variable to hold b*b - 4*a*c
x1, x2; // roots for the equation

getInput(a, b, c);

solveEquation(a, b, c, x1, x2);

displayResult(a, b, c, x1, x2);

_getch();
return 0;

}

void getInput(float& aa, float& bb, float& cc)
{
cout << "Program to Solves Quadratic Equation aX^2 + bX + c = 0\n\n";

do
{
cout << "Enter coefficient a: ";
cin >> aa;
cout << "Enter coefficient b: ";
cin >> bb;
cout << "Enter coefficient c: ";
cin >> cc;

if((bb*bb - 4*aa*cc) < 0)
cout << "\nThis equation has no real solution, Please re-enter\n";
}
while((bb*bb - 4*aa*cc) < 0);
}

void solveEquation(float aa, float bb, float cc, float& xx1, float& xx2)
{
xx1 = (-bb + sqrt( bb*bb - 4*aa*cc )) / (2*aa);
xx2 = (-bb - sqrt( bb*bb - 4*aa*cc )) / (2*aa);
}

void displayResult(float aa, float bb, float cc, float xx1, float xx2)
{
cout << "\nEquation:\t" << "(" << aa << ")"
<< "x^2 + (" << bb << ")x + (" << cc << ") = 0";

cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2);
cout << "\nRoots:\t\tx1 = " << xx1 << endl
<< "\t\tx2 = " << xx2 << endl;
}





Last edited on
The structure for coefficients should just have 3 floats (a b c) and the structure for roots should have 2 floats (x1 x2). Once you've got those, try to figure out how you can modify those functions to not need pass by reference (hint: you'll be returning a struct instead).

If you want some more info on structs, check here: http://www.cplusplus.com/doc/tutorial/structures/
struct coefficient
{
float a, b, c;
};

struct roots
{
float x1, x2;
};

So since I have added this does that mean that the float a, b, c, x1, x2; does not need to be in main then?
You should define those between using namespace std; and your function prototypes. Then, in main, you can declare them just like any other variable (e.g. coefficient myCoef;).

The coefficient and roots variables you create should replace your current variables in mian, yes.
Last edited on
Im stuck again and I don't know if I even did this correctly,

#include <iostream>
#include <conio.h>
#include <cmath> // for sqrt function
#include <iomanip> // for setprecision(2) etc
using namespace std;

struct coefficient
{
float a, b, c;
};

struct roots
{
float x1, x2;
};

void getInput(coefficient myCoef);
void solveEquation(coefficient myCoef, roots myRoots);
void displayResult(coefficient myCoef, roots myRoots);

int main()
{
//float d; // temp variable to hold b*b - 4*a*c

getInput(myCoef.a, myCoef.b, myCoef.c);
solveEquation(a, b, c, x1, x2);

//displayResult(a, b, c, x1, x2);

_getch();
return 0;

}

void getInput(coefficient myCoef)
{
cout << "Program to Solves Quadratic Equation aX^2 + bX + c = 0\n\n";

do
{
cout << "Enter coefficient a: ";
cin >> myCoef.a;
cout << "Enter coefficient b: ";
cin >> myCoef.b;
cout << "Enter coefficient c: ";
cin >> myCoef.c;

if((myCoef.b * myCoef.b - 4 * myCoef.a * myCoef.c) < 0)
cout << "\nThis equation has no real solution, Please re-enter\n";
}
while((myCoef.b * myCoef.b - 4 * myCoef.a * myCoef.c) < 0);
}

void solveEquation(coefficient myCoef, roots myRoots)
{
myRoots.x1 = (- myCoef.b + sqrt( myCoef.b * myCoef.b - 4 * myCoef.a * myCoef.c )) / (2 * myCoef.a);
myRoots.x2 = (- myCoef.b - sqrt( myCoef.b * myCoef.b - 4 * myCoef.a * myCoef.c )) / (2 * myCoef.a);
}

void displayResult(coefficient myCoef, roots myRoots)
{
cout << "\nEquation:\t" << "(" << myCoef.a << ")"
<< "x^2 + (" << myCoef.b << ")x + (" << myCoef.c << ") = 0";

cout << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2);
cout << "\nRoots:\t\tx1 = " << myRoots.x1 << endl
<< "\t\tx2 = " << myRoots.x2 << endl;
}
I'm confused on if this is correct
Don't know if you still need help on this, but you need to declare coefficient and root variables in main. Also, you need to change your functions to match your instructions (no references) while still getting the values out of the function. getInput should return a coefficient and have no parameters. solveEquation should have a coefficient as parameter and return a roots. displayResult can stay as-is.
Topic archived. No new replies allowed.