Thread: Array Problem

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    8

    Array Problem

    Hi I'm having some problems with this code. This program generates sine waves and inputs the value into an array. Originally, I had it so it was like void create_sine(double y[], int N) but that gave me this error (which is why I had to use the lower case n, but why it works I do not understand and I'm sure my prof. does not want it like that.)

    10 expected `,' or `...' before numeric constant

    now I'm getting these errors and I'm stumped.

    p1.c:7: warning: initializer element is not constant
    p1.c:7: warning: (near initialization for 'pi')
    p1.c:7: error: initializer element is not constant
    p1.c: In function 'main':
    p1.c:28: warning: ISO C90 forbids variable-size array 'y'

    Any help would be appreciated!
    Code:
    #include <stdio.h>
      #include <math.h>
      #include <stdlib.h>
      #include <time.h>
       
      #define N 100
      double r, pi = 4*atan(1), f = 1500, w = 2*pi*f, t ; 
      int i; int n=N;
      
      void create_sine(double y[], int n)
    {
         for( i = 0; i < N; ++i)
         {
              t = i/8000.0;
           y[i] = r + sin(w*t);
           }
    }
    void print_array(double y[], int n)
    {
         for (i=0; i<N; ++i)
        {
            printf("%f\n",y[i]);
            }
    }
         
      int main( void)
      {
        int seed; double y[n];
        void create_sine(double y[], int n);
        void print_array(double y[], int n);
        
       
        seed = time(0);
       
        srand(seed);
        create_sine( y, N);  
        print_array( y, N);
        
       
        return 0;
      }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [grumble]Globals bad.[/grumble]
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define N 100
    double r, pi, f = 1500, w, t ; 
    int i; int n=N;
    
    void create_sine(double y[], int n)
    {
       for ( i = 0; i < N; ++i )
       {
          t = i/8000.0;
          y[i] = r + sin(w*t);
       }
    }
    void print_array(double y[], int n)
    {
       for ( i=0; i<N; ++i )
       {
          printf("%f\n",y[i]);
       }
    }
    
    int main( void)
    {
       int seed; double y[n];
       void create_sine(double y[], int n);
       void print_array(double y[], int n);
    
       pi = 4*atan(1);
       w = 2*pi*f;
    
       seed = time(0);
    
       srand(seed);
       create_sine( y, N);  
       print_array( y, N);
    
    
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    8
    Sweeeet! thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM