Docsity

programming lectures, Schemes and Mind Maps of C programming

Toggle sidebar
bg1
11/10/2022
1
2D ARRAYS LECTURE
1
2
Contents
Two-Dimensional Array
Declaring Two-Dimensional Array
Initializing Two-Dimensional Array
Accessing elements of Two-Dimensional
Array
Inputting elements of Two-Dimensional
Array
Outputting elements of Two-Dimensional
Array
Operations on two Dimensional arrays
Matrices and operations performed
Department of Computer
Sciences
Bahria University, Islamabad
CSC 113 – Computer
Programming
7. 2D Arrays
Slide: 2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download programming lectures and more Schemes and Mind Maps C programming in PDF only on Docsity!

2D ARRAYS LECTURE

Contents

Two-Dimensional Array

Declaring Two-Dimensional Array

Initializing Two-Dimensional Array

Accessing elements of Two-Dimensional

Array

Inputting elements of Two-Dimensional

Array

Outputting elements of Two-Dimensional

Array

Operations on two Dimensional arrays

Matrices and operations performed

Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 2

What is a Two Dimensional Array ?

Example : int

list[4][4];

ROW

S

Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 4 A two dimensional array is the arrangement of elements in rows and columns. It has two indices, one for row and other for column. First index represents the number of rows, second index represents the number of columns.

Two Dimensional Arrays Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 3 NSCOLUM

Two-dimensional Arrays Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 7 Accessing Array Components

Syntax:

arrayName [intRowIndex] [intColIndex];

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

  1. 2D Arrays Slide: 8

Accessing Array Components (cont'd.) Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 9 Multi-dimensional Arrays int matrix[3] [5] matrix[1] Department of Computer^ [3] Sciences Bahria University, Islamabad CSC 113 – Computer Programming
  2. 2D Arrays Slide: 10

Initialization

To initialize the entire matrix to 0:

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

  1. 2D Arrays Slide: 13 Initialization

To initialize the matrix with user input: Row wise

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

  1. 2D Arrays Slide: 14

Initialization

To initialize the matrix with random numbers:

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

  1. 2D Arrays Slide: 15 Print

To output the components of matrix:

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

  1. 2D Arrays Slide: 16

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

  1. 2D Arrays Slide: 19 Operations on Matrices

A matrix is a two dimensional array.

It is basically an application of 2 D arrays

Some of the matrix operations can be carried on a square matrix

only

A matrix having same number of rows and columns is a square

matrix.

Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 20

Matrix : Diagonal Elements Elements of alternate diagonal Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 21 Sum = A[0][2]+A[1] [1]+A[2][0] = 7+8+5= Sum = A[0][0]+A[1] [1]+A[2][2] = 2+8+15= Elements of main diagonal 2 4 7 2 4 7 6 8 9 6 8 9 5 13 15 5 13 15

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

  1. 2D Arrays Slide: 22

Matrix: Upper and Lower Triangleelements Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 25 Sum = A[1][0]+A[2] [0]+A[2][1] = 6+5+13= Sum = A[0][1]+A[0] [2]+A[1][2] = 4+7+9= Elements of lower triangle Elements of upper triangle 2 4 7 2 4 7 6 8 9 6 8 9 5 13 15 5 13 15

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

  1. 2D Arrays Slide: 26

Matrix : Addition Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 28 Here : M=P=Rand N=Q=S To add two matrices: Prerequistes: the number of rows of 1st matrix = no of rows of 2nd matrix the number of columns of 1st matrix = no of columns of 2 nd matrix Formula : C[i][j]=A[i][j]+B[i][j]

Matrix Arithematic (Addition and Subtraction) Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming

  1. 2D Arrays Slide: 27

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

  1. 2D Arrays Slide: 31

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

  1. 2D Arrays Slide: 32

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

  1. 2D Arrays Slide: 34