9 Replies - 3608 Views - Last Post: 15 February 2011 - 03:17 PM Rate Topic: -----

#1 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • 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("..0301 / 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  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • 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
     }
}


Was This Post Helpful? 1
  • +
  • -

#4 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 01:54 PM

View Postsimeesta, 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));
}



Was This Post Helpful? 0
  • +
  • -

#5 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • 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

Was This Post Helpful? 2
  • +
  • -

#6 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 02:49 PM

View Postsimeesta, 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));
}


Was This Post Helpful? 0
  • +
  • -

#7 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • 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

Was This Post Helpful? 1
  • +
  • -

#8 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 03:01 PM

View Postsimeesta, 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

Was This Post Helpful? 0
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 218
  • View blog
  • Posts: 591
  • 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))
Was This Post Helpful? 1
  • +
  • -

#10 iqbalmmz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 15-February 11

Re: Printing out sine values of numbers 0 between 360

Posted 15 February 2011 - 03:17 PM

View Postsimeesta, 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));
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1