Thread: Beginner at C! help...Array problem

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Beginner at C! help...Array problem

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

    cant see where im wrong?

    Many thanks!!!!

    insert
    Code:
    #include <stdio.h> 
    #include <stlib.h> 
    #define PI 3.142
    
    
    
    int main()
    
    {
    //stating variable type and titles of the arrays
    
    int i, x[100], y[100]; 
    
       for(i=0; i<=100; i++)
      
       x[i]=sin((2*PI*i)/100)); //in general for every element
       y[i]=cos((2*PI*i)/100));
       
       printf("%.2f\t %.2f\n",x[i],y[i]);
       
    
       scanf("d\n");
       
       exit(0);
       }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Okay well heres some corrections:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <math.h> // Need to include math.h for sin and cos
    #define PI 3.142
    
    int main()
    {
        //stating variable type and titles of the arrays
        int i;
        float x[100], y[100]; // X and Y should be floats of doubles
        for(i=0; i<=100; i++)
        {
            x[i]=sin((2*PI*i)/100); //brackets dident match here.
            y[i]=cos((2*PI*i)/100);
            printf("&#37;.2f\t %.2f\n",x[i],y[i]);
        }
        getchar(); 
        exit(0);
    }
    Edit: oh and you might want to get into the habit of indenting as it makes your code much more readable
    Last edited by mike_g; 02-18-2008 at 07:08 AM.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Thanks!!!!! your the master.

    why must x y be floats of doubles?

    why not floats of floats?

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Np

    you need to use a float or double to store numbers with fractions. Either will do but doubles give better precision. Sin and cos return numbers between -1 and 1 so using integers all numbers would end up being -1, 0, or 1.

    Also, technically, not that it matters too much here, but sin is meant to be for y, and cos for x.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Okay..thanks!

    This question develops: "compute the dot products of x.x,, y.y, x.y checking that sinx and cosx are orthogonal..x.y=sum(x.y) "

    I am really stuck here. Maybe that is my maths. My attempt is as pitifully follows:

    include
    Code:
    #include <stdio.h> 
    #include <stlib.h> 
    #define PI 3.142
    
    int main()
    
    {
    //stating variable type and titles of the arrays
    int i;
    float ,sinC, x[36], y[36], sumsq, sumsr, sumss;
    
       for(i=0; i<=360; i++)
      {
       
       
       x[i]*y[i]= sin
       y[i]*y[i]=  
      
       //shall i define the arrays? or the mulitiplied arrays?  
       
       x.y=absinc?
      
      
       sumsq=x[i]*x[i]
       sumsr=y[i]*y[i] 
       sumss=x[i]*y[i]
       
       
       
       
       
       printf("&#37;.2f\t %.2f% %.2f\n",sumq, sumsr, sumss);
       }
    
       scanf("d\n");
       
       exit(0);
       }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would like you to remember that everything within the block { and } is part of the parent statement, in this case the for. This was your problem in your last post.
    And your indentation is not very good. You are going to get into troubles with poor indentation. I recommend you read this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 12:04 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM