Page 1 of 1

11 Replies - 509 Views - Last Post: 23 April 2021 - 09:01 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=421071&amp;s=e13f3e3ce143d86162b6a8a36a04bd12&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 lilyjase

Reputation: 1
• Posts: 3
• Joined: 20-April 21

Posted 20 April 2021 - 11:40 PM

```//Question) 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 two-dimensional 3 × 7 array,
//where each row represents a different monkey and each column represents a different day of the week.
//The monkeys are represented by integers 1, 2, and 3; the weekdays are "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday".
//The program should first prompt the user to input the data for each monkey, starting with "Sunday" for monkey #1, then monkeys #2 and #3,
//followed by "Monday" for monkey #1, then monkeys #2 and #3 and so on, through "Saturday".
//The program then creates a report that includes the following information, each properly labeled (see below):
//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 monke

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

int main()
{
// Create a two-dimensional 3x7 array.
const int mkys = 3;
const int days = 7;
int food[mkys][days];
string day[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
double pounds, least, most, sum = 0;
string leastDay, mostDay;
int numMost,numLeast;

for (int row = 0; row < days; row++)
{
for (int col = 0; col < mkys; col++)
{
do
{

cout <<"Enter the food eaten by monkey #" << (col + 1) << " on " << day[row]<< ": ";
cin  >> pounds;

if (pounds < 0)
{
cout << "invalid (negative) food quantity -- re-enter\n";
}

} while (pounds < 0);

food[col][row] = pounds;
sum += pounds;
}

}

// Get least and greatest amount of food eaten by any one monkey.
least = most = food[0][0];
for (int row = 0; row < mkys; ++row)
{
for (int col = 0; col < days; ++col)
{

if (food[row][col] < least)
{
least = food[row][col];
leastDay = day[col];
numLeast = row+1;
}
else if (food[row][col] > most)
{
most = food[row][col];
mostDay = day[col];
numMost = row+1;
}
}
}

cout << fixed << showpoint << setprecision(4);
cout << "Average food consumed daily: "<< sum/7.0<<endl;
cout << "The least daily food consumed was by Monkey #"<< numLeast <<" on "<<leastDay <<".\n";
cout << "The most daily food consumed was by Monkey #"<< numMost <<" on "<< mostDay <<".\n";

return 0;
}

```

This post has been edited by Skydiver: 21 April 2021 - 04:15 AM
Reason for edit:: Put code in code tags. Learn to do this yourself.

Is This A Good Question/Topic? 1

#2 finetunewithhammer

Reputation: 20
• Posts: 101
• Joined: 05-February 21

Posted 21 April 2021 - 03:40 AM

Here is your problem. You increment row and col before using their value in the loop. Their value is always one larger than you expect.
```for (int row = 0; row < mkys; ++row)
{
for (int col = 0; col < days; ++col)

```

Change ++row to row++ and ++col to col++ and it will work fine.

Another thing I'm wondering:
"//The least amount of food eaten during the week by any one monkey."

Does this mean the least amount of food eaten during the entire week, or on one day?
"food eaten during the week" seems to me to be different from "least amount of food eaten on any particular day (by any monkey)".

"//The greatest amount of food eaten during the week by any one monke"

Similarly I take "greatest amount of food eaten during the week" to mean a total of all food eaten during that week by any particular monkey.

I think you should first calculate the total amount of food eaten by each monkey in the entire week. I could be wrong, but that is how I interpret those instruction.

In any case you also need to allow for the possibility that one or more monkeys has eaten the same greatest or lowest amount of food during any particular interval. As it is now, you only print the first monkey in the list with the lowest or highest value.
You should find the lowest value and highest value. Then loop through all the monkey to see which all (may be more than one) that has eaten that particular amount.

This post has been edited by finetunewithhammer: 21 April 2021 - 05:23 AM

#3 Skydiver

• Code herder

Reputation: 7760
• Posts: 25,906
• Joined: 05-May 12

Posted 21 April 2021 - 09:15 AM

finetunewithhammer, on 21 April 2021 - 06:40 AM, said:

Here is your problem. You increment row and col before using their value in the loop. Their value is always one larger than you expect.
```for (int row = 0; row < mkys; ++row)
{
for (int col = 0; col < days; ++col)

```

Change ++row to row++ and ++col to col++ and it will work fine.

Actually, it won't have any logical impact. (It may have a performance impact on some processors where pre-increment is faster than post increment, but I've been told those processors are not as common anymore nowadays.) The reason why it won't have any logical impact is because a for loop that looks like:
```for(int col = 0; col < days; ++col)
{
// do something
}

```

Is actually just syntactic sugar for:
```int col = 0;
while (col < days)
{
// do something

++col;
}

```

I'm quite sure that you'll agree that this is logically the same:
```int col = 0;
while (col < days)
{
// do something

col++;
}

```

#4 lilyjase

Reputation: 1
• Posts: 3
• Joined: 20-April 21

Posted 21 April 2021 - 09:39 AM

finetunewithhammer, on 21 April 2021 - 03:40 AM, said:

Here is your problem. You increment row and col before using their value in the loop. Their value is always one larger than you expect.
```for (int row = 0; row < mkys; ++row)
{
for (int col = 0; col < days; ++col)

```

Change ++row to row++ and ++col to col++ and it will work fine.

Another thing I'm wondering:
"//The least amount of food eaten during the week by any one monkey."

Does this mean the least amount of food eaten during the entire week, or on one day?
"food eaten during the week" seems to me to be different from "least amount of food eaten on any particular day (by any monkey)".

"//The greatest amount of food eaten during the week by any one monke"

Similarly I take "greatest amount of food eaten during the week" to mean a total of all food eaten during that week by any particular monkey.

I think you should first calculate the total amount of food eaten by each monkey in the entire week. I could be wrong, but that is how I interpret those instruction.

In any case you also need to allow for the possibility that one or more monkeys has eaten the same greatest or lowest amount of food during any particular interval. As it is now, you only print the first monkey in the list with the lowest or highest value.
You should find the lowest value and highest value. Then loop through all the monkey to see which all (may be more than one) that has eaten that particular amount.

#5 finetunewithhammer

Reputation: 20
• Posts: 101
• Joined: 05-February 21

Posted 21 April 2021 - 11:20 AM

Yeah, sorry about that, I assumed post increment had an effect in a for loop, which it turns out it has not. I did "test" this, but failed to realize I used different values in the grid, resulting in success..

When food[0][0] has the smallest and/or largest amount the comparisons are never true and no leastDay numLeast and/or mostDay numMost get set.

To fix this, set most to zero and least to INT_MAX or some other unattainable number, or alternatively set leastDay numLeast mostDay numMost to correspond with food[0][0].

#6 #define

• Cannot compute!

Reputation: 1867
• Posts: 6,760
• Joined: 19-February 09

Posted 21 April 2021 - 06:40 PM

Hi, line 48 has a problem.

```48     food[col][row] = pounds;

```

Can you see what it is?

#7 Skydiver

• Code herder

Reputation: 7760
• Posts: 25,906
• Joined: 05-May 12

Posted 21 April 2021 - 08:09 PM

I thought that it was a bug too this morning, but on closer look, I saw that the loop variables are reverse as well where col is the monkeys index, and row is the days index. (see lines 31,33). The next set of nested loops has them correctly used for the appropriate indexing. (See lines 57,59)

But yes, it is confusing when a programmer uses the incorrect variable name. C arrays are row major, and so we would expect to see "row, col", but the OP used "col, row" on the line to mess with our heads.

#8 #define

• Cannot compute!

Reputation: 1867
• Posts: 6,760
• Joined: 19-February 09

Posted 22 April 2021 - 02:50 PM

Skydiver, on 22 April 2021 - 04:09 AM, said:

... but the OP used "col, row" on the line to mess with our heads.

Haha! definitely.

I see that finetunewithhammer has already highlighted (what I think is) the problem.

#9 Skydiver

• Code herder

Reputation: 7760
• Posts: 25,906
• Joined: 05-May 12

Posted 22 April 2021 - 03:51 PM

Yup. I agree. It's the uninitialized variables for the day and monkey that is the issue. His proposal to initialize the thresholds to the reverse extremes to force the setting of the day and monkey variables is the approach I traditionally would use, but the way the OP has written the code, it maybe more appropriate to set the day and monkey values to correspond with the thresholds pointing to the first array element.

#10 finetunewithhammer

Reputation: 20
• Posts: 101
• Joined: 05-February 21

Posted 23 April 2021 - 02:29 AM

@#define did you mean the col row confusion thing or the storing a double in an int thing in:
```48     food[col][row] = pounds;
```

I think both are problematic.

In all fairness the loss of data (accuracy) may be intentional?
A monkey (assuming a chimpanzee) is smaller than a human by a factor of three or four. The amount of food consumed should correlate to the size ratio. If a human consumes on average say 5 pounds of food in a day, a chimpanzee would consume 5/4 pounds. The resulting data would have three plausible values [0, 1, 2] I would think that at least one decimal would be desirable to augment this to possibly the range of 0 to maybe 2.5, giving 25 distinct values, the occurrence of these likely would follow a normal distribution, limiting the usually occurring recorded values to a smaller range. I would also question the ability to track food intake of an individual chimpanzee with the accuracy of 1/10 lb.

I would suggest logging daily food_available food_discarded for the entire group (assuming these chimpanzees are treated humanely and live in a group) calculate the daily food_eaten for the entire group, and divide that according to average data for each chimpanzee collected and calculated from times when only a partial group of the chimpanzees are together, or possibly when individuals are isolated. This known expected average food intake by individual chimpanzees should be modified by observations for each chimpanzee ["no appetite", "moderate_appetite", "normal_appetite", "excessive_appetite"] That would be an interesting model, especially if the model learns by tweaking its own knowledge of expected average food intake by individuals as it collects more data.

I hope that I have shown that loss of accuracy by converting the submitted values to integers could not have been intentional.

#11 #define

• Cannot compute!

Reputation: 1867
• Posts: 6,760
• Joined: 19-February 09

Posted 23 April 2021 - 07:31 PM

I meant the uninitialized variables for the most and least data.

Yes, the array type int maybe a confusion of lilyjase's - monkey being int. The given instructions are not specific.

#12 lilyjase

Reputation: 1
• Posts: 3
• Joined: 20-April 21