You are on page 1of 4

CS100 Computational Problem Solving

Dr. Maryam Mustafa


Thursday, 14th November 2023
LEAD TAS: HASEEB AND ABDULLAH

Lab #11: Lab Guidelines


1. You are allowed to perform/submit the lab only during lab timings.
2. Copying/sharing code is strictly prohibited. Using any unfair means will lead to immediate disqualification.
3. Make sure you get your lab evaluated from one of the TAs during the lab timings.
4. Use of mobile phones is strictly prohibited.

Lab Grading
Below is the grading table for this lab:

Task # Points

Task 1 30

Task 2 40 + 20

Task 3 30

Total 100
1. Populating and Printing a 4x4 Integer Array:

Write a C++ program that prompts the user to enter values to populate a 4x4 integer array. After
populating the array, display its contents.
Requirements:
● Use a loop to take user input for each element of the 4x4 array.
● Display the populated array using nested loops.
Row Sum Function:
Implement a C++ function named calculateRowSum that takes the 2D array and a row index as
parameters and returns the sum of all elements in that row.
Requirements:
● The function should be declared and defined separately from the main program.
● Use proper function prototypes and parameters.
● Test the function by calling it from the main program with different row indices.

EXAMPLE OUTPUT:

Enter values to populate the 4x4 array:


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

The populated array is:


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Enter the row index to calculate the sum: 2


The sum of elements in row 2 is: 42

2.Rotate 2x2 Matrix 90 Degrees Clockwise:


Task:Implement a C++ function named rotate90Degrees that takes a 2x2 square 2D array and
rotates it 90 degrees clockwise.
Requirements:

● The function should modify the original array in place.


● Do not use additional arrays for rotation.
● Hard coding the rotation for a specific 2x2 matrix is not allowed.
● YOU HAVE TO USE LOOPS
● Test the function by applying it to different 2x2 square 2D arrays in the main program
● Bonus 20 marks if you can make it work for any size of array (3 by 3 or 4 by 4)

Enter values to populate the 2x2 array:


1 2
3 4

Original array:
1 2
3 4

Array after 90-degree clockwise rotation:


3 1
4 2

3. Task: Write a C++ program that takes user input to populate a 4x4 integer matrix. Implement
a sorting function that sorts each row of the matrix independently in ascending order.
Requirements:

● Use a loop to take user input for each element of the 4x4 matrix.
● Implement a sorting function named sortRows that sorts each row independently.
● The sorting function should not sort the entire matrix but should sort each row
separately (if you can sort the whole matrix, you are a wizard! 👏)
● Display the original matrix and the matrix after sorting each row in ascending order.
Enter values to populate the 4x4 matrix:
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13

Original matrix:
4 3 2 1
8 7 6 5
12 11 10 9
16 15 14 13

Matrix after sorting each row:


1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

You might also like