You are on page 1of 3

#include <iostream> using namespace std; int main() { //const int ROWS = 3; // const int COLS = 4; //int i, j, val[ROWS][COLS]

= {8,16,9,52,3,15,27,6,14,25,2,10}; // array initialization int i, j, val[3][4]; int largest, smallest, cat1=0, cat2=0, cat3=0, cat4=0; cout<< "Enter 12 values for 3 X4 array :" <<endl; //read array from user for (i = 0; i < 3; i++) for (j = 0; j < 4; j++) cin >> val[i][j]; int total =0; for (i = 0; i < 3; i++) //display and calculate total matrix for (j = 0; j < 4; j++) { cout << val[i][j] << " "; total = total + val[i][j]; } cout << "\n\nTotal of all elements is : " <<total<<endl; for (i = 0; i < 3; i++) //display and calculate total for each row { total =0; //set total to 0 before read first element in the new row for (j = 0; j < 4; j++) total += val[i][j]; // total = total + val[i][j] cout << "\nSum of row " << i+1 << " is " <<total<<endl; } for (j = 0; j < 4; j++) //display and calculate total for each column { total =0; //set total to 0 before read first element in the new colum for (i = 0; i < 3; i++) total += val[i][j]; // total = total + val[i][j] cout << "\nSum of column " << j+1 << " is " <<total<<endl; } for (i = 0; i < 3; i++) //display largest element for each row { largest = val[i][0];//set largest element to the first element in array for (j = 1; j < 4; j++) { if (largest < val[i][j]) largest = val[i][j]; } cout <<"\n The largest element for row " << i+1 << " is "<<largest<<endl; }

for (i = 0; i < 3; i++) //display smallest element for each row { smallest = val[i][0];//set largest element to the first element in array for (j = 1; j < 4; j++) { if (smallest > val[i][j]) smallest = val[i][j]; } cout <<"\n The smallest element for row " << i+1 << " is "<<smallest<<endl; } for (j = 0; j<4; j++) //display largest element for each column { largest = val[0][j];//set largest element to the first element in array for (i = 1; i<3; i++) { if (largest < val[i][j]) largest = val[i][j]; } cout <<"\n The largest element for column " << j+1 << " is "<<largest<<endl; } for (j = 0; j<4; j++) //display smallest element for each column { smallest = val[0][j];//set largest element to the first element in array for (i = 1; i<3; i++) { if (smallest > val[i][j]) smallest = val[i][j]; } cout <<"\n The smallest element for column " << j+1 << " is "<<smallest<<endl; } largest = val[0][0];//set largest element to the first element in array for (i = 0; i < 3; i++) //display largest element for each table { for (j = 1; j < 4; j++) { if (largest < val[i][j]) largest = val[i][j]; } } cout <<"\n The largest element for the table is "<<largest<<endl; smallest = val[0][0];//set largest element to the first element in array for (i = 0; i < 3; i++) //display smallest element for the table { for (j = 1; j < 4; j++) { if (smallest > val[i][j]) smallest = val[i][j]; }

} cout <<"\n The smallest element for the table is "<<smallest<<endl; for (i = 0; i < 3; i++) //determine how many number for each category { for (j = 1; j < 4; j++) { if (val[i][j] <= 25) cat1++; //cat1=cat1+1 else if (val[i][j] > 26 && val[i][j] <= 50) cat2++; else if (val[i][j] > 51 && val[i][j] <= 75) cat3++; else cat4++; } } cout << "\nThe number of values in category 1 is " <<cat1<<endl; cout << "The number of values in category 2 is " <<cat2<<endl; cout << "The number of values in category 3 is " <<cat3<<endl; cout << "The number of values in category 4 is " <<cat4<<endl; return 0; }

You might also like