[go: up one dir, main page]

0% found this document useful (0 votes)
21 views28 pages

lab report 8

jgcjgcjmf

Uploaded by

Zoraiz Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views28 pages

lab report 8

jgcjgcjmf

Uploaded by

Zoraiz Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

National University of Science and

Technology

School of Mechanical and Manufacturing


Engineering

Lab Report # 08

CS-114 Fundamentals of Programming

Course Instructor: Dr. Shahbaz Khan

Introduction:

Name: Zoraiz Abbas

CMS ID: 454752

Date: 06-08-2024
Task 1
Create or declare four different integer 2D arrays of ten elements each:

1.
Initialize the first 2D array using an initializer list with ten non-zero integer values of your
choice.

Code:

Result:
Explanation:
• In this code we initialized an array1[2][5] and assigned values to it.
• After that we have to display the array on console and we uses nested for loops to
output the array . we uses nested loop because it is 2d array.

2.
Initialize the second 2D array using a loop with values that are squares of the index of each
element.

Code:
Result:

Explanation:

• A 2D array array2 with dimensions [2][5] is declared.

• A nested loop is used to fill the array:


o The outer loop iterates over the rows (i ranges from 0 to 1).The inner loop
iterates over the columns (j ranges from 0 to 4).

o Each element of the array is assigned a value equal to the product of the row
index i and the column index j, i.e., array2[i][j] = j * j.

• Another nested loop is used to print the 2D array to the console:


o Again, the outer loop iterates over the rows (i ranges from 0 to 1). The inner
loop iterates over the columns (j ranges from 0 to 4). Each element of the array
is printed with a space between elements.

• After printing all elements in each row, a newline is printed using endl.

• At last zero is return to the main fuction after execution .


3.
Initialize the third 2D array with values input by the user using a loop.

Code:

Result:
Explanation:

• In this we declared the array3[2][5] having 2 rows and 5 coloumns .


• Then we ge the values input from user using nested loop because it is a 2d array .
• After we stored the values we displayed it on console again using nested loop.
Task 2 :
Write a function Compute that should compute the values of an array with n rows and m
columns specified by the user.

Code:

Result:
Explanation:
• In this code first of all we have initialized two variables m and n and taken input rows
and coloums form user.
• After that we have declared the array having size we have taken input .
• After that we have used nested loop to take input integer for array .
• At last we have again used nested loop to display the 2d array on console.

Task 3:

Write a function Compute that should compute the values of nth row specified by the user.

Code:
Result:
Explanation:
• In this code two integers for rows and coloumns are declared and initialized from 0.
And we have taken input of rows and coloumn for array .
• An array[10][10] is declared and after that row is get input from user in which the
values have to be inputed .
• After that row number is check that it is valid or not .(row <n)
• After that user defined function is called and at last we have used nested loops to
desplay the array.

computeRow(int a[][10] , int row, int m):


• This function is declared which is of void type in which an array and two integers are
passed .
• After that row number is checked that it is valid or not its should be row>0.
• At last we run a loop in which we fixed the row number index and run loop from 0 to
coloumn-1. And get input from user .
Task 4:

Write a C++ function that multiplies two matrices using arrays.

Code:

#include <iostream>

using namespace std;

// Function to multiply two matrices

void multiplyMatrices(int rowsA, int colsA, int colsB, int matA[][10], int matB[][10], int
result[][10]) {

// Perform matrix multiplication

for (int i = 0; i < rowsA; ++i) {

for (int j = 0; j < colsB; ++j) {

for (int k = 0; k < colsA; ++k) {

result[i][j] += matA[i][k] * matB[k][j];

} } }

int main() {

int rowsA, colsA, rowsB, colsB;

// Input the dimensions of the matrices

cout << "Enter the number of rows for matrix A: ";


cin >> rowsA;

cout << "Enter the number of columns for matrix A (and rows for matrix B): ";

cin >> colsA;

cout << "Enter the number of columns for matrix B: ";

cin >> colsB;

// Ensure matrix B's row count matches matrix A's column count

rowsB = colsA;

// Initialize the matrices

int matA[10][10], matB[10][10], result[10][10];

// Input elements for the first matrix (A)

cout << "Enter elements of matrix A:" << endl;

for (int i = 0; i < rowsA; ++i) {

for (int j = 0; j < colsA; ++j) {

cout << "matA[" << i << "][" << j << "]: ";

cin >> matA[i][j];

// Input elements for the second matrix (B)

cout << "Enter elements of matrix B:" << endl;

for (int i = 0; i < rowsB; ++i) {

for (int j = 0; j < colsB; ++j) {

cout << "matB[" << i << "][" << j << "]: ";

cin >> matB[i][j];

}
}

// Multiply the matrices

multiplyMatrices(rowsA, colsA, colsB, matA, matB, result);

// Display the result

cout << "Resultant Matrix after multiplication:" << endl;

for (int i = 0; i < rowsA; ++i) {

for (int j = 0; j < colsB; ++j) {

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

cout << endl;

return 0;

}
Result:
Explanation:

Function Declaration:

• The function multiplyMatrices is defined to multiply two matrices. It takes as


input rows and columns for the matrices along with the matrices matA and
matB, and stores the result in the result matrix.

• The function uses a triple nested for loop to perform matrix multiplication. For
each element in the resulting matrix, it multiplies corresponding elements from
matA and matB and accumulates the sum.

• The function performs matrix multiplication using a triple nested for loop:

o The outer loop iterates over the rows of matrix A.

o The middle loop iterates over the columns of matrix B.

o The innermost loop iterates over the columns of matrix A (which is the
same as the rows of matrix B).
o For each element in the resulting matrix, it computes the sum of the
products of the corresponding elements in matA and matB, storing the
result in result[i][j].

Main Function:

• The main function starts by declaring four integer variables: rowsA, colsA, rowsB,
and colsB. These variables are used to store the dimensions of the two matrices.

• The user is prompted to input the number of rows and columns for matrix A and
matrix B.

• Three 2D arrays are declared with fixed dimensions of 10x10: matA, matB, and
result.

• matA stores the elements of the first matrix, matB stores the elements of the
second matrix, and result will store the product of matA and matB.

• The program prompts the user to enter elements for the first matrix (matA).A
nested for loop is used to iterate through each element of the matrix. The outer
loop iterates over the rows. The inner loop iterates over the columns.

• Similarly, the program prompts the user to enter elements for the second matrix

• After the multiplication is complete, the program prints the resultant matrix
(result).

• The main function ends with return 0;, indicating successful execution.

Task 5:

Write a C++ function to add two matrices using multidimensional arrays.

Code:
#include <iostream>

using namespace std;

// Function to add two matrices

void addMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int res[][10]) {

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

for (int j = 0; j < cols; ++j) {

res[i][j] = mat1[i][j] + mat2[i][j];

}
return;
}

int main() {

int rows=0, cols=0;

//getting input of rows and coloumn

cout << "Enter the number of rows (max 10): ";

cin >> rows;


cout << "Enter the number of columns (max 10): ";

cin >> cols;

// declaring matrices

int mat1[10][10], mat2[10][10], res[10][10];

//getting input of first matrix

cout << "Enter elements of the first matrix:" << endl;


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

for (int j = 0; j < cols; ++j) {


cout << "matrix1[" << i << "][" << j << "]: ";
cin >> mat1[i][j];

}}

//getting input of second matrix

cout << "Enter elements of the second matrix:" << endl;


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

for (int j = 0; j < cols; ++j) {

cout << "matrix2[" << i << "][" << j << "]: ";

cin >> mat2[i][j];

}}

//calling function
addMatrices(rows, cols, mat1, mat2, res);
//outputing resultant matrix

cout << "Resultant Matrix after addition:" << endl;

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

for (int j = 0; j < cols; ++j) {

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

cout << endl;}


return 0;

}
Result:
Explanation:
Function Declaration:

• The function addMatrices is defined to add two matrices. It takes the number of
rows and columns as input, along with three matrices: mat1, mat2, and res.

• The matrices mat1 and mat2 are added element-wise, and the result is stored in
the matrix res. This is done using nested loops.

Main Function:

• The main function starts by declaring two integer variables, rows and cols, to
store the number of rows and columns in the matrices. And takes input from
user using cin command.
• Three 2D arrays (mat1, mat2, and res) are declared with fixed dimensions of
10x10 to store the elements of the matrices.

• The program asks the user to input elements for the first matrix (mat1). A nested
for loop is used to iterate through each element of the matrix. The outer loop
iterates over the rows. The inner loop iterates over the columns.

• Similarly, the program asks the user to input elements for the second matrix
(mat2).

• The addMatrices function is called with the parameters rows, cols, mat1, mat2,
and res.

• The function adds the corresponding elements of mat1 and mat2 and stores the
results in res.

• A nested for loop is used to iterate through each element of the resultant matrix
and print it.

• The outer loop iterates over the rows, and the inner loop iterates over the
columns, printing each element followed by a space. After each row, a new line
is printed.

• The main function ends with return 0;, indicating successful execution.

Task 6:

Write a program in C++ to find the transpose of a matrix.

Code:
Result:

Explanation:
• Two integer variables, rows and cols, are declared to store the number of rows
and columns for the matrix.

• The program prompts the user to enter the number of rows and columns using
cin >> rows; and cin >> cols; .

• A 2D array matrix is declared with dimensions rows x cols to store the elements
of the matrix.

• Another 2D array transpose is declared with dimensions cols x rows to store the
transposed matrix.

• A nested for loop is used to take input for the elements of the matrix. The outer
loop iterates over the rows.The inner loop iterates over the columns.
• Another nested for loop is used to transpose the matrix. The outer loop iterates
over the rows, and the inner loop iterates over the columns. The element at
position [i][j] in the original matrix is assigned to the position [j][i] in the
transposed matrix.
• A nested for loop is used to display the original matrix.
• A similar nested for loop is used to display the transposed matrix.

Task 7:

Write a function in C++ to find out the highest number in a 2-Dimensional array

Code:
Result:

Explanation:
• In this code we have declared two integers rows and columns and have taken input
from user using cin command. We have also declared an array of 10 by 10 .
• We have used nested loops to take input for array integers. Outer loop controls the
rows and inner loop control the columns.
• After that user defined function is called and parameters are passed into it .

int highest_number( int arr[][10] , int rows , int cols):


• The function is of int type. It will return integer.
• In this we have declared and integer named max_no and assigned a value of arr[0][0].
• A nested loop is run and which will check if any number is greater than arr[0][0]. If
any number is greater than first number than it will assigned that graeter number to
max_no. Outer loop control rows and inner loop control columns.
• At last after the execution of nested loops it will return the max_no as return value.

Task 8:

Write a function that calculates the sum of all elements in the three-dimensional array
and prints the result

Code:
Result:
Explanation:
• In this code we have declared three integers rows and columns and depth and have
taken input from user using cin command. We have also declared an array of 10 by 10
by 10 mat1[10][10][10].
• We have used three nested loops to take input for array integers. 1st loop controls the
depth , 2nd loop control the rows and last and innermost loop control the columns.
• After that user defined function is called and parameters are passed into it and stored
its result in an integer named result.
• At last result is displayed.

Int sumofmat( int rows, int cols, int d , int mat[10][10][10]:


• The function is of int type. It will return integer.
• In this we have declared and integer named sum and initialized it from zero.
• The three nested loops are run and in the innermost loop the sum of value is modified
by adding the every integer of 3d array. 1st loop controls the depth , 2nd loop control
the rows and last and innermost loop control the columns.
• At last after the execution of nested loops it will return the sum as return value.

You might also like