How can I find the average in the ARRAYS?

This is a discussion on How can I find the average in the ARRAYS? within the C++ Programming forums, part of the General Programming Boards category; I have already wrote the main funstion but I cannot figure out what to put in the other function to ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    How can I find the average in the ARRAYS?

    I have already wrote the main funstion but I cannot figure out what to put in the other function to find the average of the three arrays?#include <iostream>
    #include <iomanip>
    using namespace std;

    double avg1( double [], int);
    double avg2( double [], int);
    double avg3( double [], int);
    double computeAverage(int [], double []);

    int main()
    {
    double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
    double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
    double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

    cout << fixed << setprecision(2);

    double avg1 = computeAverage (x, sizeof (x) / sizeof (x[0]) );
    double avg2 = computeAverage (y, sizeof (x) / sizeof (x[0]));

    cout << "Average of numbers in array x = " << avg1 << '\n';
    cout << "Average of numbers in array y = " << avg2 << '\n';
    cout << "Average of numbers in array z = "
    << ComputeAverage (z, sizeof (z) / sizeof (z[0])) << '\n';
    return 0;
    }


    double computeAverage ()
    {

    }


    Any help would be appreciated.

    Thanks,
    Nate2430

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    double computeAverage(double array[], int size)
    {
    double total = 0;
    for (int i = 0; i <= size - 1; i++)
    total += array[i];
    return total / size;
    }


    By the way:
    sizeof (x) / sizeof (x[0] is equal to sizeof(double). You don't need to do this unnecessary calculation.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Sorry. Omit that By the way part. I saw it wrongly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2009, 01:50 PM
  2. What's wrong with my Average Array program?
    By special1zed in forum C Programming
    Replies: 12
    Last Post: 03-15-2009, 11:01 PM
  3. Replies: 3
    Last Post: 02-22-2009, 01:54 PM
  4. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

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