1. ## Compiled code crashing

Have been trying to learn C and complete an assignment that gives the RMS of a set of input numbers but the program crashes when run. Any advice appreciated on what's wrong with the code?
Code:
```#include <stdio.h>
#include <math.h>

double RMS(double *x, int N)

{
int i;
double sum =0.0;
for (i=0; i<N; i++)
{
sum = sum + pow(x[i],2);
}
double question = sum/N;
sqrt (question);
return question;
}
int main()
{
int i, N;
double question;

double x [N];
printf("Enter the number of values in the sequence please: \n");
scanf("%d", &N);
for (i=0;i<N;i++)
{
printf("Enter the numbers in the sequence please: \n");
scanf("%lf", &x[i]);
}
question = RMS(x,N);
printf(" The RMS of your selected numbers is: %f. \n", RMS);

return 0;
}``` 2. Originally Posted by cMacD Have been trying to learn C and complete an assignment that gives the RMS of a set of input numbers but the program crashes when run. Any advice appreciated on what's wrong with the code?
Code:
```int i, N;
double question;

double x [N];```
First glance, the statement double x[N] happens before 'N' has any value. 3. How could I change this so i doesn't give me errors, because when i move the double x[N], after the part below it i get a list of errors about x being undeclared
Code:
```printf("Enter the number of values in the sequence please: \n");scanf("%d", &N);
for (i=0;i<N;i++)
doublex [N];``` 4. Putting it inside the for() loop means you're declaring x[N] every iteration.

And doublex [N] != double x[N] 5. Could this cause the program crash? It doesn't even ask for input before shutting down. 6. You likely need to use dynamic memory allocation via malloc or calloc. 7. the sqrt(question) on line 18 does nothing. 8. Variable Length Arrays or arrays defined at runtime require a C99 compliant compiler. Guessing yours isn't.

Hard code the size of the array, just use N as last position to go to in your array, and test that user input N doesn't exceed size of your array. Popular pages Recent additions 