You are on page 1of 5

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 09
(Summer 2022)

Course: Computer Programming Lab Date: _______________


Course Code: CSL-113 Max Marks: 40
Faculty’s Name: Mr. Shoaib Khan

Name: _____________________ Enroll No: ___________________ Class:


______________

Objective(s):
To Understand about:
1. Apply Arrays in C++
2. Use of Two Dimensional Array
Enrollment Number: ____________________________
Sample Code :
// CP Lab 09.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
//Declaration of 2-D Integer Array
int myArray[2][2];
//Initialization of 2-D Integer Array
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
myArray[i][j] = i + j;
}
//Accessing a 2-D Integer Array
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
// cout<<"myArray["<<i<<"]["<<j<<"] = "<<myArray[i][j]<<" ";
cout << endl;
}
int duplicateMyArray[2][2];
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
duplicateMyArray[i][j] = myArray[i][j];
}
int resultantMyArray[2][2];
//Sum of two 2-D array
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++)
resultantMyArray[i][j] = myArray[i][j] + duplicateMyArray[i][j];
}
//Task 1: Declare a 2-D array named myArray of size 2x2. Add the corrosponding
entries to
//the duplicate of myArray and save the result in the third array.
//Task 2: Delcare two Matrices A & B having 3x4 and 4x3 order respectively.
Intialize
//the values of these two matrics as sum of their position i.e rowIndex +
columnIndex
//. Multiply these two matrices and save the result in third Matrix C.

//Declare & Initialize the sizes of Matrices A, B & C


const int rowsA = 2, colA = 2, rowsB = 2, colB = 3;
//Delcare Matrices A, B & C.
int A[rowsA][colA], B[rowsB][colB], C[rowsA][colB];
//Initialize the entries of Matrix B.
for (int i = 0; i < rowsA; i++){
for (int j = 0; j < colA; j++){
A[i][j] = i + j + 1;
}
}
//Display the entries of Matrix A.
for (int i = 0; i < rowsA; i++){
for (int j = 0; j < colA; j++){
cout << A[i][j] << " ";
}cout << endl;
}
//Initialize the entries of Matrix B.
for (int i = 0; i < rowsB; i++){
for (int j = 0; j < colB; j++){
B[i][j] = i + j + 1;
}
}
//Display the entries of Matrix B.
for (int i = 0; i < rowsB; i++){
Page 2 of 5
Enrollment Number: ____________________________
for (int j = 0; j < colB; j++){
cout << B[i][j] << " ";
}cout << endl;
}
//Initialize the Matrix C with entries as 0.
for (int i = 0; i < rowsA; i++){
for (int j = 0; j < colB; j++){
C[i][j] = 0;
}
}
//if no. of colA = no of rowsB then perform multiplication
if (colA == rowsB){
for (int i = 0; i < rowsA; i++){
for (int j = 0; j < colB; j++){
for (int k = 0; k < colA; ++k)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
else {
cout << "Two matric A & B are not compatible for multiplication" <<
endl;
}
//Display the entries of Matrix C.
for (int i = 0; i < rowsA; i++){
for (int j = 0; j < colB; j++){
cout << C[i][j] << " ";
}
cout << endl;
}
return 0;
}

----------------------------- For Instructor Use Only -----------------------------------


Lab Grading Sheet :
Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 10
2. 10
3. 10
4. 10
Total 40 Signature:
Note : Attempt all tasks and get them checked by your Lab. Instructor.

**Provide solution of each task and screenshot of output at the end of that task**
Task 1
Write a program to perform matrix multiplication of m x n matrix. Given the condition if
number of rows of first matrix equal to the number of rows of the second matrix.

Page 3 of 5
Enrollment Number: ____________________________
< Solution and output of Task 1 >

Task 2
Write a C++ Program that computes the sum of two matrices. Each matrix is of 2 rows and 2
columns and will be created from user input.

Output of the program is as follows:

Enter [0][0] of Matrix A: 2


Enter [0][1] of matrix A: 3
Enter [1][0] of matrix A: 4
Enter [1][1] of matrix A: 5

Enter [0][0] of Matrix B: 6


Enter [0][1] of matrix B: 7
Enter [1][0] of matrix B: 8
Enter [1][1] of matrix B: 9

A= 2 3 + B= 6 7 = C= 8 10
4 5 8 9 12 14
< Solution and output of Task 2 >

Task 3
Write a C++ program that read 12 integer values from user, store values in Matrix of 4 X 3.
Create another Matrix of 4 X 3, divide each element of Matrix1 by five, and store the result in
the Matrix2.
Print Matrix A, with heading shown, correctly spaced.
Print Matrix B, with heading shown, correctly spaced.
Your Program should display output as follows:
Sample Output:
===================================
Matrix A – Original
===================================
18 42 13
38 76 84
24 81 49
12 48 26

Page 4 of 5
Enrollment Number: ____________________________

===================================
Matrix A – Divided by 5
===================================
4 8 3
8 15 17
5 16 10
2 10 5
< Solution and output of Task 3 >

Task 4
Write a C++ program to calculate the result of 8 students of a class and each student takes 5
courses. Following are the rules for result:
1. Each section has 8 students.
2. Each student takes 5 courses.
Marks for each subject of every student must be taken from the user.
Calculate the result of every student:
1. Obtained marks (Sum of all courses marks). Max. marks of each subject are 100
2. Percentage
Hint: Use Three Dimensional Array
< Solution and output of Task 4>

87 75 76
55 67 80

Page 5 of 5

You might also like