Sie sind auf Seite 1von 2

BLG221E DATA STRUCTURES (CRN 22440)

HOMEWORK-1
Assignment Date : 18.02.2015
Due Date
: 04.03.2015 at 18:00
Duration
: 2 weeks
You are given a text file which contains integer data of a matrix. The first line in file should contain only
two numbers representing the sizes (number of rows and number of columns) of matrix, then the data of
matrix are given.
6

Example
MATRIX.TXT
file

190
70
120
210
170
160

30
60
90
140
80
220

20
180
110
150
10
230

40
50
100
240
200
130

Write the functions whose prototypes and descriptions are given below.
int ** Allocate (int N, int M);
Input arguments: Sizes (N and M) of matrix.
Returned output: Pointer to pointer that represents the allocated matrix.
Function dynamically allocates a matrix and returns a pointer to pointer to it.
void Print (int ** P, int N, int M);
Input arguments: Matrix P, and sizes of the matrix (N and M).
Function prints the elements of matrix P on screen.
void Split (int ** P, int N, int M, int split_method, int ** &P1, int ** &P2 );
Input arguments: Matrix P, sizes of matrix (N,M), and the split method code (1: horizontal, 2: vertical).
Output arguments: Matrices P1 and P2 which are results of half-splitting.
Function creates two matrices (P1 and P2). The P1 and P2 should be allocated thru Allocate() calls.
Note: If N or M is an odd number, then the size of P1 should be 1 above the size of P2.
Example matrix:
Matrix

Horizontal split
First half
Second half

Vertical split
First
half

Second
half

void Sort (int ** &P, int N, int M, int sorting_direction);


Input arguments: : Matrix P, sizes of matrix (N,M), the sorting direction code (1: Row-wise, 2: Column-wise)
Output argument: Matrix P (it is used as both input and output argument, so the matrix is modified).
Function sorts the matrix in ascending order, by applying the specified sorting direction.
Example matrix:
45 20 50 35 40
25 30 10 55 15

Row-wise sort
10 15 20 25 30
35 40 45 50 55

Page:1/2

Column-wise sort
10 20 30 40 50
15 25 35 45 55

MAIN PROGRAM TASKS


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

Using fscanf, read data file and initialize the X matrix which should be allocated thru Allocate() call.
Call Print() for X matrix.
Call Split() for X matrix, with horizontal method code, putting results into S1 and S2 matrices.
Call Split() for X matrix, with vertical method code, putting results into S3 and S4 matrices.
Call Sort() for S1 matrix, with row-wise direction code, then Call Print() for S1 matrix.
Call Sort() for S2 matrix, with column-wise direction code, then Call Print() for S2 matrix.
Call Sort() for S3 matrix, with row-wise direction code, then Call Print() for S3 matrix.
Call Sort() for S4 matrix, with column-wise direction code, then Call Print() for S4 matrix.
EXAMPLE SCREEN OUTPUT
X matrix:
190
30
20
40
70
60 180
50
120
90 110 100
210 140 150 240
170
80
10 200
160 220 230 130
---------------------S1 matrix:
20
30
40
50
60
70
90 100
110 120 180 190
---------------------S2 matrix:
10 140 170 220
80 150 200 230
130 160 210 240
---------------------S3 matrix:
30
60
70
80
90 120
140 160
170 190
210 220
---------------------S4 matrix:
10 130
20 150
40 180
50 200
100 230
110 240
---------------------Press any key to continue . . .

Page:2/2