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 program should first have the user input the data for each monkey. Then it should create a report that includes the following information: I. Average amount of food eaten per day by the whole family of monkeys. II. The least amount of food eaten during the week by any one monkey. III. 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.
#include <stdio.h>
int main()
{
int array[3][7];
int i, j, least, great, monkey, sum;
float avg;
for (i = 0; i< 3; i++)
for (j = 0; j < 7; j++)
{
printf("Enter pounds of food for monkey %d, day %d : ", i+1, j+1);
scanf("%d", &array[i][j]);
while (array[i][j] < 0)
{
printf("Input Iivalid. Enter positive number: ");
scanf("%d", &array[i][j]);
}
}
printf("\nArray:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 7; j++)
printf("%d ", array[i][j]);
printf("\n");
}
printf("\nAverage amount of food eaten per day:\n");
avg = 0;
for (j = 0; j< 7; j++)
{
sum = 0;
for (i = 0; i < 3; i++)
sum = sum + array[i][j];
avg = (float)sum / 3;
printf("%.2f ", avg);
}
printf("\n\nLeast amount of food eaten during the week:\n");
for (i = 0; i< 3; i++)
{
sum = 0;
for (j = 0; j < 7; j++)
sum = sum + array[i][j];
if (i == 0)
{
least = sum;
monkey = i+1;
}
else if (sum < least)
{
least = sum;
monkey = i+1;
}
}
printf("%d for monkey %d", least, monkey);
printf("\n\nGreatest amount of food eaten during the week:\n");
for (i = 0; i< 3; i++)
{
sum = 0;
for (j = 0; j < 7; j++)
sum = sum + array[i][j];
if (i == 0)
{
great = sum;
monkey = i+1;
}
else if (sum > great)
{
great = sum;
monkey = i+1;
}
}
printf("%d for monkey %d", great, monkey);
}
Comments
Leave a comment