Ex 2-D Array

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1 #include<stdio.

h>
2 /*
3 *#define is a preprocessor directievs
4 *The C preprocessor is a macro preprocessor (allows you to define macros)
5 that transforms your program before it is compiled. These transformations
6 can be inclusion of header file, macro expansions etc.
7 *All preprocessing directives begins with a # symbol. For example,
8 */
9 #define ROW 50
10 #define COL 50
11 /*
12 *This program is demonstrate the use of a 2-d Array called matrix
13 *When a 2-d array is passed to a function, size of column have to specify
14 *column represents the bound of each row
15 *we can see a 2-d array as a collection of multiple 1-d arrays
16 *when we pass a 2-d array to a function, along with the array we
17 have to pass the size of the array as no. of rows and columns
18 */
19 void input(int MAT[][COL],int row,int col){
20 for(int i=0;i<row;i++){
21 for(int j=0;j<col;j++){
22 printf("Enter a number MAT[%d][%d]::",i,j);
23 /*
24 *we need to pass the index of row and column
25 *here i is the index of row, j is the index of column
26 *when we pass the individual element, we have to pass
27 the address of the element we are passing in scanf
28 for eaxmple &MAT[i][j].
29 */
30 scanf("%d",&MAT[i][j]);
31 }
32 }
33 }
34 /*
35 Description -> this function will print a 2-d array like a matrix
36 input -> a matrix with its no. of rows and columns
37 output-> print the individual element of the matrix
38 */
39 void display(int MAT[][COL],int row,int col){
40 for(int i=0;i<row;i++){
41 for(int j=0;j<col;j++){
42 printf("%d ",MAT[i][j]);
43 }
44 printf("\n");//end of a row, control pass to a new line
45 }
46 }
47 /*
48 Description - this functions performes addtion of two matrix, and addition
49 of two is stored into 3rd matrix.
50 input -> 3 matrix and no. of rows and columns
51 output -> addtion of two 2-d arrays
52 N.B. for addition matrix must be of same dimension
53 */
54 void addMatrix(int MAT1[][COL],int MAT2[][COL],int MAT3[][COL],int row,int col){
55 for(int i=0;i<row;i++){
56 for(int j=0;j<col;j++){
57 MAT3[i][j] = MAT1[i][j]+MAT2[i][j];
58 }
59 }
60 }
61 /*
62 Description -> this function will perform the sum of left diagonal elements of
63 two 2-d arrays.
64 Hints: when the index of row and column is equal we get the diagonal
65 element of the 2-d matrix
66 */
67 void leftDiagonal(int MAT1[][COL],int MAT2[][COL],int MAT3[][COL],int row,int col){
68 for(int i=0;i<row;i++){
69 for(int j=0;j<col;j++){
70 /*Check this condition of if() statement
71 * here we are looking for i==j, means left
72 diagonal elements only
73 */
74 if(i==j) MAT3[i][j] = MAT1[i][j]+MAT2[i][j];
75 //others elements will be set to zero
76 else MAT3[i][j] = 0;
77 }
78 }
79 display(MAT3,row,col);
80 }
81 /*
82 *Description -> this function will add the right diagonal elements
83 of two 2-d arrays
84 */
85 void rightDiagonal(int MAT1[][COL],int MAT2[][COL],int MAT3[][COL],
86 int row,int col){
87 //it will set all the elements to zero
88 for(int i=0;i<row;i++){
89 for(int j=0;j<col;j++){
90 MAT3[i][j]=0;
91 }
92 }
93 /*
94 *How to get the right diagonal elements?
95 *each row has only one element as diagonal
96 *so row will be start from 0 and execute upto row-1, so i will be
97 incremrent by 1 only, i= {0,1,2,3,4,.....,n-1}
98 *What about column?
99 *assume a matrix of dimension 3x3, so the left diagonal elements
100 are (0,2),(1,1),(2,0). Look at the index of j, it starts from 2
101 and ends in 0, means column number will be start from (n-1) and
102 ends with 0, means, value of j = {n-1,n-2,.....3,2,1,0}. here value
103 of j is decrement by 1 and starts with col-1
104 */
105 for(int i=0, j=col-1;i<row;i++,j--){
106 MAT3[i][j] = MAT1[i][j]+MAT2[i][j];
107 }
108 display(MAT3,row,col);
109 }
110 /*
111 *for square matrix, means no. of rows and colms are equal
112 */
113 void transposeMatrix(int MAT1[][COL],int row,int col,int MAT2[][COL]){
114 for(int i=0;i<row;i++){
115 for(int j=0;j<col;j++){
116 /*
117 *look at the index of MAT1 and MAT2, they are simply opposite
118 of each other.
119 */
120 MAT2[i][j] = MAT1[j][i];
121 }
122 }
123 }
124 /*
125 This function will transpose of a matrix of any dimension
126 */
127 void transposeMatrixRegular(int MAT1[][COL],int row,int col,int MAT2[][ROW]){
128 for(int i=0;i<col;i++){
129 for(int j=0;j<row;j++){
130 MAT2[i][j] = MAT1[j][i];
131 }
132 }
133 }
134 int main(){
135 int MAT1[ROW][COL];
136 int MAT2[ROW][COL];
137 int TRANS[ROW][COL];
138 /*
139 *initializer list, only works during declaration
140 *it will initialize all the elements to zero
141 */
142 int MAT3[ROW][COL] = {{0}};
143 int row,col;
144 printf("Enter number of row and column");
145 scanf("%d %d",&row,&col);
146 printf("Taking input for the first matrix\n");
147 input(MAT1,row,col);
148 printf("Taking input for the second matrix\n");
149 input(MAT2,row,col);
150 printf("The first matrix will be like \n");
151 display(MAT1,row,col);
152 printf("\n");
153 printf("The second matrix will be like \n");
154 display(MAT2,row,col);
155 printf("\n");
156 printf("Result after addition\n");
157 addMatrix(MAT1,MAT2,MAT3,row,col);
158 display(MAT3,row,col);
159 printf("Result after additon of left diagonal elemenst\n");
160 leftDiagonal(MAT1,MAT2,MAT3,row,col);
161 printf("\n");
162 printf("Result after additon of right diagonal elemenst\n");
163 rightDiagonal(MAT1,MAT2,MAT3,row,col);*/
164 if(row==col){
165 printf("Transpose of a square matrix \n");
166 transposeMatrix(MAT1,row,col,TRANS);
167 display(TRANS,row,col);
168 printf("\n");
169 }
170 else{
171 printf("Transpose of a regular matrix \n");
172 transposeMatrixRegular(MAT1,row,col,TRANS);
173 display(TRANS,col,row);
174 }
175
176 }
177

You might also like