# Thread: RMS from an array

1. ## RMS from an array

yes this is my homework but to be fair i have given this a good shot and am really struggling so any help even if you only want to point me in the correct direction would be great

i have opened a .txt file containing integers of a sin wave arranged like this

1
2
3
4
5
6
6
5
and so on..

and i have used a loop to put them into an array but am really struggling to find the RMS of the values and then printf the result any help would be great! thanks max

2. Originally Posted by maximgrew
yes this is my homework but to be fair i have given this a good shot
Well... show us that 'good shot' and everyone here would be happy to suggest improvements and point out mistakes.

3. > but to be fair i have given this a good shot
You did?

Your attempt at posting code seems to have been a complete "miss".

How much do you know about RMS?
Does this help -> Root mean square - Wikipedia, the free encyclopedia

The first formula is for an array of numbers of known length.

4. so this is my code so far sorry its quite messy, and yeah had a look at the wiki and and rosetta code had a play around with what they suggest but cant get it to work. as you can see for "option 2" i have i have worked out the average from the integers stored in the array but im at a loss for the RMS

cheers max

Code:
```
#include <stdio.h>
#include <math.h>

main(){
int sum = 0;
int count = 0;
int in;
int i;
int j = 0;
int array[10000];
FILE *instream;
int avg;

instream = fopen ("data1.txt", "r");
char str[1000];
int input;
while ( !feof(instream) )
{

fscanf(instream,"%d",&in);
array[count] = in;
count++;
}
while (input !=5)
{
printf ("please select an option\n\r" "Enter 1 to print out all values\n" "Enter 2 to calculate the mean value\n" "Enter 3 to calculate the RMS\n" "option 4\n" "option 5\n\n");

fgets(str,1000,stdin);
sscanf(str, "%d", &input);

if (input ==1)

{
printf ("you have entered 1\n");
for ( i = 0 ; i < count ; i++ )
{
printf("%d\n",array[i]);

}
}

else if (input ==2)
{
printf ("you have entered 2\n");
for ( i = 0 ; i < count ; i++ )

sum = sum + array[i];
{
avg = sum/count;

}

}
else if (input ==3)
{
printf ("you have entered 3\n");

}
else if (input ==4)
{
printf ("you have entered 4\n");
}
else if (input ==5)
{
printf ("you have entered 5, the program will now close\n");
}
}

getchar ();
}```

5. sorry completely forgot to add that option three will be the one displaying the RMS

6. Well the first step would be a sum-of-squares

sum = sum + array[i] * array[i];

7. What's with the insane nesting?

The point of posting the code is so that we could look over it and make suggestions; we can't make such suggestions if we can't reasonably follow the code.

*shrug*

In any event, start a little simpler.

Start with a simple ("hard-coded") array with values taken from a known sample and get the mathematics working. You can add the interface stuff once you have the mechanics down.

Soma

8. Originally Posted by Salem
How much do you know about RMS?
Does this help -> Root mean square - Wikipedia, the free encyclopedia
I was sure he was talking about this RMS: Richard Stallman - Wikipedia, the free encyclopedia. RMS lives in every array, and in every bit of code, no matter how big or small.

@maximgrew:
A few other things:

• Declare main to explicitly return an int, then return one (usually 0) at the bottom of main. Read this: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
• FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com. Use the return value of fscanf or fgets to control your loop.
• If you declare array to have 10,000 slots, then #define a constant for this, and use that constant in the declaration. Also use the constant in your input loop, and all relevant loops, to ensure that if you have a very large file, you don't have an array overrun. Alternatively, count the entries in your file ahead of time (store that count in a variable), and use variable length arrays or malloc/free to allocate exactly the right number of slots. Use that variable in loops to avoid an array overrun.
• Don't make lines 180+ characters long, it's just a pain. Make a separate print statement for each line of output (your menu choices).
• As phantomotap pointed out, code clarity matters. Messy code means it's easy to make mistakes and hard to find or fix them. Read this: SourceForge.net: Indentation - cpwiki.