Thread: Help with assigning values to arrays/printing arrays

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    3

    Help with assigning values to arrays/printing arrays

    Hi, I have just begun to learn C and I have come across the following question:

    Using a for loop, construct two 100 element arrays, x and y, such that element i of x stores the value sin(2*pi*i/100)) and the corresponding element of y stores cos((2*pi*i/100)). Print the values stored in the elements of x and y as you calculate them.

    I have attempted to solve it but I'm not sure why the value 0 is only being printed, maybe I haven't assigned sin(2i/100)) and cos((2i/100)) to the arrays properly?


    Here is my attempt at the solution:


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    int main ()
    {
    
         /*Define arrays*/
         int x[100];
         int y[100];
         int i; 
       
             /* For loops for x and y using i*/
             
             for (i=0; i<100; i++)
                 {
                 
                 
                 x[i] = sin (((2.0*M_PI)*i)/100.0);
                 y[i] = cos (((2.0*M_PI)*i)/100.0);
                 
                 
                 printf(" Value of x %d: %d\n", i, x[i]);
                 printf(" Value of y %d: %d\n", i, y[i]);
                 
                 
                 }
         return(0);
    
    }
    Thanks

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest trying double instead of int for the result of sin functions.
    Note: the printf needs to be changed to output doubles.

    Code:
         double x[100];
         double y[100];
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    3
    Thank you for your help : D

    Also, I wondering why it works with double instead of int?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    sin() and cos() return values between -1 and 1. Converting to int truncates toward zero (except in C89, where the direction of rounding is unspecified). So any value except -1 and 1 (which might not be obtained from your sin() or cos() expressions because of rounding errors - pi cannot be exactly represented in floating point) will be rounded to zero.

    As Tim said, use floating point variables, and adjust the printf() format accordingly.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers, Arrays, Assigning values
    By Iconate in forum C Programming
    Replies: 3
    Last Post: 01-26-2011, 01:13 AM
  2. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  3. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  4. Assigning values to arrays using fscanf
    By Hallu in forum C Programming
    Replies: 5
    Last Post: 03-24-2004, 05:50 PM
  5. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 08:52 PM