223k views
1 vote
Write and debug this program in Visual C++, and upload for grading. Declare a 2D array, 4 rows by 3 columns, initialized to all zeros. The program should prompt for and read a list of integers into the 2-dimensional array It should stop reading in new values when the user types a non-numeric or at 12 numbers, whichever comes first.

Then the program should print out:
• the average of the values entered
• the maximum value entered
• the array as a 4x3 matrix with spacing of 3 spaces per column (Hint: setw(3))
• the array transposed, as a 3x4 matrix (just print it in transposed order, do not create an actual 3x4 array. To print it transposed, just copy the nested loop that printed it as 4x3, and make the outer loop the columns and inner loop the rows)
Example program run the program prints everything below except the user input, whch is 1 2 3 4 5 6 74
Enter up to 12 integers, separated by spaces, terminated with a non-numeric: 1 2 3 4 5 6.79
Average - 4
Maximum
4x3 matrix
1 2 3
4 5 6
7 0 0
0 0 0
3x4 matrix
1 4 7 0
2 5 0 0
3 6 0 0

User CYAN CEVI
by
4.8k points

1 Answer

3 votes

Answer:

See explaination

Step-by-step explanation:

#include <iostream>

#include <iomanip>

using namespace std;

int main( )

{

//2D array with 4 rows and 3 columns and initializing it with zeros

int arr[4][3]={0};

int i,j;

//for taking input

int x;

//for counting number of numeric inputs

int cnt=0;

//for storing maximum value

int maxi=0;

//for storing sum of all numeric values

float sum=0;

cout<<"Enter up to 12 intergers,seperated by spaces, terminated with a non-numeric:\\";

//taking 12 inputs

for(i=0;i<4;i++){

for(j=0;j<3;j++){

//taking input in x

cin>>x;

//if input is non-numeric then break

if(x==0){

break;

}

//counting the number numeric inputs

cnt++;

//calculating sum of all numeric values

sum+=x;

//finding maximum among all numeric values

maxi=max(maxi,x);

//storing the input in 2D array

arr[i][j]=x;

}

//if input is non-numeric then break

if(x==0){

break;

}

}

//calculating average of all numeric values

float avg=sum/cnt;

//displaying average and maximum

cout<<"Average = "<<avg<<"\\";

cout<<"Maximum = "<<maxi<<"\\";

//displaying 4x3 matrix

cout<<"4x3 matrix:\\";

for(i=0;i<4;i++){

for(j=0;j<3;j++){

cout<<arr[i][j]<<setw(3);

}

cout<<"\\";

}

//displaying transpose of matrix

cout<<"3x4 matrix:\\";

for(i=0;i<3;i++){

for(j=0;j<4;j++){

cout<<arr[j][i]<<setw(3);

}

cout<<"\\";

}

return 0;

}

User Bperson
by
5.2k points