Help finding the average of reach row in a 2D array function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodecSmash49
    New Member
    • Oct 2008
    • 8

    Help finding the average of reach row in a 2D array function

    im currently working on a assignment, where you have 3 monkeys that are feed for 7 days. You have to use a 2D array to gather the info. display like a chart, and find the average eaten each day.

    This is the code I have so far. I only need help with displaying it as a chart and finding the average. Please ^.^


    Code:
     
    #include <cstdlib>
    #include <iostream>
     
    using namespace std;
     
    void getFood(double arrayPound[3][7]);
    void displayFood(double arrayPound[3][7]);
    void sortLeast(double arrayPound[3][7]);
    void sortGreatest(double arrayPound[3][7]);
    void sortAverage(double arrayPound[3][7]);
     
    int main(int argc, char *argv[])
    {
    double arrayPound[3][7];
     
    getFood(arrayPound);
    cout << endl;
     
    displayFood(arrayPound);
    cout << endl;
     
    sortAverage(arrayPound)
    cout << endl;
     
    sortLeast(arrayPound);
    sortGreatest(arrayPound);
     
     
     
    system("PAUSE");
    return EXIT_SUCCESS;
    }
     
    void getFood(double arrayPound[3][7])
    {
    double pound = 0;
     
    cout << "Food for the monkeys." << endl;
    cout << endl;
     
    for(int i = 1; i < 4; i++)
    {
    for(int u = 1; u < 8; u++)
    {
    cout << "Please enter the amount feed to monkey " << i << " on day " << u << " :";
    cin >> pound;
     
    while((cin == 0) || (pound < 0))
    {
    cin.clear();
    cin.ignore(10000, '\n');
    cout << "Please enter the amount feed to the monkey: ";
    cin >> pound;
    }
    arrayPound[i][u] = pound;
    }
    }
     
    }
     
    void displayFood(double arrayPound[3][7])
    {
    double pound;
     
    cout << "Total food eaten across all 7 days." << endl;
    cout << endl;
     
    for(int i = 1; i < 4; i++)
    {
    for(int u = 1; u < 8; u++)
    {
    cout << "The amount eaten by monkey " << i << " on day number " << u << " was: " << arrayPound[i][u] << endl;
    }
    }
    }
     
    void sortLeast(double arrayPound[3][7])
    {
    double leastVal = arrayPound[0][0];
     
    for(int i = 1; i < 4; i++)
    {
    for(int u = 1; u < 8; u++)
    {
    if(arrayPound[i][u] < leastVal)
    {
    leastVal = arrayPound[i][u];
    }
    }
     
    }
    cout << "The least amount was eaten: " << leastVal << endl;
    }
     
    void sortGreatest(double arrayPound[3][7])
    {
    double greatestVal = arrayPound[0][0];
     
    for(int i = 1; i < 4; i++)
    {
    for(int u = 1; u < 8; u++)
    {
    if(arrayPound[i][u] > greatestVal)
    {
    greatestVal = arrayPound[i][u];
    }
    }
    }
    cout << "The greatest amount was eaten: " << greatestVal << endl;
    }
     
    void sortAverage(double arrayPound[3][7])
    {
    double dailyAvg;
     
    for(int u = 1; u < 8; u++)
    {
    ( arrayPound[1][u] + arrayPound[2][u] + arrayPound[3][u] ) / 3 = dailyAvg;
    }
     
    cout << "The average eaten by the monkeys on day " << u << " was: " << dailyAvg << endl;
     
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What kind of chart are you to display?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I haven't analysed your code to get some sense of the underlying algarithm (please use code tags) but I question the need for 5 2d arrays as an over complication. Have you given any thought to using just one array splitting monkeys and days and performing the required data manipulation by successive traverses?

      Comment

      Working...