Vous êtes sur la page 1sur 2

Date:

Assignment No: 02
Title: Write a program to implement matrix operations using arrays & perform following matrix
operations: a. b. c. d. Transpose with pointers to arrays Addition with pointers to arrays Multiplication of without pointers to arrays Saddle point without pointers to arrays

Theory:
Two-Dimensional Arrays: A Two Dimensional arrays can be consider as a table consisting of rows and column. int a[3][4], declares an integer array of three rows and four columns. Row index starts from 0 and will go upto 2. Similarly, column index starts from 0 and will go upto 3.
Col 0 Col 1 Col 2 Col 3 Row 0 Row 1 Row 2

Pointers: Pointers are variables that hold the memory address of other variables. int *P, declares pointer to an integer. P=&X, declares address of X can be store in P using &operator. Dynamic Memory Allocation: It is possible to allocate memory to a program while executing. The malloc( ) and calloc( ) functions of C are used to dynamically allocate memory. It returns a pointer to a block of n bytes of memory allocated during runtime. Allocating dynamic memory for storing 100 integer numbers using malloc( ) int *P; P = ( int * ) malloc (100 * sizeof(int));
Address of block of memory is casted to address of integer Number of bytes required for storing 100 integers

a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3]

Allocating dynamic memory for storing 2 dimensional integer array int ** a; //for storing address of an array of pointers a = (int **) malloc (m * sizeof (int *)); //each element of array a[] is of the type int* for i = 0 to m rows a[i] = (int *) malloc (n* sizeof(int));

Time and Space Complexity: Time complexity 1. Addition :- O(n) 2. Multiplication:- O(n) 3. Transpose :- O(n) Space complexity No extra storage is required for matrix operations.

Algorithm:
1. Start 2. Initialize variables, arrays & pointers 3. Accept matrix from user 4. Display menu 5. Take choice from user 6. if (ch! = 1) gotostep 8 7. Perform Transpose of a matrix and then print the resultant matrix 8. if (ch! = 2) gotostep 10 9. Perform addition of matrices and then print the resultant matrix 10. if (ch! = 3) gotostep 12 11. Perform multiplication of matrices and then print the resultant matrix 12. if (ch! = 4) gotostep 14 13. Perform saddle function of matrices and then print the resultant matrix 14. If you want to continue then goto step 4 15. Stop

Conclusion:
Thus we have study how to declare a two dimensional array, read elements of array and print the elements of the array using static and dynamic method. We have also implemented the following operations on matrices: a. Transpose b. Addition c. Multiplication d. Saddle Point

Vous aimerez peut-être aussi