Hey guys. We have a homework assignment where we have to create a 1-D array to hold the units of food for 7 monkeys. All the data in the file is in the format of Monkey(#), day, and units eaten (#)
example: 4, 6, 3 (Monkey #4 ate on the 6th day 3 units.
All of the values in the file are out of order so the array must assign them. We have to use the tools we have been taught as this is an introductory class.
We have learned:
1-D arrays,
functions (basic ones that do not need extra include headers)
while, for, do/while, if, and switch statements, logical operators, strings, characters.
I have had help with making this a 2-D array, but cannot figure out the logic needed to make it a 1-D. I have written some of the code so far, but I am stuck on how to continue. I was thinking an if statement so once it hits monkey #7 for example, it adds up the units and continues to accumulate the sum as it keeps seeing 7? It seems like it would be a crap ton of if statements though. Thanks guys and I apologize, this is a newb class so I am very new, please don't be mean.
Instructions:
for each monkey, the average food eaten per day
the average food for all the monkeys
the monkey that ate the most food for the week
the monkey that ate the least food for the week
Use functions and a 1-D array of 7 or 8 in length. (There are 7 monkeys)
example of file structure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/************************************/
//function declarations
void printHeader();
void printMonkey();
float daily_Average();
float average_All();
int mostFood();
int leastFood();
/************************************/
void printHeader() //printing the header for the days
{
int i;
cout<<" ";
for(i=1;i<8;i++)
{
cout<<setw(10)<<"Day"<<i;
}
cout<<setw(10)<<" Average";
cout<<endl;
}
void printMonkey() //printing the monkey header on the left side
{
int i;
for(i=1;i<8;i++)
{
cout<<"Monkey"<<i<<':'<<endl;
}
}
float daily_Average(int arr[], int size) //average each monkey eats each day
{
int i;
for(i=0;i<8;i++)
{
//left blank for now
}
}
float average_All(int arr[], int size) //average all monkeys eat each day
{
int i;
for(i=0;i<8;i++)
{
//left blank for now
}
}
int most_Food(int arr[], int size) //most amount of food eaten by the monkeys
{
int i;
int most=-1;
for(i=0;i<8;i++)
{
if(arr[i]>most)
{
most=arr[i];
return most;
}
}
}
int least_Food(int arr[], int size) //least amount of food eaten by the monkeys
{
int i;
int least=999;
for(i=0;i<8;i++)
{
if(arr[i]<least)
least=arr[i];
return least;
}
}
int main() //beginning of main file
{
infile.open("C:\\data\\monkeyFile.txt"); //opening the file
if(!infile)
{
cout<<"Cannot open file, please try again.";
return 0;
}
int monkeyRecord[49]; //declaring the array
int monkey;
int day;
int unit;
int i;
int sum;
float dailyAverage;
int mostFood;
int leastFood;
for(i=0;i<49;i++) //writing the file to an array
{
infile>>monkey>>day>>unit;
monkeyRecord[i]=monkey;
}
printHeader();
printMonkey();
dailyAverage=daily_Average(monkeyRecord, 8);
average_All(monkeyRecord, 8);
mostFood=most_Food(monkeyRecord, 8);
leastFood=least_Food(monkeyRecord, 8);
|
I just have a table output for now.
Day1 Day2 Day3 Day4 Day5 Day6 Day7 Average
Monkey1:
Monkey2:
Monkey3:
Monkey4:
Monkey5:
Monkey6:
Monkey7:
|