:-/
Now, I'm trying practice on c++ basics, since few minutes I tried practice "for loop" especially, in console screen, I wrote this trivial program to calculate sin angles between angle (0 to 90) with for loop,
but when i build it, it gave me one result like my attach image, why program gave me one result..?

//sin angles from 0 to 90 | loop for
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double angle=0;
	double sinAngle = sin(angle);

	for (angle=0; angle<=90; angle++)
		cout << sinAngle << " " << endl;
	
	return 0;
}

Recommended Answers

All 9 Replies

I edit post and added code ..

You only did the calculation once:

double angle=0;
double sinAngle = sin(angle);

This doesn't magically assign that function to sinAngle, it evaluates it at angle 0 and prints it over and over again in your loop. Leave the declaration double sinAngle; outside and move the sinAngle = sin(angle); inside your loop.

commented: Indeed it does :) +19

I confused ..!
please, can you edit my code ..

Step through the code to see:

int main()
{
	double angle=0;       //angle = 0
	double sinAngle = sin(angle);  //sinAngle =  0 at this point

	for (angle=0; angle<=90; angle++)  
		cout << sinAngle << " " << endl;  //sinAngle hasn't changed
	                                       //and won't change so the value ^^^
                                              //is printed 91 times
	return 0;
}

Compare with (skip the other two variables):

for (double angle=0; angle<=90; angle++)  //since you're going by 1 anyway you could use int
		cout << sin(angle) << " " << endl;  //angle is changing each 
                                                                          //pass through

P.S. the math library uses radians so you need to go from 0 to Pi/2 if you want 0-90 in degrees

Thanks JONSCA for helping me ..

Do you understand it now (is the important question)?

SURE and thanks to you

This doesn't magically assign that function to sinAngle

Indeed it does :) - Salem

Clarification:
What I meant was that OP couldn't say double sinAngle = sin(angle); and then have the function sin(angle) (in the abstract) substitute back in for the value sinAngle down below like the OP wanted it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.