Sie sind auf Seite 1von 4

Object Oriented Paradigm

Lab 01
Topic(s): Arrays and Pointers

Question No. 01
Read a matrix from a file named “matrix.txt”. The size of the matrix will be given in the first row.
Create a blank matrix with a single column. Find the minimum number from each row of the first
matrix and store it into the corresponding row of the second matrix. Now, create another matrix
with a single row and store the maximum number from each column of the first matrix into the
corresponding column. Write both the matrices into another file named “minMax.txt”.

matrix.txt

5 5

1 2 5 6 0
6 5 8 2 3
4 0 5 2 6
4 2 58 5 21
2 1 5 2 3

minMax.txt

0
2
0
2
1

6 5 58 6 21

Question No. 02
Read two matrices given in two different files named “matrix1.txt” and “martix2.txt” into 2D
arrays of appropriate sizes. Write a C++ program that updates matrix1 as shown below.

Page 1 of 4
matrix1.txt

0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

matrix2.txt

1 2 3 4
5 6 7 8
6 1 2 3

After updating matrix1, matrix2 is located at the top-right corner of matrix1

output.txt

0 0 1 2 3 4
0 0 5 6 7 8
0 0 6 1 2 3
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Question No. 03

Read a matrix from a file named “matrix.txt”. Create a blank miniMatrix of a single column. Find
the minimum numbers from each row of the first matrix and store it into the corresponding row
of the miniMatrix. Now, use this miniMatrix to sort the two matrices. After sorting, the row
having the minimum number will be row 1 and so forth. Write both matrices into another file
named “minimum.txt”.

Page 2 of 4
matrix

11 2 5 6 0
6 5 8 12 3
4 8 5 2 6
4 22 58 5 21
2 1 5 2 3

miniMatrix

0
3
2
4
1

After Sorting:

matrix

11 2 5 6 0
2 1 5 2 3
4 8 5 2 6
6 5 8 12 3
4 22 58 5 21

miniMatrix

0
1
2
3
4

Page 3 of 4
Question No. 04
You are given a file that contains a list of integers. Read these integers into a 2D array. Your matrix
should resemble a square as much as possible. Any remaining indices should be filled with -1.
Create two separate functions; one for ascending sort and one for descending sort. Sort the odd
numbered rows in ascending order and even numbered rows in descending order. Write the
resultant matrix into another file named “output.txt”.

Note: You should choose the size of your matrix very carefully. For example, if you are given 80
numbers; instead of creating an 8x10 or 10x8 matrix, you should create a 9x9 matrix. Fill any
remaining indices with -1.

Page 4 of 4

Das könnte Ihnen auch gefallen