Programming Assignment 5 (Deadline: May 11th Monday 23:59)

Problem

Write a C code that implements the following set of function prototypes.

void print(int row,int column, int *matrix);
/*
row= number of rows of the matrix
column= number of columns of the matrix
matrix is an integer pointer to the base address of the matrix
*/

This function is supposed to take in the number of rows, the number of columns and the base address of the matrix and print the whole matrix to the console in the proper matrix form (rows printed line by line, elements in each row are separated by a space.).
Matrix elements (which are of type int) are stored contigously on the memory area pointed by the base pointer. Additionally, they are stored row by row. In other words, elements of upper rows are stored on lower addresses. Moreover, lefter elements are stored on lower addresses within each row. There is no memory gap between the contigously placed matrix elements.
Use pointer arithmetic to access the elements of the matrix.

int *matrixmult(int row1, int column1, int *matrix1, int row2, int column2, int *matrix2);
/*
row1= number of rows of the first matrix
column1= number of columns of the first matrix
matrix1 is an integer pointer to the base address of the first matrix
row2= number of rows of the second matrix
column2= number of columns of the second matrix
matrix2 is an integer pointer to the base address of the second matrix
*/

This function is supposed to multiply two matrices : matrix1 having number of rows = row1 and number of columns = column1 and matrix2 having number of rows = row2 and number of columns = column2.
By including int *matrix1, we are passing the base address of the 2-D matrix1.
By including int *matrix2, we are passing the base address of the 2-D matrix2.
Use pointer arithmetic to access the elements in the matrices.
The function returns an integer pointer, containing the base address of the resultant matrix.
You should dynamically allocate the storage for the resultant matrix using 'malloc'
Your print function implementation will be used to print the resultant matrix.

void sort(int *array, int size); 
/*
array is an integer pointer to the base address of the array
size is the length of the array which is equal to the number of elements in the array.
*/

This function is supposed to sort a given array,having length as size,in the ascending order of the values of its elements.
Use pointer arithmetic to access the elements in the array.
Your print function implementation will be used to print the sorted array by passing the pointer to the base address of the array with row=1 and column= size of the array.

void swap(int *a, int *b);
/*
a is an integer pointer containing the address of the first element.
b is an integer pointer containing the address of the second element.
*/

This function is supposed to swap the values of the integers pointed by a and b. This function will be handy while implementing the sort function.

Your code will be tested by linking your code with different programs.
Thus, your code should not contain a main function.
You can test your code by linking it with your own programs.
(Pass more than one source file to gcc to do this.)

Sample Program

#include<stdio.h>

void print(int row,int column, int *matrix);
void sort(int *array, int size);
void swap(int *a, int *b);
int *matrixmult(int row1, int column1, int *matrix1, int row2, int column2, int *matrix2);

int main()
{
int a[][2]={{1,2},{3,4}};
int b[][3]={{2,3,5},{4,5,9}};
int num[6]={7,3,5,10,9,6};
int j=15;
int k=90;
int *result;
result=matrixmult(2,2,a,2,3,b);
print(2,3,result);
sort(num,6);
print(1,6,num);
swap(&j,&k)
printf("%d\n",j);
printf("%d",k);
return 0;
}

Sample Output

10 13 23
22 29 51
3 5 6 7 9 10
90
15

Assumptions and Constraints

The matrix multiplication will always be possible i.e. number of columns of matrix1=number of rows of matrix2.
You may safely assume that the memory areas passed to a single function do not overlap.
The size of the parameter of the sort function will not exceed 50.
The number of rows and the number of columns for the matrices will not exceed 50.
The values of the elements of the resultant matrix for the matrix multiplication task will remain in the int data range

Hints

Good programmers make use of sizeof() whenever convenient.
You may use additional functions.
You may use gdb to find out which statements cause a Segmentation fault and why.

Submission

Submit your source file (.c) through the submission system.