










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Contents
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
What is a Two Dimensional Array ?
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Two Dimensional Arrays Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Two-dimensional Arrays Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
where intRowIndex and intColIndex are expressions yielding nonnegative integer values, and specify the row and column position Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Accessing Array Components (cont'd.) Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Initialization
const int rowSize = 7; const int colSize = 6; int matrix[rowSize][colSize]; for (int row = 0; row < rowSize; row+ +) { for (int col = 0; col < colSize; col+ +) { matrix[row][col] = 0; } } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
input const int rowSize = 7; const int colSize = 6; int matrix[rowSize][colSize]; for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { cout << "Enter value for Row " << row << "and Column " << col; cin>>matrix[row][col]; } cout << endl; } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Initialization
srand(time(0)); int randLimit = 100; const int rowSize = 7; const int colSize = 6; int matrix[rowSize][colSize]; for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { matrix[row][col] = 1 + rand() % randLimit; } } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { cout<<setw(5)<<matrix[row][col]<<" "; } cout << endl; } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Sum of col elements : Program int sum; for (int col = 0; col < colSize; col++) { sum = 0; for (int row = 0; row < rowSize; row++) { sum += matrix[row][col]; } cout <<"Sum of the Column : "<<col+1<<" "<<sum<< endl; } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Matrix : Diagonal Elements Elements of alternate diagonal Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Sum of diagonal elements if (rowSize == colSize) { int sumDaigonal = 0; int sumDaigonalAlernative = 0; for (int col = 0; col < colSize; col++) { for (int row = 0; row < rowSize; row++) { if(col==row) sumDaigonal += matrix[row][col]; if(row + col == rowSize - 1) sumDaigonalAlernative += matrix[row][col]; } } cout << "Sum of the Daigonal is : " << sumDaigonal << endl; cout << "Sum of the Alternative Daigonal is : " << sumDaigonalAlernative << endl; } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Matrix: Upper and Lower Triangleelements Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Sum of upper and lower diagonal elements if (rowSize == colSize) { int sumUpperTriangle = 0; int sumLowerTriangle = 0; for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { if (row < col) sumUpperTriangle += matrix[row][col]; else if (row > col) sumLowerTriangle += matrix[row][col]; } } cout << "Sum of the Upper Triangle is : " << sumUpperTriangle << endl; cout << "Sum of the Lower Triangle is : " << sumLowerTriangle << endl; } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Matrix : Addition Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Matrix Arithematic (Addition and Subtraction) Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Matrix Transpose int rowSize=3; int colSize=2; int matrix[rowSize][colSize]; int matrixTranspose[colSize][rowSize]; for (int col = 0; col < colSize; col++) { for (int row = 0; row < rowSize; row++) { matrixTranspose[col][row] = matrix[row][col]; } } Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Largest Element in a Matrix int largest= matrix[0][0]; for (int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { if (largest < matrix[row][col]) largest = matrix[row] [col]; } } cout << "The largest element in Matrix is = " << largest << endl; Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
Largest Element in Each Row and Each Column int largest; for (int row = 0; row < rowSize; row++) { largest = matrix[row][0]; for (int col = 0; col < colSize; col++) { if (largest < matrix[row][col]) largest = matrix[row][col]; } cout << "The largest element in row " << row + 1 << " is = " << largest << endl; } for (int col = 0; col < colSize; col++) { largest = matrix[0][col]; for (int row = 0; row < rowSize; row++) { if (largest < matrix[row][col]) largest = matrix[row][col]; } cout << "The largest element in column " << col + 1 << " is = " << largest << endl; } Department of Computer SciencesCSC 113 – Computer Programming Bahria University, Islamabad 7. 2D Arrays Slide: 33 Matrix Multiplication Same Dimensions Different Dimensions Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming