1: /*
2: ============================================================================
3: Name : Matrix Operations
4: Authors : Shivkumar, Mujahid
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ============================================================================
9: */
10: int *input(int **mat,int *row,int *col)
11: {
12: int i,j;
13: printf("\nEnter Number of Rows and columns :\n");
14: scanf("%d%d",row,col);
15: mat=malloc(*row * sizeof(int*));
16: for(i=0;i<*row;i++)
17: {
18: mat[i]=malloc(*col * sizeof(int*)); //While using malloc use m[i] instead of *(m+i)
19: if(mat[i]==0)//While using malloc use m[i] instead of *(m+i)
20: {
21: printf("\nMemory can not be alloted.\n");
22: return 0;
23: }
24: }
25: printf("\nEnter Elements :\n");
26: for(i=0;i<*row;i++)
27: for(j=0;j<*col;j++)
28: {
29: scanf("%d",(*(mat+i)+j));
30: // printf("%d\n",*(*(mat+i)+j));
31: }
32: return(mat);
33: }
34: void display(int **mat,int row,int col)
35: {
36: int i,j;
37: printf("\nMatrix :");
38: printf("\nRow : %d Col : %d",row,col);
39: for(i=0;i<row;i++)
40: {
41: printf("\n");
42: for(j=0;j<col;j++)
43: printf(" %d ",*(*(mat+i)+j));
44: }
45: }
46: int *add(int **mat1,int **mat2,int **mat3,int row,int col)
47: {
48: int i,j;
49: mat3=malloc(row * sizeof(int*));//Whenever new Matrix defined malloc must be involved for it
50: for(i=0;i<row;i++)
51: {
52: mat3[i]=malloc(col * sizeof(int*));
53: if(mat3[i]==0)//While using malloc use m[i] instead of *(m+i)
54: {
55: printf("\nMemory can not be alloted.\n");
56: return 0;
57: }
58: }
59: for(i=0;i<row;i++)
60: for(j=0;j<col;j++)
61: *(*(mat3+i)+j)=*(*(mat1+i)+j)+*(*(mat2+i)+j);
62: return(mat3);
63: }
64: int *transpose(int **mat,int **mat3,int row,int col)
65: {
66: int i,j;
67: mat3=malloc(row * sizeof(int*));//Whenever new Matrix defined malloc must be involved for it
68: for(i=0;i<row;i++)
69: {
70: mat3[i]=malloc(col * sizeof(int*));
71: if(mat3[i]==0)//While using malloc use m[i] instead of *(m+i)
72: {
73: printf("\nMemory can not be alloted.\n");
74: return 0;
75: }
76: }
77: for(i=0;i<row;i++)
78: for(j=0;j<col;j++)
79: *(*(mat3+i)+j)=*(*(mat+j)+i);
80: return(mat3);
81: }
82: int *multiply(int **mat1,int **mat2,int **mat3,int row1,int col1,int row2,int col2)
83: {
84: int i,j,k,sum=0;
85: mat3=malloc(row1 * sizeof(int*));//Whenever new Matrix defined malloc must be involved for it
86: for(i=0;i<row1;i++)
87: {
88: mat3[i]=malloc(col2 * sizeof(int*));
89: if(mat3[i]==0)//While using malloc use m[i] instead of *(m+i)
90: {
91: printf("\nMemory can not be alloted.\n");
92: return 0;
93: }
94: }
95: sum=0;
96: printf("\nResultant Matrix :\n");
97: for(i=0;i<row1;i++ )
98: {
99: for(j=0;j<col2;j++)
100: {
101: for(k=0;k<row1;k++)
102: {
103: sum=sum+(*(*(mat1+i)+k))*(*(*(mat2+k)+j));
104: }
105: *(*(mat3+i)+j) = sum;
106: sum = 0;
107: }
108: }
109: return(mat3);
110: }
111: #include<stdio.h>
112: #include<stdlib.h>
113: int main()
114: {
115: int **mat1=0,**mat2=0,**mat3=0,ch,row,col,row1,col1,row2,col2;
116: mat1=input(mat1,&row1,&col1);
117: display(mat1,row1,col1);
118: mat2=input(mat2,&row2,&col2);
119: display(mat2,row2,col2);
120: do
121: {
122: printf("\nMenu\n1.Addition\n2.Multiplication\n3.Transpose\n0.Exit\n");
123: scanf("%d",&ch);
124: switch(ch)
125: {
126: case 1 :
127: if(row1!=row2&&col1!=col2)
128: {
129: printf("\nRow or Column doesn't match.");
130: return 0;
131: }
132: else
133: row=row1;
134: col=col1;
135: printf("\nAdd :\n");
136: mat3=add(mat1,mat2,mat3,row,col);
137: display(mat3,row,col);
138: break;
139: case 2 :
140: if(row1!=col2)
141: {
142: printf("\nRow and Column doesnt match.");
143: return 0;
144: }
145: printf("\nMultiplication :\n");
146: mat3=multiply(mat1,mat2,mat3,row1,col1,row2,col2);
147: display(mat3,row1,col2);
148: break;
149: case 3 :
150: row=row1;
151: col=col1;
152: mat3=transpose(mat1,mat3,row,col);
153: display(mat3,row,col);
154: row=row2;
155: col=col2;
156: mat3=transpose(mat2,mat3,row,col);
157: display(mat3,row,col);
158: break;
159: case 0: break;
160: default: printf("\nInvalid Choice\n");
161: break;
162: }
163: }while(ch!=0);
164: free(mat1);
165: free(mat2);
166: free(mat3);
167: return 0;
168: }
169: -------------OUTPUT-------------
170: Enter Number of Rows and columns :
171: 3
172: 3
173: Enter Elements :
174: 1
175: 2
176: 3
177: 4
178: 5
179: 6
180: 7
181: 8
182: 9
183: Matrix :
184: Row : 3 Col : 3
185: 1 2 3
186: 4 5 6
187: 7 8 9
188: Enter Number of Rows and columns :
189: 3
190: 3
191: Enter Elements :
192: 1
193: 2
194: 3
195: 4
196: 5
197: 6
198: 7
199: 8
200: 9
201: Matrix :
202: Row : 3 Col : 3
203: 1 2 3
204: 4 5 6
205: 7 8 9
206: Menu
207: 1.Addition
208: 2.Multiplication
209: 3.Transpose
210: 0.Exit
211: 1
212: Add :
213: Matrix :
214: Row : 3 Col : 3
215: 2 4 6
216: 8 10 12
217: 14 16 18
218: Menu
219: 1.Addition
220: 2.Multiplication
221: 3.Transpose
222: 0.Exit
223: 2
224: Multiplication :
225: Resultant Matrix :
226: Matrix :
227: Row : 3 Col : 3
228: 30 36 42
229: 66 81 96
230: 102 126 150
231: Menu
232: 1.Addition
233: 2.Multiplication
234: 3.Transpose
235: 0.Exit
236: 3
237: Matrix :
238: Row : 3 Col : 3
239: 1 4 7
240: 2 5 8
241: 3 6 9
242: Matrix :
243: Row : 3 Col : 3
244: 1 4 7
245: 2 5 8
246: 3 6 9
247: Menu
248: 1.Addition
249: 2.Multiplication
250: 3.Transpose
251: 0.Exit
252: 0
No comments:
Post a Comment