### 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!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` `````` 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<
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

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include 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.