CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2007
    Posts
    2

    Angry getting average!!

    hi im stuck.
    lets say i have
    int count =6;
    and make and array of
    float n[6]={1,2,3,4,5,6},total,average;
    and i want it to add all the numbers up
    How would i do this in a loop?
    NOT this way
    n[0]+n[1]+n[2]+.... then /count

  2. #2
    Join Date
    Mar 2005
    Location
    Romania,Cluj-Napoca
    Posts
    1,073

    Re: getting average!!

    Code:
    float sum = 0;
    float n[6]={1,2,3,4,5,6}
    
    for( int i = 0; i<count; ++i)
    {
        sum += n[i];
    
    }
    
    float average = sum/count;
    Good luck!

    Gili
    Please use code tags [code] [/code]

    We would change the world, but God won't give us the sourcecode..
    Undocumented futures are fun and useful....
    ___
    ______
    Gili

  3. #3
    Join Date
    Feb 2007
    Posts
    2

    Re: getting average!!

    thnx its working fine now

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: getting average!!

    Or, with the use of accumulate
    Code:
    #include <algorithm>
    
    float n[6]={1,2,3,4,5,6}
    float sum = std::accumulate(&n[0], &n[5], 0);
    float avg = sum / 6;
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: getting average!!

    Hello,

    It is advantageous to use STL where the size of the array is bound with the data itself. Here is a sample code to evaluate the average of a vector array.

    Code:
    float average(std::vector<float> array)
    {
    	float sum = 0;
    	for (int count = 0; count < array.size(); count++)
    		sum += array[count];
    	return sum / array.size();
    }
    Regards,
    Pravin.
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: getting average!!

    Quote Originally Posted by Pravin Kumar
    Hello,

    It is advantageous to use STL where the size of the array is bound with the data itself. Here is a sample code to evaluate the average of a vector array.

    Code:
    float average(std::vector<float> array)
    {
    	float sum = 0;
    	for (int count = 0; count < array.size(); count++)
    		sum += array[count];
    	return sum / array.size();
    }
    Regards,
    Pravin.
    I suggest passing the vector by reference to avoid making a useless copy (especially if the vector holds many elements).
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: getting average!!

    Quote Originally Posted by cilu
    Or, with the use of accumulate
    Code:
    #include <algorithm>
    
    float n[6]={1,2,3,4,5,6}
    float sum = std::accumulate(&n[0], &n[5], 0);
    float avg = sum / 6;
    1) accumulate is in <numeric>
    2) the "end()" iterator has to be one past the last element

    Code:
    float sum = std::accumulate(&n[0], &n[6], 0); // or (n,n+6,0.0f)
    float avg = sum / 6;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured