Thread: Program converting degree to radians from 0 to n. Printing a # of lines

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Program converting degree to radians from 0 to n. Printing a # of lines

    Here's the problem:

    Write a C program to generate a table of conversion from degrees to radians. Your table should include a third column that contains the sine of the angle. Print 10 lines int he table with starting angle at 0 degree and allow the user to enter the ending angle in degrees.



    Here's my code so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.1416
     
     
    int main(void)
    {
           double radians,sinx;
           int degrees;
           int end;
          
          
     
           printf(" Enter the ending angle in degrees");
           scanf("%d",&end);
     
           for(degrees=0;degrees<=end;degrees=degrees+10)
     
     
           {
                 
                 
                  radians=degrees*PI/180;
                  sinx= sin(radians);
                  printf("%d %.3f %.3f\n",degrees,radians,sinx);
           }
           getch();
    }
    But my problem is mainly on what to do to just print 10 lines Here's my attempt.


    I tried using this:
    Code:
        for(degrees=0,k=1;degrees<=end,k<=10;degrees=degrees+10,k++)
    I don't know what to do to print 10 lines in the loop proportional to the ending degree value. I know I messed up incrementing it by 10 in here which would mess up the loop, but I don't know what else to do. Please any help would be appreciated.
    Last edited by tadm123; 10-03-2012 at 03:28 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're on the right track for you loop, printing exactly 10 lines. The problem is, as you stated, figuring out how much to increment by. Work it out on paper first. Start with some simple ones:

    From 0 to 10, 10 to 20, 100 to 110, etc.
    From 0 to 20, 40 to 60, 100 to 120, etc.

    Then onto more complicated ones.
    From 10 to 25, 30 to 75, etc.

    Figure out what it is you do on paper, paying attention to all the little steps and details involved. Once you understand how to do the problem in general, you can worry about coding it.

    Hint: there's subtraction and division involved.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You're almost there -- you just need to tweak the last expression inside the for statement.
    Code:
    for (degrees = 0; degrees <= end; degrees = degrees + <WHATEVER IS NEEDED TO GET TO end IN 10 ITERATIONS>)
    ~~~~~~~~

    But allow me to suggest an alternative.
    You know you only want to loop a definite number of times (10), so loop that number and fix the variables inside the loop accordingly
    Code:
    for (loopcount = 0; loopcount < 10; loopcount++)
    {
        degrees = /* fix degrees according to loopcount */;
        radians = degrees * PI / 180;
        sinx = sin(radians);
        printf("%d %.3f %.3f\n", degrees, radians, sinx);
    }

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Thanks so much everyone now it works, can't believe I missed something so simple all this time heh.

    Thanks again
    Last edited by tadm123; 10-03-2012 at 04:31 PM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by tadm123 View Post
    Code:
    #include <stdio.h>
    #include <math.h>
    #define PI 3.1416
      
      
    int main(void)
    {
           double radians,sinx;
           int degrees;
           int end,incr;
           
          
           
      
           printf(" Enter the ending angle in degrees");
           scanf("%d",&end);
      
           incr= (end-0)/10;
           for(degrees=0;degrees<=end;degrees=degrees+incr)
      
      
           {
                  
                  
                  radians=degrees*PI/180;
                  sinx= sin(radians);
                  printf("%d %.3f %.3f\n",degrees,radians,sinx);
           }
           getch();
    }
    Thanks so much everyone now it works, can't believe I missed something so simple all this time heh.

    Thanks again

    what happens when the user enters an value less than 10 ?
    Hint in integer math 9/10 = 0

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Oh thanks for pointing it out, i'll change it to double.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Actually hey man can you edit your last post where I posted the final code? No pressure but when you type the problem in google it's redirected here, I just don't want people trying to copy pasting it since it's for an exam.
    Last edited by tadm123; 10-03-2012 at 04:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program Degree to Radians
    By Cyberman86 in forum C Programming
    Replies: 5
    Last Post: 05-04-2009, 09:34 PM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. Printing an array in lines of 10.
    By furiousferret in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2004, 07:30 PM
  4. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM
  5. Printing multi-lines?
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2001, 04:52 PM