How can I fix the nan error in this triangle program? - c++

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));

Related

Very basic calculator issues

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;
}

Why do I keep getting zero and nan for these variables?

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;
}
}
}

How to get the result of a quadratic equation with negative root in c ++?

I am trying to obtain the result of a quadratic equation with negative root, but the result that I obtain is not the same when I do the operation on paper, also the result is not the same only with negative roots when it is the opposite, it works perfectly.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a, b, c;
double x1, x2;
cout << "Quadratic Formula" << endl;
cout << " -b + sqrt(b ^ 2) - 4ac" << endl;
cout << "x = -----------------------" << endl;
cout << " 2a" << endl;
cout << "" << endl;
cout << "a Value: " << endl;
cin >> a;
cout << "b Value: " << endl;
cin >> b;
cout << "c Value: " << endl;
cin >> c;
double sqRoot, det;
det = b * b - 4 * a * c;
if(det < 0)
{
sqRoot = sqrt(det * -1); //minus per minus = plus so it's possible to calculate the sqrt of
//the negative number
x1 = (-b + sqRoot)/ 2 * a;
x2 = (-b - sqRoot)/ 2 * a;
cout << "value of x1: " << x1 << " i"<< endl;
cout << "Value of x2: " << x2 << " i"<< endl;
}
else
{
sqRoot = sqrt(det);
x1 = (-b + sqRoot)/ 2 * a;
x2 = (-b - sqRoot)/ 2 * a;
cout << "Value of x1: " << x1 << endl;
cout << "Value of x2: " << x2 << endl;
}
}
You could use std::complex:
#include<iostream>
#include<cmath>
#include<complex>
using namespace std;
int main()
{
double a = 1.0;
double b = 4.0;
double c = 5.0;
double det = b * b - 4.0 * a * c;
auto sqRoot = sqrt(std::complex<double>(det));
auto x1 = (-b + sqRoot) / (2.0 * a);
auto x2 = (-b - sqRoot) / (2.0 * a);
cout << "value of x1: " << x1 << endl; // (-2,1)
cout << "value of x2: " << x2 << endl; // (-2,-1)
}
Your first problem is opeartion priority.
You should have a parenthesis in the following lines:
x1 = (-b + sqRoot)/ (2 * a);
x2 = (-b - sqRoot)/ (2 * a);
instead of
x1 = (-b + sqRoot)/ 2 * a;
x2 = (-b - sqRoot)/ 2 * a;
The second problem is your division using int variables.
The code shall be like the following:
#include<iostream>
#include<cmath>
#include <iomanip>
using namespace std;
int main()
{
int a, b, c;
double x1, x2;
cout << "Quadratic Formula" << endl;
cout << " -b + sqrt(b ^ 2) - 4ac" << endl;
cout << "x = -----------------------" << endl;
cout << " 2ac" << endl;
cout << "" << endl;
cout << "a Value: " << endl;
cin >> a;
cout << "b Value: " << endl;
cin >> b;
cout << "c Value: " << endl;
cin >> c;
double sqRoot, det;
det = b*b - 4*a*c;
// complex roots
if(det < 0)
{
sqRoot = sqrt(det *(-1)); //minus per minus = plus so it's possible to calculate the sqrt of
//the negative number
// HERE ALSO BUT SOLUTIONS ARE COMPLEX SO YOU NEED TO SHOW IMAGINARY PART AND REAL PART
// x1 = (-b + sqRoot)/ (2 * a); // ==>x=-b/(double)(2*a) and y =i*sqRoot/(double)(2*a)
// x2 = (-b - sqRoot)/ (2 * a);//==>x=-b/(double)(2*a) and y =-i*sqRoot/(double)(2*a)
cout << "value of x1: " << std::setprecision (4)<< (-b/(double)(2*a)) << "+"<<sqRoot/(2*a)<<*"i"<< endl;
cout << "Value of x2: " << std::setprecision (4)<< (-b/(double)(2*a)) << -sqRoot/(2*a)<<"i"<<endl;
}
else
{
sqRoot = sqrt(det);
// ALSO HERE
x1 = (-b + sqRoot)/ (2 * a);
x2 = (-b - sqRoot)/ (2 * a);
cout << "Value of x1: " << x1 << endl;
cout << "Value of x2: " << x2 << endl;
}
}
the results can be tested below:
3x^2 + 4x + 2 = 0
gives for the execution:
Quadratic Formula
-b + sqrt(b ^ 2) - 4ac
x = -----------------------
2ac
a Value:
3
b Value:
4
c Value:
2
value of x1: -0.6667+0.4714i
Value of x2: -0.6667-0.4714i

Programming a manual square root function?

For my class I'm programming a square root function using inclusion. No, I may not use any other method...
This is my code so far, with the program nearly working. It works for perfect square roots and some other values (like 11 or 5) but it gets into an infinite loop for others (8, 2).
The reason why this happens is that the upper and lower bounds (b and a) do not change. Ideally, the bounds would be current x and previous x, creating new x. What happens is that new x is currently formed by current x and either a or b, being a constant.
I've tried for so long but I have not yet found a way to 'remember' or find the 'previous x', as every time the while loop repeats, only the current x is available for use. Anyone know how such a problem could be solved?
void inclusion ()
{
double v ;
cout << "*** Now solving using Inclusion ***" << endl << "To calculate the square root, enter a positive number: " ;
cin >> v ;
while (v<0)
{
cout << "Square roots of negative numbers cannot be calculated, please enter a positive number: " ;
cin >> v ;
}
cout << endl ;
int n = 0;
while (v >= n*n)
n++ ;
double b = n ;
double a = n-1 ;
int t = 0 ;
double x = (a+b)/2 ;
while ((x * x - v >= 0.1) || (x * x - v <= -0.1))
{
t++ ;
if (x * x < v)
{
cout << "Lower Bound: " << x << '\t' << '\t' ;
cout << "Upper Bound: " << b << '\t' << '\t' ;
x = (b + x)/2 ;
cout << "Approximation " << t << ": " << x << endl ;
}
else
{
cout << "Lower Bound: " << a << '\t' << '\t' ;
cout << "Upper Bound: " << x << '\t' << '\t' ;
x = (a + x)/2 ;
cout << "Approximation " << t << ": " << x << endl ;
}
}
cout << endl << "The answer is " << x << ". Iterated " << t << " times." << endl << endl ;
}
I have not yet found a way to 'remember' or find the 'previous x'
Have a variable previous_x that you previous_x = x at the end of the loop
But that's not your problem. You are changing x, but not a or b, so you get into an infinitely repeating pattern. You should instead adjust whichever bound brings you tighter in.
void inclusion ()
{
double v ;
cout << "*** Now solving using Inclusion ***" << endl << "To calculate the square root, enter a positive number: " ;
cin >> v ;
while (v<0)
{
cout << "Square roots of negative numbers cannot be calculated, please enter a positive number: " ;
cin >> v ;
}
cout << endl ;
int n = 0;
while (v >= n*n)
n++ ;
double b = n ;
double a = n-1 ;
int t = 0 ;
double x;
for (x = (a+b)/2; abs(x * x - v) >= 0.1; x = (a+b)/2, ++t)
{
if (x * x < v)
{
cout << "Lower Bound: " << x << '\t' << '\t' ;
cout << "Upper Bound: " << b << '\t' << '\t' ;
a = (b + x)/2 ;
cout << "Approximation " << t << ": " << x << endl ;
}
else
{
cout << "Lower Bound: " << a << '\t' << '\t' ;
cout << "Upper Bound: " << x << '\t' << '\t' ;
b = (a + x)/2 ;
cout << "Approximation " << t << ": " << x << endl ;
}
}
cout << endl << "The answer is " << x << ". Iterated " << t << " times." << endl << endl ;
}
You need to update the bounds too:
a = x;
x = (b + x)/2;
and
b = x;
x = (a + x)/2;

C++ Quadratic Code Errors

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;
}