Follow the instructions for starting C++ and viewing the Introductory22.cpp file
ID: 3821828 • Letter: F
Question
Follow the instructions for starting C++ and viewing the Introductory22.cpp file, which is contained in either the Cpp8Chap12 folder. (Depending on your C++ development tool, you may need to open the project/solution file first.) The program should display the contents of the two-dimensional array, column by column and also row by row. Complete the program using a while statement in the outer loops and a for statement in the nested loops. Save and then run the program.
Example program:
//Introductory22.cpp - displays the contents of a
//two-dimensional array, column by column and row by row
//Created/revised by <your name> on <current date>
#include <iostream>
using namespace std;
int main()
{
int nums[2][4] = {{17, 24, 86, 35},
{23, 36, 10, 12}};
//display column by column
cout << endl;
//display row by row
return 0;
} //end of main function
Explanation / Answer
C++ CODE:
//Introductory22.cpp - displays the contents of a
//two-dimensional array, column by column and row by row
//Created/revised by <your name> on <current date>
#include <iostream>
using namespace std;
int main()
{
int nums[2][4] = {{17, 24, 86, 35},
{23, 36, 10, 12}};
cout << "array : 17, 24, 86, 35 23, 36, 10, 12 " ;
//display column by column
int i=0;
cout << "column by column : ";
while(i<4){
for(int j=0;j<2;j++){
cout << nums[j][i] << " ";
}
cout << " ";
i++;
}
//display row by row
i=0;
cout << "row by row : ";
while(i<2){
for(int j=0;j<4;j++){
cout << nums[i][j] << " ";
}
cout << " ";
i++;
}
return 0;
} //end of main function
Output:
array :
17, 24, 86, 35
23, 36, 10, 12
column by column :
17 23
24 36
86 10
35 12
row by row :
17 24 86 35
23 36 10 12