// // Lab Project 5 Name__________________________________ // #include <iostream
ID: 3776688 • Letter: #
Question
// // Lab Project 5 Name__________________________________ // #include <iostream> #include <iomanip> using namespace std; // create a function to print the contents of array x row wise // example Row 1: 9 8 7 // Row 2: 4 5 6 // Row 3: 3 2 1 // use nested for loops. void print_arrayx( { // use a for loop for printing for (int row = } // create a function to total and print the column values in array x void total_x( ) { } int main() { int x [3][3] = { {9,8,7},{4,5,6},{3,2,1} }; // call the function to print array x print_arrayx( ) // call the function to total the column values in x total_x( ) return 0; }
Explanation / Answer
//
// Lab Project 5 Name__________________________________
//
#include <iostream>
#include <iomanip>
using namespace std;
// create a function to print the contents of array x row wise
// example Row 1: 9 8 7
// Row 2: 4 5 6
// Row 3: 3 2 1
// use nested for loops.
void print_arrayx(int x[3][3])
{
// use a for loop for printing
for (int row = 0;row<3;row++)
{
for(int col=0;col<3;col++)
{
cout<<x[row][col]<<" ";
}
cout<<" ";
}
}
// create a function to total and print the column values in array x
void total_x( int x[3][3] )
{
for(int col=0;col<3;col++)
{
int total=0;
cout<<"Column "<<col<<" : ";
for(int row=0;row<3;row++)
{
cout<<x[row][col]<<" ";
total+=x[row][col];
}
cout<<"Total: "<<total<<endl;
}
}
int main()
{
int x [3][3] = { {9,8,7},{4,5,6},{3,2,1} };
// call the function to print array x
print_arrayx( x );
// call the function to total the column values in x
total_x( x );
return 0;
}
Sample Output:
9 8 7
4 5 6
3 2 1
Column 0 : 9 4 3 Total: 16
Column 1 : 8 5 2 Total: 15
Column 2 : 7 6 1 Total: 14
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.