1

I am doing a a problem using Multi-dimensional Arrays. I call the function in main but I get this Error message. This is the Original Problem I have:

A local zoo wants to keep track of how many pounds of food each of its three tigers eats each day during a typical week. Write a program that stores this information in a two-dimensional 3 x 7 array, where each row represents a different tiger and each column represents a different day of the week. The program should first have the user input the data for each tiger. Then it should create a report that includes the following information:

  • Average amount of food eaten by each of the tigers during the week.
  • Average amount of food eaten by all the tigers per day for each day.
  • The least amount of food eaten during the week by any tiger.
  • The greatest amount of food eaten during the week by any tiger.

the code:

#include <iostream>
using namespace std;

const int TIGERS = 3;
const int DAYS = 7;
int food[TIGERS][DAYS];
void inputArray(int food[TIGERS][DAYS]);
float AvgTig(float);
float Least(float);
float most(float);

int main()
{
    float maximum;
    float minimum;
    float total = 0.0f;
    float average = 0.0f;
    float result = 0;

    inputArray(food);
    maximum = food[0][0];
    minimum = food[0][0];
    cout << "\n \n";
    AvgTig(result);
    cout << "\n \n";
    Least(minimum);
    cout << "\n \n";
    most(maximum);
    cout << "\n \n";

    system("PAUSE");

    return 0;

    //end program
}

void inputArray(int food[TIGERS][DAYS])
{
    int total = 0;
    for (int tig = 0; tig < TIGERS; tig++) {
        for (int day = 0; day < DAYS; day++) {
            cout << "TIGER " << (day + 1) << ", day " << (day + 1) << ": ";
            cin >> food[tig][day];
        }
        cout << endl;
    }
}

float AvgTig(float output)
{
    int total = 0;
    for (int i = 0; i < TIGERS; i++) {
        for (int day = 0; day < DAYS; day++) {
            total += food[i][day];
        }
        cout << endl;
    }
    output = total / (TIGERS * DAYS);
    cout << "Average food for Tigers in the days is :" << output << " ";
    return output;
}

float Least(float minimum)
{
    for (int least = 0; least < TIGERS; least++) {
        for (int day = 0; day < DAYS; day++) {
            if (food[least][day] < minimum)
                minimum = food[least][day];
        }
    }
    cout << "Minimum food eaten: " << minimum << " ";
    return minimum;
}

float most(float maximum)
{
    for (int most = 0; most < TIGERS; most++) {
        for (int day = 0; day < DAYS; day++) {
            if (food[most][day] > maximum)
                maximum = food[most][day];
        }
    }
    cout << " Maximum number of food is: " << maximum << " ";
    return maximum;
}
mch
  • 9,424
  • 2
  • 28
  • 42
Jos
  • 65
  • 7
  • You just have to remove the index when you call it. change the line => `inputArray(food[TIGERS][DAYS]);` by `inputArray(food);`. – hiral2 Apr 29 '20 at 01:40
  • I fixed the error and added the functions but the input array function operates in a rather Bizarre way, I updated the code in original question – Jos Apr 29 '20 at 06:05
  • What rather bizarre way? Say what actually happens and what you expect to happen instead. Don't just leave us to figure it out. – john Apr 29 '20 at 06:11
  • Do yourself a favor and format your code according to convention. Most IDEs will do auto formatting based on language. As it is, this is unreadable. If it is only this way in your post, please be generous to people trying to help out and take the time to make sure the formatting is right in your post as well. – dmedine Apr 29 '20 at 06:26
  • Another tip is not to modify an argument in a function as you do with ```maximum``` in ```most```. This can cause all kinds of haywire. And it is also a bad idea to name a variable the same name as the function in whose scope it's in. You have also done this in ```most``` with ```int most```. – dmedine Apr 29 '20 at 06:31
  • I fixed all my mistakes, but when the program runs, it would sometimes break in the InputArray functon, it will fill 2 tigers out of 3 and will not fill the third tiger – Jos Apr 29 '20 at 07:07

1 Answers1

0

On this line:

inputArray(food[TIGERS][DAYS]);

in main, you are calling inputArray with a single int, and not the entire food array. In fact, even reading this position in food is UB.

You need to call the function like this:

inputArray(food);
cigien
  • 57,834
  • 11
  • 73
  • 112
  • I updated my code in the original question but the inputArray function operates in a rather bizarre way – Jos Apr 29 '20 at 06:02
  • @JohnnyJoestar That's likely because you don't flush the output before waiting for input. So it's sitting invisbly in the output buffer. – David Schwartz Apr 29 '20 at 06:48
  • Hello Sir, Unfortunately I don't think I understand what you mean because I don't know what flush means. – Jos Apr 29 '20 at 07:08
  • ```cout << "TIGER " << (day + 1) << ", day " << (day + 1) << ": ";``` should be ```cout << "TIGER " << (tig + 1) << ", day " << (day + 1) << ": ";``` – dmedine Apr 30 '20 at 01:56
  • https://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer – dmedine Apr 30 '20 at 01:58