Thread: EASY Function: Array Average

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    10

    EASY Function: Array Average

    What is an example of a function I would use to find the average value if I have an array of known size?

    i.e.

    Code:
    const int LENGTH = 5;
    double a[LENGTH] = {1,5,6,8,10};
    double avg;
    double sum;
    any ideas? I know this is probably super easy...

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How would you do the calculation if you were doing it by hand?

    Generally, the same method that you'd use by hand works on computer. In this case, you'd add the numbers together and divide by the number of "samples" (elements in the array).

    --
    Mats

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Yea, i can do it by hand easily...what I don't understand is how to add each element to the next one. If you can help me with that I would really appreciate it. Here's what I have:
    Code:
    double avg = 0;
    	for(int i=0; i<npts; i++)
    		avg = (avg + a[i])/5;
    	return avg;
    is this even close?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Would you divide by 5 each time if you were doing it by hand?

    Or would you only divide the total at the end just the once?
    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.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    I figured out how to do the avg:
    Code:
    int main()
    {
    	const int SIZE = 5; //max array size
    	double x[SIZE] = {1,5,6,8,10};
    	int num_points = 0;
    
    	double sum = 0;
    	double avg;
    	for(int i=0;i<SIZE;i++)
    	{
    	sum = sum + x[num_points];
    	num_points++;
    
    	}
    	avg = sum/SIZE;
    	cout<<"the average of the values in the array is:"<<endl;
    	cout<<avg<<endl;
    	//for(int i=0; i<SIZE; i++)
    	//{
    	return 0;
    }
    now the problem gets even more fun

    thanks for the help

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by benjamin923 View Post
    I figured out how to do the avg:
    Code:
    int main()
    {
    	const int SIZE = 5; //max array size
    	double x[SIZE] = {1,5,6,8,10};
    	int num_points = 0;
    
    	double sum = 0;
    	double avg;
    	for(int i=0;i<SIZE;i++)
    	{
    	sum = sum + x[num_points];  /* delete this line, num_points is an unneeded variable, just use i */
                /* indenting inside curly braces is a BIG help for us poor humans - please do it */
                sum += x[i];   /* this is C standard idiom, learn it well */
    	num_points++;  /* delete this line */
    
    	}
    	avg = sum/SIZE;
    	cout<<"the average of the values in the array is:"<<endl;
    	cout<<avg<<endl;
    	//for(int i=0; i<SIZE; i++)
    	//{
    	return 0;
    }
    now the problem gets even more fun

    thanks for the help
    The advice to do your program similarly to how you'd do it by hand, is especially good for beginner's. If you can't do something, it's a rare day indeed that you'll be able to program a computer to do that something.

    If you can do a task by hand, it gives a very fertile soil from the description of exactly how you do it, to let the seed of a program, grow.
    Last edited by Adak; 08-02-2007 at 05:19 PM.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    10
    Thanks for that advice - anything to save time and space helps a bunch.
    now if i can only figure out the other part....(next post on board) AHHHHH

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Why post C++ code in the C section? At least learn what language you're learning before you start learning it.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Agreed - moved.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM