Oop Assign 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

3

1.

// sort 2D matrix row-wise

#include<bits/stdc++.h>

using namespace std;

void sortRowWise(int m[][3],

int r, int c)

// loop for rows of matrix

for (int i = 0; i < r; i++)

// loop for column of matrix

for (int j = 0; j < c; j++)

// loop for comparison and swapping

for (int k = 0; k < c - j - 1; k++)

if (m[i][k] > m[i][k + 1])

// swapping of elements

swap(m[i][k], m[i][k + 1]);

// printing the sorted matrix

for (int i = 0; i < r; i++)

for (int j = 0; j < c; j++)

cout << m[i][j] << " ";

cout << endl;

}
// Driver code

int main()

int m[][3] = {{9, 8, 7},

{7, 3, 0},

Op: 7 8 9

037

136

2. /* C Program to find Sum of Diagonal Elements of a Matrix */

#include<stdio.h>

int main()

int i, j, rows, columns, a[10][10], Sum = 0;

printf("\n Please Enter Number of rows and columns : ");

scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");

for(rows = 0; rows < i; rows++)

for(columns = 0;columns < j;columns++)

scanf("%d", &a[rows][columns]);

for(rows = 0; rows < i; rows++)


{

Sum = Sum + a[rows][rows];

printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );

return 0;

Op:

Please Enter Number of rows and columns : 3 3

Please Enter the Matrix Elements

10 20 30

40 50 60

70 80 90

The Sum of Diagonal Elements of a Matrix = 150

4.
/* C++ program to transpose of a given matrix*/

#include <iostream>

using namespace std;

int main()

int matrix[3][3]={{43,23,55},{65,76,32},{33,51,22}}; //array declaration and values

// change array values here

int temp;

cout<<"Given matrix is-"<<endl;

for(int i=0;i<3;i++){ // print given matrix

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

cout<<matrix[i][j]<<"\t";

cout<<endl;

cout<<endl;

for(int i=0;i<3;i++){ // transpose


for(int j=i;j<3;j++){ //NESTED loop

temp=matrix[i][j]; //swap variables

matrix[i][j]=matrix[j][i];

matrix[j][i]=temp;

cout<<"Transpose of given matrix is-"<<endl;

for(int i=0;i<3;i++){ //print transpose of matrix

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

cout<<matrix[i][j]<<"\t";

cout<<endl;

return 0;

Op:

Given matrix is-

43 23 55

65 76 32

33 51 22

Transpose of given matrix is-

43 65 33

23 76 51

55 32 22

5.

/* C Program for multiplication of two matrix using array */

#include <stdio.h>

int main()

int A[3][3], B[3][3], C[3][3];

int row, col, i, sum;

/*

* Reads elements in first matrix from user


*/

printf("Enter elements in matrix A of size 3x3: \n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

scanf("%d", &A[row][col]);

/*

* Reads elements in second matrix from user

*/

printf("\nEnter elements in matrix B of size 3x3: \n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

scanf("%d", &B[row][col]);

/*

* Multiplies both matrices A*B

*/

for(row=0; row<3; row++)

for(col=0; col<3; col++)

sum = 0;

/*

* Multiplies row of first matrix to column of second matrix

* And stores the sum of product of elements in sum.

*/

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

sum += A[row][i] * B[i][col];

}
C[row][col] = sum;

/*

* Prints the product of matrices

*/

printf("\nProduct of Matrix A * B = \n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

printf("%d ", C[row][col]);

printf("\n");

return 0;

Op:

Enter elements in matrix A of size 3x3:

123

456

789

Enter elements in matrix B of size 3x3:

321

654

987

Product of Matrix A * B =

42 36 30

96 81 66

150 126 102

6.
Program to Add Two Matrices

#include <stdio.h>

int main()

int rowCount, columnCount, i, j;

int firstMatrix[10][10], secondMatrix[10][10], resultMatrix[10][10];

printf("Number of rows of matrices to be added : ");

scanf("%d", &rowCount);

printf("Number of columns matrices to be added : ");

scanf("%d", &columnCount);

printf("Elements of first matrix : \n");

for (i = 0; i < rowCount; i++)

for (j = 0; j < columnCount; j++)

scanf("%d", &firstMatrix[i][j]);

printf("Elements of second matrix : \n");

for (i = 0; i < rowCount; i++)

for (j = 0; j < columnCount; j++)

scanf("%d", &secondMatrix[i][j]);

printf("Sum of entered matrices : \n");

for (i = 0; i < rowCount; i++)

for (j = 0; j < columnCount; j++)

resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];

printf("%d\t",resultMatrix[i][j]);

printf("\n");
}

return 0;

Op:

Number of rows of matrices to be added : 3

Number of columns matrices to be added : 3

Elements of first matrix :

123

456

789

Elements of second matrix :

321

654

987

Sum of entered matrices :

4 4 4

10 10 10

16 16 16

7.

You might also like