Printing out sine values of numbers 0 between 360

Page 1 of 1

9 Replies - 14203 Views - Last Post: 15 February 2011 - 03:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=216329&amp;s=b1338905d1133f3d4ceb598845d2756a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 01:00 PM

Hi Guys

I need help doing my homework. I've started it but I think I'm doing it wrong, I need to print out a table of sine value for a trigonometric circle (angles 0 thru 360). Here is what I have currently:

```# include <stdio.h>
# include <math.h>
#definePI = 3.14

/*	Write a program that will print out a table of sin function values for angles between 0 and 	360 degrees in 30 degree increments.  Use formatting to line up the columns neatly.  	Remember that the trigonometric functions expect the angle to be in radians, not degrees.  	You will need 12 print statements.  Use copy and paste to reduce typing effort.
*/

main ()

{
printf("..Sin(x)…..Value..\n");
printf("..000…...…0000..\n");
printf("..030………1 / 2..\n");
printf("..060\n");
printf("..090\n");
printf("..120\n");
printf("..150\n");
printf("..180\n");
printf("..210\n");
printf("..240\n");
printf("..270\n");
printf("..300\n");
printf("..330\n");
printf("..360\n");
}

```

This seems too tediously to me, I think I am not suppose to actually write in the values myself, the math header should do it for me. Any help will be appreciated, thanks.

Is This A Good Question/Topic? 0

Replies To: Printing out sine values of numbers 0 between 360

#2 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 01:34 PM

It would be better to use a loop.

e.g. prints out numbers 1 to 5
```int i;
for(i=1;i<=5;i++)
{
printf("%d\n",i);
}

```

This post has been edited by simeesta: 15 February 2011 - 01:35 PM

#3 Plus

• D.I.C Regular

Reputation: 41
• Posts: 414
• Joined: 24-November 08

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 01:50 PM

guess it would looks similar to this ...

```#include <math.h>
#include <stdio.h>

void main()
{
int i = 0;

while (i <= 360)
{
printf(" sin ( %d ) = %d \n", i, sin(i) );

i = i + 45; // 0, 45, 90, .., 360
}
}

```

#4 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 01:54 PM

simeesta, on 15 February 2011 - 01:34 PM, said:

It would be better to use a loop.

e.g. prints out numbers 1 to 5
```int i;
for(i=1;i<=5;i++)
{
printf("%d\n",i);
}

```

I see, what I was doing didn't feel right but now I don't know how to do this for increments of 30 degrees(radian), is there an more efficient way to write this:

```
#include<stdio.h>
#include<math.h>
#definePI = 3.14

main()
{
float i;
for(i=0; i<= 2*PI;)
printf("%f %f\n", i, sin(i));
}

```

#5 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 02:02 PM

instead of i++ use i+=30 or (i=i+30), they do the same thing.

@Plus, NEVER use void main(). It's ALWAYS int main()

On a side note I think sin x takes the value x, in radians so you'll have to convert degrees to radians. 2 * pi radians = 360 degrees . (or start at zero and increment by PI/6 e.g i+=(PI/6.0).

and it should be
```#define PI 3.14
```

A space between define and pi.

This post has been edited by simeesta: 15 February 2011 - 02:04 PM

#6 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 02:49 PM

simeesta, on 15 February 2011 - 02:02 PM, said:

instead of i++ use i+=30 or (i=i+30), they do the same thing.

@Plus, NEVER use void main(). It's ALWAYS int main()

On a side note I think sin x takes the value x, in radians so you'll have to convert degrees to radians. 2 * pi radians = 360 degrees . (or start at zero and increment by PI/6 e.g i+=(PI/6.0).

and it should be
```#define PI 3.14
```

A space between define and pi.

okay, I did all the corrections you made but now I'm not able to compile, it says theres a syntax error on line 5 (the printf). I can't find any syntax errors maybe you can?

```main ()
{
float i;
for(i=0; i += (PI/6));
printf("%f %f\n", i, sin(i));
}

```

#7 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 02:56 PM

You need a condition in the for loop.

You have initialization, i=0; and increment (i+=(PI/6)), where is condition. eg i<(2*PI).

for(i=0;i<5;i++)

and remove the ';' after the for loop

This post has been edited by simeesta: 15 February 2011 - 02:58 PM

#8 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 03:01 PM

simeesta, on 15 February 2011 - 02:56 PM, said:

You need a condition in the for loop.

You have initialization, i=0; and increment (i+=(PI/6)), where is condition. eg i<(2*PI).

for(i=0;i<5;i++)

and remove the ';' after the for loop

I don't understand, can you just do it so I can see? I am an absolute beginner to coding

This post has been edited by iqbalmmz: 15 February 2011 - 03:02 PM

#9 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 03:04 PM

for(int i=0;i<(2*PI);i+=(PI/6.0))

#10 iqbalmmz

Reputation: 0
• Posts: 94
• Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 03:17 PM

simeesta, on 15 February 2011 - 03:04 PM, said:

for(int i=0;i<(2*PI);i+=(PI/6.0))

Okay, now I see what you were saying. Thanks for your help. This is what I got at the end

```main ()
{
float i;
for(int i=0;i<(2*PI);i+=(PI/6.0))
printf("%f %f\n", i, sin(i));
}

```