Array Table issue

Hi, this is my first time here and I am learning arrays in c++ and I am supposed to create an array which will display a table with the Monkey column (Monkey 1, 2, and 3) and the days of the week along with values inputted by the user. I have it down I think however I cannot seem to figure out how to display 1, 2, and 3 under the Monkey header, along with trying to align the values accordingly. If anyone could help explaining that would be amazing and greatly appreciated!

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
 const int days = 7; 
string dayNames[] = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"}; 
const int monkeyNum = 3;               
float foodPounds [monkeyNum][days];       
float totalFood = 0;
double average; 
int monkey;                               
int day;


int main() 
{


   for (monkey = 0; monkey < monkeyNum; monkey++)
   {
           for (day = 0; day < days; day++)
               {
               cout << "Enter the food eaten by Monkey #" << (monkey + 1) <<" on "<< dayNames[day]<<": ";
               cin >> foodPounds[monkey][day];
              
               }
   }


    double maxFood = foodPounds[0][0];
   double minFood = foodPounds[0][0];
   int imin = 0, imax= 0, jmin = 0, jmax = 0;

   for (monkey = 0; monkey < monkeyNum; monkey++)   {      

       for (day = 0; day < days; day++)
       {
        
           totalFood = totalFood + foodPounds[monkey][day];
       
           if (foodPounds[monkey][day] > maxFood)
               {
                   imax = monkey;
                   jmax = day;
                   maxFood = foodPounds[monkey][day];
               }  

           
           if (foodPounds[monkey][day] < minFood)
               {
                   minFood = foodPounds[monkey][day];
                   imin = monkey;
                   jmin = day;
               }

       }
   }
cout << fixed << showpoint << setprecision(2);
cout << setw(5) << "Monkey" << setw(7) << "Sun." << setw(7) << "Mon." << setw(7) << "Tues." << setw(7) << "Wed." << setw(7) << "Thurs." << setw(7) << "Fri." << setw(7) << "Sat\n " ;


for(int i = 0; i < monkeyNum; i++)
{
    for(int j = 0; j < days; j++)
    {
		cout << setw(5) << foodPounds[i][j];
    }
    cout<<endl;
}

   average = (totalFood) / (monkeyNum*days);

   cout << "\nAverage pounds of food consumed daily: : " << average << endl;
   cout << "The least daily pounds of food consumed was by monkey "<< (imin+1)<<" on "<<dayNames[jmin]<<": "<<minFood <<endl;
   cout << "The most daily pounds of food consumed was by monkey " << (imax+1)<<" on "<<dayNames[jmax]<<": "<<maxFood<<endl;

   return 0;
}
To align fields in the output, use the manipulator std:setw to set the width of each field.
http://en.cppreference.com/w/cpp/io/manip/setw

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

int main()
{
    const int ndays = 7 ;
    const int nmonkeys = 3 ;

    const std::string day_names[ndays] { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" } ;

    double food[nmonkeys][ndays] = // in actual code, accept from user input
    {
        { 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8 },
        { 3.4, 4.5, 5.6, 6.7, 7.8, 1.2, 2.3 },
        { 6.7, 7.8, 1.2, 2.3, 3.4, 4.5, 5.6 }
    };

    const int fld_width = 10 ;
    const auto fw = std::setw(fld_width) ; // http://www.stroustrup.com/C++11FAQ.html#auto

    // print the header
    std::cout << fw << "monkey" ;
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( const std::string& dn : day_names ) std::cout << fw << dn ;
    std::cout << '\n' ;
    for( int i = 0 ; i < (ndays+1) ; ++i ) std::cout << fw << "----" ;
    std::cout << '\n' ;

    // print the details
    std::cout << std::fixed << std::setprecision(2) ;
    for( int i = 0 ; i < nmonkeys ; ++i )
    {
        std::cout << fw << '#' + std::to_string(i+1) ; // monkey number
        for( double amt : food[i] ) std::cout << fw << amt ; // food amt for each day
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/b7903b8db4271f87
Topic archived. No new replies allowed.