Answer to Question #201218 in C++ for khosa

Question #201218

Assuming the following declarations: const int NUMROWS = 3; const int NUMCOLS = 4; int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10}; Which of the following loops correctly outputs each element of the array in row order? 1. for (i = 0; i < NUMROWS; i++) { for (j = 0; j < NUMCOLS; j++) cout << setw(4) << val[i][j]; cout << endl; } 2. for (i = 0; i < NUMROWS; i++) { cout << setw(4) << val[i][j]; cout << endl; } 3. for (i = 0; i < NUMCOLS; i++) { cout << setw(4) << val[i][j]; cout << endl; } 4. for (i = 0; i < NUMROWS*NUMCOLS; i++) { cout << setw(4) << val[i][j]; cout << endl; }


1
Expert's answer
2021-05-31T09:16:23-0400

The correct loop is 1

#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
    const int NUMROWS = 3; 
    const int NUMCOLS = 4; 
    int val[NUMROWS][NUMCOLS] = {8,16,9,52,3,15,27,6,14,25,2,10};
    //1.
    int i;
    int j;
    for (i = 0; i < NUMROWS; i++) { 
        for (j = 0; j < NUMCOLS; j++) 
            cout << setw(4) << val[i][j]; cout << endl; 
        
    }
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog