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

    Help! problem with arrays

    A local zoo wants to keep track of how many pounds of food each of its three monkeys eats
    each day during a typical week. Write a program that stores this information in a twodimensional
    3 × 7 array, where each row represents a different monkey and each column
    represents a different day of the week. The program should first have the user input the data
    for each monkey. Then it should create a report that includes the following information:
    • Average amount of food eaten per day by the whole family of monkeys.
    • The least amount of food eaten during the week by any one monkey.
    • The greatest amount of food eaten during the week by any one monkey.
    Input Validation: Do not accept negative numbers for pounds of food eaten.

    This part is the one that I don't understand: • The greatest amount of food eaten during the week by any one monkey.

    This is so far what I have:

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    double totalAverage =0;
    int monkey, day;


    const int NUM_MONKEYS = 3;
    const int NUM_DAYS = 7;
    double average [NUM_MONKEYS][NUM_DAYS];
    double highest, lowest;

    cout << "Enter the amount of food in pounds eaten by the referred monkey.\n";

    for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
    {
    for (day = 0; day < NUM_DAYS; day++)
    {
    cout << "Monkey " << (monkey + 1) << ", "
    << "Day " << (day + 1) << ": ";
    cin >> average [monkey][day];
    while ((average [monkey][day] < 0))
    {
    cout << "Please enter positive values.\n";
    cin >> average [monkey][day];
    }
    }
    cout << endl;
    }

    for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
    {
    for (day =0; day< NUM_DAYS; day++)

    totalAverage += average [monkey][day]/ NUM_DAYS; // I don't get this part on how to get the average per day for each monkey.
    }
    //
    for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
    {
    for (day =0; day< NUM_DAYS; day++)
    {
    if ((average [monkey][day]) > highest)
    highest = average [monkey][day];
    }
    //
    }
    for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
    {
    for (day =0; day< NUM_DAYS; day++)
    {
    if ((average [monkey][day]) ) LOWEST Y PUES NOSE :S
    lowest = average [monkey][day];
    }
    //
    }
    cout << fixed << showpoint << setprecision (2); DOUBLE
    cout << "The average amount is: " << totalAverage << endl;
    cout << "The highest amount is: " << highest << endl;
    cout << "The lowest amount is: " << lowest << endl;


    system ("PAUSE");
    return 0;
    }

  2. #2
    Join Date
    Mar 2004
    Location
    Central Florida
    Posts
    293

    Re: Help! problem with arrays

    Please make sure you code compiles and links before posting it, unless your question is about compiler/linker errors.

    The code would not compile because of spurious text on lines 54 and 59.

    A compiler warning was issued because you used the variable highest before initializing it. This could/should have caused all sorts of unexpected behavior at runtime.

    In the future pelase use CODE BLOCKS ( like below) to preserve your formatting.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        double totalAverage =0;
        int monkey = 0, day = 0;
        int entered_value;
    
    
        const int NUM_MONKEYS = 3;
        const int NUM_DAYS = 7; 
        double average [NUM_MONKEYS][NUM_DAYS];
        double highest = 0, lowest;
        double monkeys[NUM_MONKEYS];
    
        memset(&monkeys[0], 0, sizeof(monkeys));
        memset(&average[0], 0, sizeof(average));
    
        cout << "Enter the amount of food in pounds eaten by the referred monkey.\n";
    
        for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
        {
            for (day = 0; day < NUM_DAYS; day++)
            {
                cout << "Monkey " << (monkey + 1) << ", "
                << "Day " << (day + 1) << ": ";
                cin >> average [monkey][day];
                while ((average [monkey][day] < 0))
                {
                    // Ensure that negative numbers are not accepted.
                    entered_value = -1;
                    while (entered_value < 0)
                    {
                        cout << "Please enter positive values.\n";
                        cin >> entered_value;
                        if (entered_value > -1)
                        {
                            average [monkey][day] = entered_value; 
                        }
                        else
                        {
                            cout << "Negative values are not accepted.\n";
                        }
                    }
                }
            }
            cout << endl;
        }
    
        for (monkey = 0; monkey < NUM_MONKEYS; monkey++)
        {
            // Get the total amount of food eaten during the week
            for (day =0; day< NUM_DAYS; day++)
            {
                // Store the total amount eaten by each monkey
                monkeys[monkey] += average [monkey][day];
            }
        }
    
        // Compute the average amount eaten by all monkeys each day.
        totalAverage = monkeys[0] + monkeys[1] + monkeys[2];
        totalAverage =  totalAverage / NUM_DAYS;
    
        // Compare the amounts eaten by each monkey to determine which one ate the most.
        for (monkey = 0; monkey < NUM_MONKEYS - 1; monkey++)
        {
            if (monkeys[monkey] > monkeys[monkey + 1])
            {
                if (monkeys[monkey] > highest)
                {
                    highest = monkeys[monkey];
                }
            }
            else
            {
                highest = monkeys[monkey + 1];
            }
        }
    
        if (monkeys[0] < monkeys[1] && 
            monkeys[0] < monkeys[2])
        {
            lowest = monkeys[0];
        }
        else if (monkeys[1] < monkeys[2]) 
        {
            lowest = monkeys[1];
        }
        else
        {
            lowest = monkeys[2];
        }
    
        cout << fixed << showpoint << setprecision (2); // DOUBLE
        cout << "The average amount is: " << totalAverage << endl;
        cout << "The highest amount is: " << highest << endl;
        cout << "The lowest amount is: " << lowest << endl; 
    
    
        system ("PAUSE");
        return 0;
    }
    I added afew code comments to explain some of the changes I made. If you step through the code using a debugger, you shoudl be able to understand the logic and see how it works. In my experience you will learn more and have a better understanding that way.
    "Effective teaching is the essence of leadership..."

    "There is no substitute for a carefully thought-out design."

    If you have found this post to be useful, please Rate it.

Tags for this Thread

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