Working with Arrays

Hello everyone. I am having a problem with stacking two arrays together.
I am trying to do the following output:

Name Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7
Bonnie 45 33 55 66 77 88 100
Clyde 88 77 66 55 44 33 22
Trigger 44 55 11 66 77 88 99

What I have currently is this:

Name
Bonnie
Clyde
Trigger

45 33 55 66 77 88 100
88 77 66 55 44 33 22
44 55 11 66 77 88 99

I need to add days on top and align numbers to the names.

Please help...

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;
void display(string [], int, double [][NUM_DAYS], int);

int main(){
    string names [NUM_MONKEYS] = {"none","none","none"};
    double food [NUM_MONKEYS][NUM_DAYS] = {{0,0,0,0,0,0,0,},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}};
    
    for (int i = 0; i<NUM_MONKEYS;i++){ //function 1
        string name;
        cout<<"Please enter the name of the monkey #"<<i+1<<endl;
        cin>>name;
        names[i] = name;
    }
    
    
    for (int row = 0; row<NUM_MONKEYS; row++){ //function 2
        for (int colm = 0; colm<NUM_DAYS; colm++){
            cout<<"Monkey #"<<(row+1);
            cout<<", Day #"<<(colm+1)<<" ";
            cin>>food[row][colm];
        }
        cout<<endl;
    }
    
    display(names, NUM_MONKEYS,food,NUM_MONKEYS);
}

void display(string names[],int size,double numbers[][NUM_DAYS],int rows){
    
    cout<<"Names:"<<endl;
    for (int i = 0; i<size;i++){
        cout<<names[i]<<" "<<endl;
        
    }
    
    for (int x = 0; x<rows;x++){
        for (int y = 0; y<NUM_DAYS; y++){
            cout<<setw(4)<<numbers[x][y];
        }
        cout<<endl;
    }
    
}
Last edited on
you'll have to use nested loops
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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int NUM_MONKEYS = 3;
const int NUM_DAYS = 7;
void display(string [], int, double [][NUM_DAYS], int);

int main(){
    string names [NUM_MONKEYS] = {"none","none","none"};
    double food [NUM_MONKEYS][NUM_DAYS] = {{0,0,0,0,0,0,0,},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}};

    for (int i = 0; i<NUM_MONKEYS;i++){ //function 1
        string name;
        cout<<"Please enter the name of the monkey #"<<i+1<<endl;
        cin>>name;
        names[i] = name;
    }


    for (int row = 0; row<NUM_MONKEYS; row++){ //function 2
        for (int colm = 0; colm<NUM_DAYS; colm++){
            cout<<"Monkey #"<<(row+1);
            cout<<", Day #"<<(colm+1)<<" ";
            cin>>food[row][colm];
        }
        cout<<endl;
    }

    display(names, NUM_MONKEYS,food,NUM_MONKEYS);
}

void display(string names[],int size,double numbers[][NUM_DAYS],int rows){

    cout << "Names" << setw(6) << "Day1" << setw(6) << "Day2" << setw(6) << "Day3" << endl;
    for (int i = 0; i<size;i++){
        cout<<names[i] << setw(10);
      //  for (int x = i; x < rows; ++x)
       // {
            for (int y = 0; y < NUM_DAYS; ++y)
            {
                cout<<setw(8)<<numbers[i][y];
            }
            std::cout << "\n";
        //}

    }



}

above program requires some tidying up but should give you the general idea
Topic archived. No new replies allowed.