Reputation: 21
Currently working on a project where I have two arrays (one 1-D and another 2-D). One contains names and another contains a set of numbers. I'm working on a function that finds the lowest value in the table and outputs it. However, I need to attribute the name of the monkey to the amount of food he has eaten. So "Monkey 1 has eaten the least amount of food only eating 11 pounds on Day 1". Below is the function to find the lowest value which is working fine, however I'm not sure how to implement the monkey's name (without just typing it in) and the day of the week. If the rest of my code is needed please let me know.
void leastAmt(string names[], int food[][NUM_DAYS])
{
int lowest = food[0][0];
for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
{
for (int day = 0; day < NUM_DAYS; day++)
{
if (food[monkey][day] < lowest)
lowest = food[monkey][day];
}
}
cout << "Least amount of food: " << lowest << endl;
}
Upvotes: 0
Views: 72
Reputation: 13954
Store least_eating_monkey in an array, storing monkey names for each day.
int leastEatingMonkey[NUM_DAYS];
for (int day = 0; day < NUM_DAYS; day++)
{
lowest = INT_MAX;
for (int monkey = 0; monkey < NUM_MONKEYS; monkey++)
{
if (food[monkey][day] < lowest){
leastEatingMonkey[day] = monkey;
lowest = food[monkey][day];
}
}
}
for (int day = 0; day < NUM_DAYS; day++)
{
cout << "monkey " << names[day] << " ate least amount of food on day " << day << endl;
}
Upvotes: 0
Reputation: 1814
create two variables for storing monkey and money values.
int lowest_monkey, least_day;
Whenever lowest value is modified update these values with current values of monkey and day variables. Replace the last line with the following
cout << "Monkey " << lowest_monkey << " has eaten least amount of food only eating " << lowest << " on " << least_day << endl;
Upvotes: 1