How do I use IF in FOR correctly ?

Hello, I need to do some exercises, in which I need to use IF in FOR, and im really having trouble with it. The result I need to get in exercises :
1st exercise : if y1=y4, it has to say "Crosses", otherwise "***". In 2nd one, if y>0, it has to say "***".
What I have wrote in 1st and 2nd exercise, but no idea on how to use if to get the correct answers on y. Here is what I have wrote without if, and the results I need to get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
 double x,y;
 for (x=15;x<=25;x++)
 {
    y=sqrt(x*x+3*x-500);
    cout << x << "  " << setprecision(3) << y << endl;
 }
    return 0;
}


What I need to get:
x y
15 ***
16 ***
17 ***
18 ***
19 ***
20 ***
21 2.00
22 7.07
23 9.90
24 12.17
25 14.14

2nd exercise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
 double x,y1,y2;
 for (x=0;x<=10;x++)
 {
    y1=2*x+2;
    y2=3*x+1;
    cout << x << "  "  << y1 << "  " << y2 << endl;
 }
    return 0;
}


What i need to get :
x y1 y2
1 2 1 ***
1 4 4 Crosses
2 6 7 ***
3 8 10 ***
4 10 13 ***
5 12 16 ***
6 14 19 ***
7 16 22 ***
8 18 25 ***
9 20 28 ***
10 22 31 ***

Would be really grateful for some help here.
Last edited on
So for your first problem you are getting an error for y. The reason that is happening is because you are trying to take the square root of a negative number. Try using an if statement to display *** if (x*x+3*x-500) < 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	double x, y;
	cout << setw(3) << left << "x" << "y" << endl;
	for (x = 15; x <= 25; x++)
	{
		y = sqrt(x*x + 3 * x - 500);
		if ((x * x + 3 * x - 500) < 0)
		{
			cout << x << " " << "***" << endl;
		}
		else
		cout << fixed << setprecision(0) << x << " " << fixed << setprecision(2) << y << endl;
	}
	return 0;
}
closed account (48T7M4Gy)
First one:
1
2
3
4
5
calculate z= x*x + 3*x etc
if ( z < 0)
   display ***
else
   display sqrt(z)
Please feel free to ask if you don't understand some of this. I tried to use only the stuff in the beginning of a college c++ book.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	double x, y1, y2;
	cout << setw(3) << left << "x" << setw(3) << left << "y1" << setw(3) << left << "y2" << endl;
	for (x = 0; x <= 10; x++)
	{
		y1 = 2 * x + 2;
		y2 = 3 * x + 1;
		if (y2 != y1)
		{
			cout << setw(3) << left << x << setw(3) << left << y1 
                                << setw(3) << left << y2 << "***" << endl;
		}
		else
			cout << setw(3) << left << x << setw(3) << left << y1
                               << setw(3) << left << y2 << "Crosses" << endl;
	}
	return 0;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
int main()
{
 double x,y, z;
 for (x=15;x<=25;x++)
 {
    z = x * x + 3 * x - 500;
    if (z < 0)
        cout << x << "   ***";
    else
        cout << x << "  " << setprecision(3) << sqrt(z);

     cout << endl;
 }
    return 0;
}
Last edited on
Thank you for your help. I didnt think its that complicated with all those setw and left. And I have one question about 1st exercise. When I get y, why if(y<0) doesnt work ? I know its square root, but no idea why it doesnt work.
closed account (48T7M4Gy)
Unless you are working in the complex plane, the square root of -ve numbers does not exist.

The inverse of the square root is a squared number which is always positive.
If a= sqrt(x) then x = a*a and if a is -ve or +ve x will always be +ve because +ve * +ve is +ve, and -ve * -ve is +ve.
So there is no way to get a -ve for x unless complex/imaginary i is allowed, which it isn't. :)
Last edited on
Okay, thank you ^^
Topic archived. No new replies allowed.