Like Tree3Likes
  • 1 Post By phantomotap
  • 2 Post By anduril462

RMS from an array

This is a discussion on RMS from an array within the C Programming forums, part of the General Programming Boards category; yes this is my homework but to be fair i have given this a good shot and am really struggling ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    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. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by maximgrew View Post
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    3
    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;
                                             
                                                            printf("%d\n^ your average\n",avg);
                                            }  
                                               
                                   }
                                   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. #5
    Registered User
    Join Date
    May 2012
    Posts
    3
    sorry completely forgot to add that option three will be the one displaying the RMS

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Well the first step would be a sum-of-squares

    sum = sum + array[i] * array[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,415
    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
    stahta01 likes this.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by Salem View Post
    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.
    manasij7479 and stahta01 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 07:41 AM
  2. Replies: 2
    Last Post: 03-20-2012, 09:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 03:31 PM
  4. Replies: 1
    Last Post: 10-21-2007, 08:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21