lab report 8
lab report 8
Technology
Lab Report # 08
Introduction:
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:
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.
• After printing all elements in each row, a newline is printed using endl.
Code:
Result:
Explanation:
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.
Code:
#include <iostream>
void multiplyMatrices(int rowsA, int colsA, int colsB, int matA[][10], int matB[][10], int
result[][10]) {
} } }
int main() {
cout << "Enter the number of columns for matrix A (and rows for matrix B): ";
// Ensure matrix B's row count matches matrix A's column count
rowsB = colsA;
cout << "matA[" << i << "][" << j << "]: ";
cout << "matB[" << i << "][" << j << "]: ";
}
}
return 0;
}
Result:
Explanation:
Function Declaration:
• 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 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:
Code:
#include <iostream>
void addMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int res[][10]) {
}
return;
}
int main() {
// declaring matrices
}}
cout << "matrix2[" << i << "][" << j << "]: ";
}}
//calling function
addMatrices(rows, cols, mat1, mat2, res);
//outputing resultant matrix
}
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:
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 .
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.