VB icon

Matrix Table

Email
Submitted on: 10/7/2016 9:20:07 PM
By: Ben128  
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general), Microsoft Visual C++
Views: 2217
 
     Code that shows how to display a matrix table using a 2d array. This will also show the max, min values in the table plus the total sum hope it help someome.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Matrix Table
// Description:Code that shows how to display a matrix table using a 2d array. This will also show the max, min values in the table plus the total sum hope it help someome.
// By: Ben128
//**************************************

// Print out a matrix table find the max,min and total sum of values in table.
// By DreamVB 22:16 07/10/2016
#include <iostream>
using namespace std;
using std::cout;
using std::endl;
int main(int argc, char *argv[]){
	int i = 0;
	int j = 0;
	int max = 0;
	int min = 0;
	int sum = 0;
	const int ROWS = 4;
	const int COLS = 4;
	int matrix[ROWS][COLS] = {
		1, 2, 3, 4,
		5, 9, 4, 3,
		4, 7, 2, 1,
		8, 4, 3, 6
	};
	
	min = matrix[0][0];
	//Print pretty matrix table
	for (i = 0; i < COLS; i++){
		if (i < 1){
			//Add headers
			cout << " " << (char)(65 + i);
		}
		else{
			//Add headers
			cout << " " << (char)(65 + i);
		}
	}
	cout << endl;
	//Print matrix
	for (i = 0; i < ROWS; i++){
		//Add rows
		cout << " " << (char)(65 + i);
		for (j = 0; j < COLS; j++){
			cout << " " << matrix[i][j];
		}
		//Add line break
		cout << endl;
	}
	cout << endl;
	//Find highest number in matrix
	for (i = 0; i < ROWS; i++){
		for (j = 0; j < COLS; j++){
			//Get the sum of matrix
			sum += matrix[i][j];
			if (matrix[i][j]>max){
				max = matrix[i][j];
			}
			if (matrix[i][j] < min){
				min = matrix[i][j];
			}
		}
	}
	cout << "Max value in matrix is : " << max << endl;
	cout << "Min value in matrix is : " << min << endl;
	cout << "Sum of matrix is: " << sum << endl << endl;
	system("pause");
	return 0;
}


Other 50 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.