# filling an array..

This is a discussion on filling an array.. within the C Programming forums, part of the General Programming Boards category; Hi, I've got this homework: Write a program that calculates the Root-Mean-Square (RMS) of a sequence of numbers whose length ...

1. ## filling an array..

Hi,
I've got this homework:
Write a program that calculates the Root-Mean-Square (RMS) of a
sequence of numbers whose length and content are determined by the
user. The RMS calculation should be done in a function of prototype
double RMS(double *x, int N), where N is the number of elements
in the array x. Demonstrate its use in a main function which prompts
the user for the length of the sequence, and then the values of this
sequence.

And I've got that far:
Code:
```#include <stdio.h>
#include <math.h>

double RMS(double *x, int N)
{
float rms;
int i;
for (i=0; i<N; i++) {
rms=sqrt(x[N^2]);
}

return rms;
}

int main()
{
int i, N;
float rms;
printf("Please enter the length of the sequence:\n");
scanf("%d", &N);
double x[N];
for (i=0;i<N;i++){
scanf("%d", &x[N]);
}
rms=RMS(x,N);
printf("%f", rms);

return0;
}```
It doesn't give me any errors but neither does it work.. Can someone please tell me if I'm on the right track?

Thanks

2. This is wrong; the "^" is NOT the power operator in C.
Look for pow or power in math library. http://www.cplusplus.com/reference/clibrary/cmath/pow/
Code:
`rms=sqrt(x[N^2]);`
Even if it was right the equation is wrong.
http://en.wikipedia.org/wiki/Root_mean_square

Tim S.

3. Thanks for reply. Sorry if I'm a bit slow but I'm quite new to programming..
I changed the function but it still doesn't work.
Code:
```#include <stdio.h>
#include <math.h>

double RMS(double *x, int N)
{
float rms;
int i;
int y=2;
int z=1;
for (i=0; i<N; i++) {
rms=sqrt((z*pow(*x,y))/N);
}

return rms;
}```
Unfortunately I have no idea how to type it properly..

4. Originally Posted by kromagnon
Code:
```	for (i=0; i<N; i++) {
rms=sqrt((z*pow(*x,y))/N);
}```
How does the value of rms differ from being inside the for loop?
Hint: No matter how many times it runs in the for loop the answer is the same.

Your math is wrong. And, the value of *x does not change for each loop run.
`void print_text(double *x, int N);`