Okay i have a project due in about 6 hours. We are coding a quadratic equation solver. I need to figure out how to loop so that the user can input the a b and c values for any number of equations...and then after the loop terminates i need to put in my output how many of the equations had 2 distict roots. Sorry for such a beginners question, i am VERY new to c++.
so here is what i have so far
#include<iostream>
#include <string>
#include<fstream>
#include <iomanip>
#include <math.h>
#include <cmath>
using namespace std ;
int main()
{
int a, b, c;
char x;
double root1, root3, root4;
cout<< fixed << showpoint << setprecision(3);
cout<<"===================="<<endl<<endl;
cout<<" Project 2 "<<endl<<endl;
cout<<"===================="<<endl<<endl;
cout<<"After entering 3 coifincients this program will solve the problem using the quadratic formula"<<endl<<endl;
cout<<"please enter A: ";
cin>>a;
cout<<"Please enter B: ";
cin>>b;
cout<<"Please enter C: ";
cin>>c;
if (a==0){
cout<<"This equation is not quadratic becuase a=o"<<endl;
}
else{
cout<<"\n"<<a<<"x^2 + "<<b<<"x + "<<c<<endl<<endl;
}
root1=pow(b,2) - (4 * a * c);
root3=(-b + sqrt(b * b - 4 * a * c)) / (2 * a);
root4=(-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if (root1 < 0)
cout << "The equation had no real solutions" << endl;
else if (root1==0)
cout << "The one repated root of the equation is: " <<root3<<endl;
else if (root1>0)
{
cout << "The roots of the equation are:" << endl;
cout<<"Root 1: "<<root3<<endl;
cout<<"Root 2: "<<root4<<endl;
}
Maybe post this in the Beginners forums then. haha
This is not the best way to do it but since it's for a class and you are a beginner I would o about it this way as to not cause any suspicion from you professor.
All I did was add a prompt for how many equations they want to enter and than set it to a for loop which is called a count controlled loop because you use them mainly when you know exactly how many times you want to loop.
If you do not know how many times you might loop you will use a while or do-while loop, although a while loop is generally more preferable.
Edit - If you are only supposed to output the results at the end of the loop than you will have to store either the results or the numbers that they enter. There are several ways to accomplish this and the best ways are probably beyond your understanding of C++ at this point in time so I dont know what to say about that.
If you can get away with outputting what you were doing originally after each input and then at the end of them all output the dinstinct roots than just output that after the loop.
int main()
{
int a, b, c, n;
char x;
double root1, root3, root4;
cout<< fixed << showpoint << setprecision(3);
cout<<"===================="<<endl<<endl;
cout<<" Project 2 "<<endl<<endl;
cout<<"===================="<<endl<<endl;
cout<<"After entering 3 coifincients this program will solve the problem using the quadratic formula"<<endl<<endl;
cout << "How many equations do you wish to enter?" << endl;
cin >> n;
cout << "You selected to enter " << n << " equations." << endl;
for (int i = 0; i < n; ++i)
{
cout<<"please enter A: ";
cin>>a;
cout<<"Please enter B: ";
cin>>b;
cout<<"Please enter C: ";
cin>>c;
if (a==0){
cout<<"This equation is not quadratic becuase a=o"<<endl;
}
else{
cout<<"\n"<<a<<"x^2 + "<<b<<"x + "<<c<<endl<<endl;
}
root1=pow(b,2) - (4 * a * c);
root3=(-b + sqrt(b * b - 4 * a * c)) / (2 * a);
root4=(-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if (root1 < 0)
cout << "The equation had no real solutions" << endl;
elseif (root1==0)
cout << "The one repated root of the equation is: " <<root3<<endl;
elseif (root1>0)
{
cout << "The roots of the equation are:" << endl;
cout<<"Root 1: "<<root3<<endl;
cout<<"Root 2: "<<root4<<endl;
}
}
return 0;
}