I'm having issues with the code below:
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
float discriminant, A, B, C, root1, root2;
fin >> A >> B >> C;
while (A != -99)
{
discriminant = (pow(B, 2.0) - 4 * A*C);
if (A == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant > 0)
{
root1 = (-B - sqrt(discriminant)) / (2.0*A);
root2 = (-B + sqrt(discriminant)) / (2.0*A);
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
fin >> A >> B >> C;
}
fout.close();
ifstream fin2("output.txt");
fin2 >> A >> B >> C >> root1 >> root2;
while (!fin2.eof())
{
cout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
fin2 >> A >> B >> C >> root1 >> root2;
}
cout << endl;
cout << "Coded by Paye W. Kialain" << "\t"<< endl;
system("pause");
return 0;
}
In the project description, I was told to create an input file containing a, b, and c, which I did. The format of the output is also correct. It is a table displaying the a, b and c values along with the 2 calculated roots. However, the calculations of the roots seem to be off. Are my if statements the issue?
The statements discriminant == 0 and A == 0 are dangerous comparisons because discriminant and A are floats. Floating-point calculations are often accompanied with floating-point errors (Think errors you get in mathematical approximations).
Consider this simple example of floating-point errors:
#include <iostream>
#include <string>
int main()
{
float a = 3.0;
float b = 10.0;
std::cout.precision(20);
std::cout << a/b << std::endl;
}
3.0/10.0, that's elementary math! You'd expect the result to be 0.3. However, as it turns out, the result is 0.30000001192092895508. If a and b were doubles, the result would be 0.2999999999999999889. This is because the way floating-point numbers are represented in binary does not allow for an accurate representation of 0.3. Now imagine what would have happened if I had code like if(a/b == 0.3). The condition will never be satisfied.
A solution to this problem is to introduce an epsilon value. This epsilon value basically serves as a value for error tolerance.
float a = 3.0;
float b = 10.0;
const float epsilon = 0.000001;
if(fabs(a/b - 0.3) < epsilon) {
std::cout << "a/b is equal to 0.3!" << std::endl;
}
Related
I'm a fairly new to c++. I'm trying to create a very basic calculator and the results I'm getting are completely wrong. I've come to a standstill after 2 hours of trying everything in my knowledge. What am I doing wrong?
#include <iostream>
using namespace std;
int main()
{
int a = 0.0;
int b = 0.0;
float sum = 'a' + 'b';
float diff = 'a' - 'b';
float prod = 'a' * 'b';
float quot = 'a' / 'b';
float rem = 'a' % 'b';
//Input
cout << "Enter a number: " << endl;
cin >> a;
cout << "Enter another number: " << endl;
cin >> b;
cout << a << " + " << b << " = " << sum <<endl;
cout << a << " - " << b << " = " << diff <<endl;
cout << a << " / " << b << " = " << quot <<endl;
cout << a << " * " << b << " = " << prod <<endl;
cout << a << " % " << b << " = " << rem <<endl;
return 0;
}
you are calculating with character literals. 'a' is not the same as a here.
remove the quotes when calculating, but add them when you print the actual literal "a"
float sum = 'a' + 'b';
You are calculating the ASCII value of the character "a" (which is 65) with the ASCII value of "b" (which is 66)
It should be
float sum = a + b;
instead.
When you print the values, you did the reverse:
cout << a << " + " << b << " = " << sum <<endl;
You want it to be
cout << "a" << " + " << "b" << " = " << sum <<endl;
instead. You want to print characters for the equation and only a number for the result.
You also calculate the values of a and b before they have an actual value.
You should put the calculation after you enter them.
Fixed, by moving the calculations after the input; and by using variables, not literals.
#include <iostream>
using namespace std;
int main()
{
int a = 0.0;
int b = 0.0;
//Input
cout << "Enter a number: " << endl;
cin >> a;
cout << "Enter another number: " << endl;
cin >> b;
float sum = a + b;
float diff = a - b;
float prod = a * b;
float quot = a / b;
float rem = a % b;
cout << a << " + " << b << " = " << sum <<endl;
cout << a << " - " << b << " = " << diff <<endl;
cout << a << " / " << b << " = " << quot <<endl;
cout << a << " * " << b << " = " << prod <<endl;
cout << a << " % " << b << " = " << rem <<endl;
return 0;
}
Here is my code, for n and m, I consistently get 0 for n and nan for m, and I do not know why. I have tried everything I could, but n always shows 0 whilst m is always nan.
#include <iostream>
#include <iomanip>
#include <math.h>
#include <complex>
using namespace std;
int main()
{
float a, b, c, root1, root2, n, m ;
cout << "Please input values for a, b and c to represent the variables in equation : ax^2 + bx + c" << endl; // Dont know how to raise 2 instead of using ^2
cin >> a >> b >> c;
const float d = (b * b) - (4 * a * c);
cout << "Determinant is equal to " << d << endl;
cout << setprecision(3); // to set number of decimal places
n = b / 2 * a;
m = sqrt(d) / 2 * a;
if (d > 0) {
root1 = (-b + sqrt(d)) / (2.0 * a); // positive root
root2 = (-b - sqrt(d)) / (2.0 * a); // negative root
cout << "Determinant is greater than zero, therefore the equation has two real roots, which are " << root1 << " and " << root2 << endl;
}
else if (d < 0) {
cout << "Determinant is less than zero, therefore there would be two imaginary roots: " << n << " + " << m << "i ";
cout << "and " << n << " - " << m << "i" << endl;
}
}
you have to put parenthesis.
n = b / (2 * a);
m = sqrt(d) / (2 * a);
If the d is negative, you should first convert it to a non-negative number before feed it into sqrt(). Remember C/C++ does not equal to math language.
My quick fix:
#include <complex>
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
float a, b, c, root1, root2, n, m;
cout << "Please input values for a, b and c to represent the variables in equation : ax^2 + bx + c " << endl; // Dont know how to raise 2 instead of using ^2
cin >> a >> b >> c;
const float d = (b * b) - (4 * a * c);
cout << "Determinant is equal to " << d << endl;
cout << setprecision(3); // to set number of decimal places
n = b / (2 * a);
if (d > 0) {
m = sqrt(d) / (2 * a);
root1 = (-b + sqrt(d)) / (2.0 * a); // positive root
root2 = (-b - sqrt(d)) / (2.0 * a); // negative root
cout << "Determinant is greater than zero, therefore the equation has two "
"real roots, which are "
<< root1 << " and " << root2 << endl;
} else if (d < 0) {
m = sqrt(-d) / (2 * a);
cout << "Determinant is less than zero, therefore there would be two "
"imaginary roots: "
<< n << " + " << m << "*i ";
cout << "and " << n << " - " << m << "*i" << endl;
}
}
A reference to sqrt() function:
http://www.cplusplus.com/reference/cmath/sqrt/
You will need to make some changes to get the correct answers for all cases First, you need to include the case of zero determinant which produces two identical real roots. You will need to calculate m with -d in the third case, and you have to make sure to change its sign into positive sign if necessary. For more aesthetic results, you can include special cases to omit n if it equals 0, and m if it equals 1.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float a, b, c, root1, root2, n, m;
cout << "Please input values for a, b and c to represent the variables in equation : ax^2 + bx + c" << endl;
cin >> a >> b >> c;
const float d = b * b - 4 * a * c;
cout << "Determinant is equal to " << d << endl;
cout << setprecision(3); // to set number of decimal places
if (d > 0)
{
root1 = (-b + sqrt(d)) / 2 / a;
root2 = (-b - sqrt(d)) / 2 / a;
cout << "Determinant is greater than zero, therefore the equation has two real roots, which are " << root1 << " and " << root2 << endl;
}
else if (d == 0)
{
root1 = -b / 2 / a;
cout << "Determinant is zero, therefore the equation has two identical real roots, which both are " << root1 << endl;
}
else
{
n = -b / 2 / a;
m = sqrt(-d) / 2 / a;
if (m < 0)
m *= -1;
if (n == 0)
{
if (m == 1)
cout << "Determinant is less than zero, therefore there would be two imaginary roots: i and -i" << endl;
else
cout << "Determinant is less than zero, therefore there would be two imaginary roots: " << m << "i and -" << m << "i" << endl;
}
else
{
if (m == 1)
cout << "Determinant is less than zero, therefore there would be two imaginary roots: " << n << " + " << "i and " << n << " - " << "i" << endl;
else
cout << "Determinant is less than zero, therefore there would be two imaginary roots: " << n << " + " << m << "i and " << n << " - " << m << "i" << endl;
}
}
}
Trying to let only numbers be a vaild input for a quadratic equation solver. I used bool and broke my code and no matter the input it just gives me my error message even if it is a vaild input.
After the progarm ask you to confirm the coefficients are correct, even if you put Y you get the " Please input a number. Please try again". how do you fix this?
I added the bool becasue that what my teacher showed me to use for checking inputs, very new to c++ and coding. I just added the whole code, the HW was to update HW 5 to do a few things more. I broke it trying to check inputs, when adding bool.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string first, last;
double a, b, c, x1, x2, discriminant, realpart, imaginarypart;
char ch;
cout << "Please enter First and Last name," << endl;
cout << " " << endl;
cout << "First Name= ";
cin >> first;
cout << "and" << endl;
cout << "Last Name=";
cin >> last;
cout << " " << endl;
cout << " Hello " << first << " " << last << " "
<< "welcome." << endl;
cout << " " << endl;
bool isCorrect{ true };
do {
start:
isCorrect = true;
cout << "Enter the coefficients of a: ";
cin >> a;
cout << " " << endl;
cout << "Enter the coefficients of b: ";
cin >> b;
cout << " " << endl;
cout << "Enter the coefficient of c: ";
cin >> c;
cout << " " << endl;
cout << " A = " << a;
cout << " B = " << b;
cout << " C = " << c << endl
<< endl;
cout << " Confirm the coefficients value are correct or not (y/n): " << endl;
cout << " " << endl;
cin >> ch;
if (ch == 'n' || ch == 'N')
goto start;
cout << " " << endl;
discriminant = b * b - 4.0 * a * c;
if (cin.fail())
;
{
isCorrect = false;
cin.clear();
cin.ignore(245, '\n');
cout << " Please input a number. Please try again" << endl;
}
} while (!isCorrect);
bool isExit(false);
if (a == 0) {
cout << " " << endl;
cout << "Error message: a can not = zero (0) " << endl;
}
else if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2.0 * a);
x2 = (-b - sqrt(discriminant)) / (2.0 * a);
cout << "Roots are real and different." << endl;
cout << " " << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 =" << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
cout << " " << endl;
x1 = -b / (2.0 * a);
cout << "x1 = x2 ="
" "
<< x1 << endl;
}
else {
//cout << "Error message: No real solution exist." << endl; // Part 1 error code for no real solutions
realpart = -b / (2.0 * a); //Code for part 2
imaginarypart = sqrt(-discriminant) / (2.0 * a); // Code for part 2
cout << "Roots are complex and different." << endl; // Code for part 2
cout << " " << endl;
cout << "x1 = " << realpart << "+" << imaginarypart << "i" << endl; // Code for part 2
cout << "x2 = " << realpart << "-" << imaginarypart << "i" << endl; // Code for part 2
}
cout << " " << endl;
cout << " Would you like to solve another quadratic equation (y/n): " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
goto start;
else
(ch == 'n' || ch == 'N');
return 0;
}
I get the following output:
This program is designed to aid in the analysis of triangles.
Would you like to specify the coordinates of a triangle (C),
or the lengths of their sides(S) ? Enter 'C' or 'S: s
Enter the length of side a: 3
Enter the length of side b: 4
Enter the length of side c: 5
The coordinates are A(0,0) , B(5,0) , C(nan,nan)
The angles are A = nan, B = nan and C = nan
The desired output is:
The coordinates of the triangle are at A(0,0), B(5,0) and C(3.2,2.4). The angles are A = 0.643501, B = 0.927295, and C = 1.5708
I've tried declaring variables in the beginning of main, as well as initializing them to 0. I either get a nan error or the incorrect values for my output
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double PI = 3.1415926535897932384626433832795;
int main() {
char mode = ' ';
cout << "\n\n";
cout << setw(10) << "" << "This program is designed to aid in the analysis of triangles. " << endl;
cout << setw(10) << "" << "Would you like to specify the coordinates of a triangle (C), " << endl;
cout << setw(10) << "" << "or the lengths of their sides(S) ? Enter 'C' or 'S: " ;
while (cin >> mode)
{
if (mode == 'C' || mode == 'c')
{
double Ax, Ay, Bx, By, Cx, Cy;
cout << "\n";
cout << setw(10) << "" << "Enter the x coordinate of point A: ";
cin >> Ax;
cout << setw(10) << "" << "Enter the y coordinate of point A: ";
cin >> Ay;
cout << setw(10) << "" << "Enter the x coordinate of point B: ";
cin >> Bx;
cout << setw(10) << "" << "Enter the y coordinate of point B: ";
cin >> By;
cout << setw(10) << "" << "Enter the x coordinate of point C: ";
cin >> Cx;
cout << setw(10) << "" << "Enter the y coordinate of point C: ";
cin >> Cy;
double a = sqrt((Cx - Bx) * (Cx - Bx) + (Cy - By) * (Cy - By));
double b = sqrt((Ax - Cx) * (Ax - Cx) + (Ay - Cy) * (Ay - Cy));
double c = sqrt((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By));
double A = acos(((b * b) + (c * c) - (a * a)) / 2 * b * c);
double B = asin(b*sin(A) / a);
double C = asin(c*sin(A) / a);
cout << "\n" << setw(10) << "" << "The lengths of the sides are: a = " << a << ", b = " << b << " and c = " << c << endl;
cout << setw(10) << "" << "The angles are A = " << A << ", B = " << B << " and C = " << C << endl;
}
else if (mode == 'S' || mode == 's')
{
double a = 0, b = 0, c = 0;
cout << setw(10) << "" << "Enter the length of side a: ";
cin >> a;
cout << setw(10) << "" << "Enter the length of side b: ";
cin >> b;
cout << setw(10) << "" << "Enter the length of side c: ";
cin >> c;
if (a + b > c && b + c > a && a + c > b)
{
double A = acos(((b * b) + (c * c) - (a * a)) / 2 * b * c);
double B = asin(b*sin(A) / a);
double C = asin(c*sin(A) / a);
double Ax = 0;
double Ay = 0;
double Bx = c;
double By = 0;
double Cx = b * (cos(A) * PI / 180);
double Cy = b * (sin(A) * PI / 180);
cout << setw(10) << "" << "The coordinates are A(" << Ax << "," << Ay << ") , B(" << Bx << "," << By << ") , C(" << Cx << "," << Cy << ")" << endl;
cout << setw(10) << "" << "The angles are A = " << A << ", B = " << B << " and C = " << C << endl;
}
else
cout << setw(10) << "" << "These lengths don't form a triangle. Try again";
}
else
{
cout << setw(10) << "" << "Please enter 'S' or'C': ";
cin >> mode;
}
}
}
multiplication(*) and division(/) operators are having same precedence and associativity is left-to-right, just change the angle A calculation to
double A = acos(((b * b) + (c * c) - (a * a)) / (2 * b * c));
So I'm trying to get my Quadratic equation solution code to loop unless "0" is entered as any 1 of the quadratic coefficients.
It works fine up until entering a non integer value, in which the program terminates.
I'd like the code to spit out a message prompting the user to enter a numerical value, and continue the loop as normal.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
for ( ; ; ){
float a, b, c, D, x1, x2, real, im;
cout << "Please enter the Quadratic Coefficients" << endl;
cin >> a >> b >> c;
if (cin.fail()){
cout << "Error, please enter numerical values!" << endl;
cin >> a >> b >> c;
}
if ((a == 0) || (b == 0) || (c == 0)){
break;
}
D = b*b - 4*a*c;
if (D < 0) {
real = -b/(2*a);
im = sqrt(-D)/(2*a);
cout << "Roots are Complex" << endl;
cout << "x1 = " << real << "+" << im << "i" << endl;
cout << "x2 = " << real << "-" << im << "i" << endl;
}
else if (D == 0) {
x1 = (-b + sqrt(D)) / (2*a);
cout << "Real and Repeated Roots" << endl;
cout << "x1 = " << x1 << endl;
}
else if (D > 0)
{
x1 = (-b + sqrt(D)) / (2*a);
x2 = (-b - sqrt(D)) / (2*a);
cout << "Real and Distinct Roots" << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
} }
return 0;
}
This solution here should help.
cin.fail() set the input stream into a failed state, and you need to reset it manually to get it to do any further work. When you call cin again, it will notice its failed state and just go on, otherwise.
cin >> a >> b >> c;
if (cin.fail()){
cin.clear(); //removes error flags
cin.ignore(); //ignores last input
cout << "Error, please enter numerical values!" << endl;
cin >> a >> b >> c;
}