Writing 3 types of variables to a 1-D array

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

7 2 3
6 4 1
6 3 3
7 1 2
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:

Last edited on
You can’t use a struct?
(That’s what I would use, but if not we (you) may need clarification from your professor.)
No we cant use that yet. We haven't learned it yet. That's what makes these so challenging cause it would be easier with a 2D array or something else, but it has to be a 1D
Either you do a struct liek this :
1
2
3
4
struct monkey{
    int id;
    int food[7]; //seven day in a week, each cell represent a day
};


Or a 2D-array :

1
2
3
4
5
6
int monkey[7][7];
/**
There is seven lines of seven cell in this array, representing
7 days for each of your monkey.
For example : monkey[3][0] represent the monday food quantity of your 4th monkey.
**/


I would better use the second way (btw you can convert it in 1D-array).
I think you're misinterpreting the instructions. I don't think you need to pay attention to the day numbers at all. The average food per day means if a monkey ate 70 units over the week then he ate an average of 10 units per day.

And you certainly don't need to output a table.
Yes, he said we may need to ignore the day. We cant use a 2D array or struct. I emailed him today and asked and he said it has to be a 1D array. That's what has me so confused.
How would you write a 2d into a 1d??
@CodeNovice01, easy:

1
2
3
4
5
const int Rows = 5, Cols = 7;
int a[Rows * Cols] {};

// To access row r, column c:
a[r * Cols + c] = 42;

That's what has me so confused.

What's confusing? If you ignore the day then it's a 1-dimensional problem.
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
#include <iostream>
#include <fstream>

const int NUM_MONKEYS = 7 ;
const int ARRAY_SIZE = NUM_MONKEYS + 1 ; // as position zero is unused
const int NUM_DAYS = 7 ;

int main()
{
    // array to hold amount of food eaten
    // food eaten by monkey #n is accumulated into total_food[n]
    int total_food[ARRAY_SIZE] {} ; // initialise to all zeroes

    std::ifstream file( "monkey_business.txt" ) ; // open the file for input

    if( file.is_open() )
    {
        int monkey_number ;
        int day_number ;
        int food_eaten ;

        // for each 3-tuple of values read in from the input file
        while( file >> monkey_number >> day_number >> food_eaten )
        {
            // if the 3-tuple holds valid values
            if( monkey_number > 0 && monkey_number <= NUM_MONKEYS && // valid monkey number
                day_number > 0 && day_number <= NUM_DAYS && // valid day number
                food_eaten > 0 )
            {
                // update the total_food array entry for this monkey
                total_food[monkey_number] += food_eaten ;
            }
        }

        // the array now contains the total food eaten by each monkey
        // process the array to compute and print the required output
        // (pass it to functions to compute and print average etc.)
    }
}

Last edited on
Apologies JLBorges, but your code has made me even more confused on this topic.

I do have a quick general question about 2d. Can one take a 2d and make it a 1d, by say IF the 2nd d is a const int, can the array be taken down to a 1d?
Hey guys, I ended up figuring it out by reading the monkeys, day, and unit, but ignoring everything but the unit and then using functions to perform the calculations.

1
2
3
4
5
6
 int totalFood[8]={0};
 for(i=0;i<49;i++)   //reading the values into the array 
    {
        infile>>monkey>>day>>unit;
        totalFood[monkey]+=unit;
    }

this printed the daily amount for each monkey and sorted it correctly. I had to initialize my array to 0 to get it to work. Not sure why.
I had to initialize my array to 0 to get it to work. Not sure why.

Really? You can't understand why increasing the value of a number is meaningless, if you don't know what value that number was in the first place?
Topic archived. No new replies allowed.