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. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    8

    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++){
       printf("Please enter the values:\n", i+1);
       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
    Last edited by kromagnon; 11-06-2011 at 12:59 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    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.
    Last edited by stahta01; 11-06-2011 at 02:22 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    8
    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. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    Quote Originally Posted by kromagnon View Post
    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.
    http://upload.wikimedia.org/wikipedi...d9fd08e730.png

    Hint2: create a function print_text that just displays the value in the array.
    Once you get that to work, you will know more about writing RMS function.
    Code:
    void print_text(double *x, int N);

    Tim S.
    Last edited by stahta01; 11-06-2011 at 07:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 01:22 PM
  2. Filling an array
    By GCNDoug in forum C Programming
    Replies: 18
    Last Post: 04-25-2007, 03:46 PM
  3. Filling an array
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 02:02 PM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM

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