1: /*
2: ==============================================================
3: Name : Sparse Matrix
4: Author : Mujahid, Shivkumar
5: Version :
6: Copyright : Your copyright notice
7: Description : Hello World in C, Ansi-style
8: ===========================================================
9: */
10: #include<stdio.h>
11: #define MAX 30
12: void creat(int a[10][10],int m,int n);
13: void sparse(int a[10][10],int m,int n,int b[MAX][3]);
14: void print(int b[MAX][3]);
15: void addition(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3]);
16: void transpose(int [][3],int [][3]);
17: void fast_transpose(int [MAX][3],int [MAX][3]);
18: void main()
19: {
20: int a[10][10],b1[MAX][3],b2[MAX][3],b3[MAX][3];
21: int m1,n1,m2,n2,choice,op;
22: printf("Enter First Matrix:");
23: printf("\nEnter number of rows and column:\n");
24: scanf("%d%d",&m1,&n1);
25: creat(a,m1,n1);
26: sparse(a,m1,n1,b1);
27: printf("\n\nEnter Second Matrix:");
28: printf("\nEnter number of rows and column:\n");
29: scanf("%d%d",&m2,&n2);
30: creat(a,m2,n2);
31: sparse(a,m2,n2,b2);
32: printf("\nYour First Sparse Matrix:");
33: print(b1);
34: printf("\n\nYour Second Sparse Matrix:");
35: print(b2);
36: do
37: {
38: printf("\n_______________________________________________________");
39: printf("\nChoose the operation which do you want to perform: ");
40: printf("\n 1.Addition\n 2.Simple Transpose\n 3.Fast Transpose\n");
41: printf("Your choice is: ");
42: scanf("%d",&choice);
43: switch(choice)
44: {
45: case 1:
46: if(m1==m2 && n1==n2)
47: {
48: printf("\nAddition of Sparse Matrix:\n");
49: addition(b1,b2,b3);
50: print(b3);
51: }
52: else
53: printf("\nAddition is not possible...!!!");
54: break;
55: case 2:
56: printf("\nTranspose of First Sparse Matrix:\n");
57: transpose(b2,b3);
58: print(b3);
59: printf("\n\nTranspose of Second Sparse Matrix:\n");
60: transpose(b2,b3);
61: print(b3);
62: break;
63: case 3:
64: printf("\nTranspose of First Sparse Matrix:\n");
65: fast_transpose(b1,b3);
66: print(b3);
67: printf("\n\nTranspose of Second Sparse Matrix:\n");
68: fast_transpose(b2,b3);
69: print(b3);
70: break;
71: default:
72: printf("Invalid option entered....!!!!");
73: break;
74: }
75: printf("\n\nEnter your choice:\nDO you want to continue or not???(0/1): ");
76: scanf("%d",&op);
77: }while(op==1);
78: }
79: /*----------- Creat Function ---------*/
80: void creat(int a[10][10],int m,int n)
81: {
82: int i,j;
83: printf("Enter matrix:\n");
84: for(i=0;i<m;i++)
85: for(j=0;j<n;j++)
86: {
87: scanf("%d",&a[i][j]);
88: }
89: }
90: /*-------- Sparse Matrix ---*/
91: void sparse(int a[10][10],int m,int n,int b[MAX][3])
92: {
93: int i,j,k=1;
94: b[0][0]=m;
95: b[0][1]=n;
96: for(i=0;i<m;i++)
97: for(j=0;j<n;j++)
98: if(a[i][j]!=0)
99: {
100: b[k][0]=i;
101: b[k][1]=j;
102: b[k][2]=a[i][j];
103: k++;
104: }
105: b[0][2]=k-1;
106: }
107: /*-------- Print Function ----------*/
108: void print(int b[MAX][3])
109: {
110: int i,n;
111: n=b[0][2];
112: printf("\nRows: %d\tColumns: %d",b[0][0],b[0][1]);
113: printf("\nEntered Sparse Matrix:\n");
114: for(i=0;i<=n;i++)
115: printf("\n%d\t%d\t%d\t",b[i][0],b[i][1],b[i][2]);
116: }
117: /*---------------- Addition Function ------------*/
118: void addition(int b1[MAX][3],int b2[MAX][3],int b3[MAX][3])
119: {
120: int i,j,k,t1,t2;
121: t1=b1[0][2];
122: t2=b2[0][2];
123: i=j=k=0;
124: b3[0][0]=b1[0][0];
125: b3[0][1]=b1[0][1];
126: while(i<=t1 && j<=t2)
127: {
128: if(b1[i][0]<b2[j][0])
129: {
130: b3[k][0]=b1[i][0];
131: b3[k][1]=b1[i][1];
132: b3[k][2]=b1[i][2];
133: i++;
134: k++;
135: continue;
136: }
137: if(b2[j][0]<b1[i][0])
138: {
139: b3[k][0]=b2[j][0];
140: b3[k][1]=b2[j][1];
141: b3[k][2]=b2[j][2];
142: j++;
143: k++;
144: continue;
145: }
146: if(b1[i][1]<b2[j][1])
147: {
148: b3[k][0]=b1[i][0];
149: b3[k][1]=b1[i][1];
150: b3[k][2]=b1[i][2];
151: i++;
152: k++;
153: continue;
154: }
155: if(b2[j][1]<b1[i][1])
156: {
157: b3[k][0]=b2[j][0];
158: b3[k][1]=b2[j][1];
159: b3[k][2]=b2[j][2];
160: j++;
161: k++;
162: continue;
163: }
164: b3[k][0]=b1[i][2];
165: b3[k][1]=b1[i][2];
166: b3[k][2]=b2[i][2]+b2[j][2];
167: i++;
168: j++;
169: k++;
170: }
171: while(i<=t1)
172: {
173: b3[k][0]=b1[i][0];
174: b3[k][1]=b1[i][1];
175: b3[k][2]=b1[i][2];
176: i++;
177: k++;
178: }
179: while(j<=t2)
180: {
181: b3[k][0]=b2[j][0];
182: b3[k][1]=b2[j][1];
183: b3[k][2]=b2[j][2];
184: j++;
185: k++;
186: }
187: b3[0][2]=k-1;
188: }
189: /*---------- Simple Transpose Function ------*/
190: void transpose(int b1[][3],int b3[][3])
191: {
192: int i,j,k,n;
193: b3[0][0]=b1[0][1];
194: b3[0][1]=b1[0][0];
195: b3[0][2]=b1[0][2];
196: k=1;
197: n=b1[0][2];
198: for(i=0;i<b1[0][1];i++)
199: for(j=0;j<n;j++)
200: if(i==b1[j][1])
201: {
202: b3[k][0]=i;
203: b3[k][1]=b1[j][0];
204: b3[k][2]=b1[j][2];
205: k++;
206: }
207: }
208: /*-------------- Fast Transpose Function ------------*/
209: void fast_transpose(int b1[MAX][3],int b3[MAX][3])
210: {
211: int m,n,t,i,col_num,loc;
212: int total[MAX],index[MAX];
213: m=b1[0][0];
214: n=b1[0][1];
215: t=b1[0][2];
216: b3[0][0]=n;
217: b3[0][1]=m;
218: b3[0][2]=t;
219: for(i=0;i<n;i++)
220: total[i]=0;
221: for(i=0;i<=t;i++)
222: {
223: col_num=b1[i][1];
224: total[col_num]++;
225: }
226: index[0]=1;
227: for(i=1;i<n;i++)
228: index[i]=index[i-1]+index[i-1];
229: for(i=1;i<=t;i++)
230: {
231: col_num=b1[i][1];
232: loc=index[col_num];
233: index[col_num]++;
234: b3[loc][0]=b1[i][1];
235: b3[loc][1]=b1[i][0];
236: b3[loc][2]=b1[i][2];
237: }
238: }
239: ----------------OUTPUT----------------
240: Enter First Matrix:
241: Enter number of rows and column:
242: 3
243: 3
244: Enter matrix:
245: 1
246: 2
247: 0
248: 3
249: 5
250: 0
251: 6
252: 9
253: 0
254: Enter Second Matrix:
255: Enter number of rows and column:
256: 3
257: 3
258: Enter matrix:
259: 1
260: 0
261: 2
262: 3
263: 5
264: 0
265: 6
266: 0
267: 9
268: Your First Sparse Matrix:
269: Rows: 3 Columns: 3
270: Entered Sparse Matrix:
271: 3 3 6
272: 0 0 1
273: 0 1 2
274: 1 0 3
275: 1 1 5
276: 2 0 6
277: 2 1 9
278: Your Second Sparse Matrix:
279: Rows: 3 Columns: 3
280: Entered Sparse Matrix:
281: 3 3 6
282: 0 0 1
283: 0 2 2
284: 1 0 3
285: 1 1 5
286: 2 0 6
287: 2 2 9
288: _______________________________________________________
289: Choose the operation which do you want to perform:
290: 1.Addition
291: 2.Simple Transpose
292: 3.Fast Transpose
293: Your choice is: 1
294: Addition of Sparse Matrix:
295: Rows: 6 Columns: 6
296: Entered Sparse Matrix:
297: 6 6 8
298: 1 1 2
299: 0 1 2
300: 0 2 2
301: 3 3 6
302: 5 5 10
303: 6 6 12
304: 2 1 9
305: 2 2 9
306: Enter your choice:
307: DO you want to continue or not???(0/1): 1
308: _______________________________________________________
309: Choose the operation which do you want to perform:
310: 1.Addition
311: 2.Simple Transpose
312: 3.Fast Transpose
313: Your choice is: 2
314: Transpose of First Sparse Matrix:
315: Rows: 3 Columns: 3
316: Entered Sparse Matrix:
317: 3 3 6
318: 0 0 1
319: 0 1 3
320: 0 2 6
321: 1 1 5
322: 2 0 2
323: 6 6 12
324: Transpose of Second Sparse Matrix:
325: Rows: 3 Columns: 3
326: Entered Sparse Matrix:
327: 3 3 6
328: 0 0 1
329: 0 1 3
330: 0 2 6
331: 1 1 5
332: 2 0 2
333: 6 6 12
334: Enter your choice:
335: DO you want to continue or not???(0/1): 1
336: _______________________________________________________
337: Choose the operation which do you want to perform:
338: 1.Addition
339: 2.Simple Transpose
340: 3.Fast Transpose
341: Your choice is: 3
342: Transpose of First Sparse Matrix:
343: Rows: 3 Columns: 3
344: Entered Sparse Matrix:
345: 3 3 6
346: 0 0 1
347: 0 1 3
348: 0 2 6
349: 1 2 9
350: 2 0 2
351: 6 6 12
352: Transpose of Second Sparse Matrix:
353: Rows: 3 Columns: 3
354: Entered Sparse Matrix:
355: 3 3 6
356: 0 0 1
357: 1 1 5
358: 0 2 6
359: 2 0 2
360: 2 2 9
361: 6 6 12
362: Enter your choice:
363: DO you want to continue or not???(0/1): 0
No comments:
Post a Comment