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;
}