Q & A Section 2
Some more questions on C++ below :
Question :
1. How many elements can an array store?
It depends on the value of the subscript (ex:
a[10] means you can store 10 elements)
2. Which is the last character of a string?
The last character of a string is the null character ( denoted by '/0' )
3. What is string?
String is an array of characters.
SS: Thanks for your prompt reply and for the creation of the "question and answer" section. It will be useful not only to me, but to all others interested in learning C++.
Some concerns:
1. I'm trying to use C++ in one of my Math classes to obtain solutions to the quadratic equation ax (squared) + bx + c = 0, where a, b, and c are constants (integers and floating numbers) being input through the standard input. I attempted using conditional flow since the solutions to this problem requires b (squared) - 4ac > or = 0, but the program is not just working.
Answer :
// To find roots of quadratic equation
#include <iostream>
#include <cmath>
// Needed for using pow( ) function
using namespace std;
int main( )
{
float a,b,c,d;
float r;
float real, imaginary;
float root1, root2;
cout<<"Enter the value of a: ";
cin>>a;
cout<<endl<<"Enter the value of b: ";
cin>>b;
cout<<endl<<"Enter the value of c: ";
cin>>c;
d = (b*b)-(4*a*c);
if ( d = = 0 )
{
cout<<endl<<"The roots are real and equal";
root1 = -(b/(2*a));
cout<<endl<<"The roots are "<<root1<<" and "<<root1;
return 0;
}
else if (d>0)
{
cout<<endl<<"The roots are real and unequal";
r = pow(d,0.5);
root1 = (r-b)/(2*a);
root2 = (-r-b)/(2*a);
cout<<endl<<"The roots are "<<root1<<" and "<<root2;
return 0;
}
else if (d<0)
{
cout<<endl<<"The roots are imaginary";
r = pow(-d,0.5);
real = -(b/(2*a));
imaginary = (r/(2*a));
cout<<endl<<"The roots are "<<real<<" + or - "<<imaginary<<" i ";
return 0;
}
return 0;
}
I hope I have clarified your doubt. In case you have further clarification feel free to write to me.
Check out the next Q
and A page 3
Email me at : ssbell2000@yahoo.com
if you have any clarifications
Or Go back to the Contents Page
Copyright © 2002 Sethu Subramanian All rights reserved.