943,936 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1366
  • C RSS
Sep 11th, 2007
0

please check for me the error and tell me~~ thx!

Expand Post »
Write a complete C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice. The program should display the following menu to the user for them to select one of the five options listed.

------------------
MAIN MENU
------------------
1. MATRIX ADDITION
2. MATRIX SUBTRACTION
3. MATRIX MLTIPLICATION
4. MATRIX TRANSPOSE
5. TO EXIT
---------------------------
Please enter your option <1/2/3/4/5>:

The program should ask the elements of the input matrix or matrices from the user once the required operation is selected. The program should produce a neat output showing both the input and the resultant matrix in matrix form. The program should not be terminated until the user selects the option number 5 (TO EXIT).


THIS IS THE PROGRAM I HAVE DONE.. URGENT PLEASE CORRECT FOR ME! THANKS A LOT!!!!



  1. #include <stdio.h> /* This header file is for C language */
  2. #include <stdlib.h> /* for the exit() function */
  3.  
  4. /*Function prototype*/
  5. int matrix_add(int a[3][3],int b[3][3],int result[3][3]);
  6. int matrix_subtract(int a[3][3],int b[3][3],int result[3][3]);
  7. int matrix_multiply(int a[3][3],int b[3][3],int result[3][3]);
  8. int matrix_transpose(int a[3][3], int result[3][3]);
  9. int print_matrix(int c[3][3]);
  10.  
  11. int main()
  12. {
  13. int counter = 0; /* initialize counter */
  14. int option;/*initialize options available*/
  15. int a[3][3];
  16. int b[3][3];
  17. int c[3][3];
  18.  
  19. /*Print the operations that to be performed*/
  20.  
  21. printf(" Welcome to User Menu of Matrix Operations\n\n");
  22. printf("--------------------------------------------\n");
  23. printf("\t\tMAIN MENU\n");
  24. printf("--------------------------------------------\n");
  25. printf("\t1. MATRIX ADDITION\n");
  26. printf("\t2. MATRIX SUBTRACTION\n");
  27. printf("\t3. MATRIX MULTIPLICATION\n");
  28. printf("\t4. MATRIX TRANSPOSE\n");
  29. printf("\t5. TO EXIT\n");
  30. printf("--------------------------------------------\n");
  31. printf("Please enter your option <1/2/3/4/5>:\n");
  32.  
  33. /*Print the input matrices & output*/
  34. {
  35. printf("\nMatrix 1:\n");
  36. print_matrix(a);
  37.  
  38. printf("\nMatrix 2:\n");
  39. print_matrix(b);
  40.  
  41. printf("\nResult:\n");
  42. print_matrix(c);
  43. }
  44.  
  45.  
  46. /*As buffer*/
  47.  
  48. while (counter==0)
  49. {
  50. /* Get the option*/
  51. if (option==1)
  52. {
  53. matrix_add(a,b,c);
  54. }
  55. else if (option==2)
  56. {
  57. matrix_subtract(a,b,c);
  58. }
  59. else if (option==3)
  60. {
  61. matrix_multiply(a,b,c);
  62. }
  63. else if (option==4)
  64. {
  65. matrix_transpose(a,c);
  66. }
  67. else
  68. {
  69. exit( 1 );
  70. }
  71.  
  72. /*Get the input for two matrices*/
  73. /*Suppose the option is transpose, the input is one matrice*/
  74.  
  75.  
  76. /*Switch case for each operation*/
  77. switch (matrix_operations)
  78.  
  79. {
  80. case 1:/*addition function*/
  81. {
  82. printf("Enter elements of Matrix 1:\n");
  83. scanf("%d",& int a[3][3]);
  84. printf("Enter elements of Matrix 2:\n");
  85. scanf("%d",& int b[3][3]);
  86. matrix_add(a,b,c)
  87. }
  88. break;
  89. case 2:/*subtraction function*/
  90. {
  91. printf("Enter elements of Matrix 1:\n");
  92. scanf("%d",& int a[3][3]);
  93. printf("Enter elements of Matrix 2:\n");
  94. scanf("%d",& int b[3][3]);
  95. matrix_subtract(a,b,c)
  96. }
  97. break;
  98. case 3:/*multiplication function*/
  99. {
  100. printf("Enter elements of Matrix 1:\n");
  101. scanf("%d",& int a[3][3]);
  102. printf("Enter elements of Matrix 2:\n");
  103. scanf("%d",& int b[3][3]);
  104. matrix_multiply(a,b,c)
  105. }
  106. break;
  107. case 4:/*transpose function*/
  108. {
  109. printf("Enter elements of Matrix 1:\n");
  110. scanf("%d",& int a[3][3]);
  111. matrix_transpose(a,c)
  112. }
  113. break;
  114. default:
  115. {
  116. printf("End of Program!\n");
  117. exit( 1 );
  118. }
  119.  
  120. }
  121.  
  122. system("pause");
  123. return 0;
  124. }
  125.  
  126. /*Function definition*/
  127. int matrix_add(int a[3][3], int b[3][3], int result[3][3])
  128. {
  129. int i, j;
  130. for(i=0; i<3; i++)
  131. {
  132. for(j=0; j<3; j++)
  133. {
  134. result[i][j] = a[i][j] + b[i][j];
  135. }
  136. }
  137. return (a+b);
  138. }
  139.  
  140. int matrix_subtract(int a[3][3],int b[3][3], int result[3][3])
  141. {
  142. int i,j;
  143. for (i=0;i<3;i++)
  144. {
  145. for(j=0;j<3;j++)
  146. {
  147. result[i][j]= a[i][j]- b[i][j];
  148. }
  149. }
  150. return (a-b);
  151. }
  152.  
  153. int matrix_multiply(a[3][3],b[3][3],int result[3][3])
  154. {
  155. int i,j,k;
  156. {
  157. for (i = 0; i < n; i++)
  158. for (j = 0; j < n; j++)
  159. for (k = a[i][j] = 0; k < n; k++)
  160. {
  161. result[i][j] += b[i][k] * c[k][j];
  162. }
  163. }
  164. return (a*b);
  165. }
  166.  
  167. int matrix_transpose(int a[3][3],int result[3][3])
  168. {
  169. int i,j,n,temp;
  170. {
  171. for (i = 0; i < n-1; i++)
  172. for (j = i+1; j < n; j++)
  173. {
  174. temp = b[i][j];
  175. b[i][j] = b[j][i];
  176. b[j][i] = temp;
  177. }
  178. }
  179. return (b);
  180. }
  181. int print_matrix(int c[3][3])
  182. {
  183. int i, j;
  184. for (i=0; i<3; i++)
  185. {
  186. for (j=0; j<3; j++)
  187. {
  188. printf("%d\t", a[i][j]);
  189. }
  190. printf("\n");
  191. }
  192. }
  193. }
Last edited by Ancient Dragon; Sep 11th, 2007 at 8:28 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
anzdyy is offline Offline
2 posts
since Sep 2007
Sep 11th, 2007
0

Re: please check for me the error and tell me~~ thx!

>URGENT PLEASE CORRECT FOR ME! THANKS A LOT!!!!
You'll get better results if you describe the problems that you're having.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 11th, 2007
0

Re: please check for me the error and tell me~~ thx!

And this has exactly what to do with Linux?
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Sep 12th, 2007
0

Re: please check for me the error and tell me~~ thx!

Looks like a direct rip-off of this post as well, right down to the syntax errors.
http://www.daniweb.com/forums/post428174-13.html

Almost certainly in the same class, could even be the same person registering with a new account hoping the "clean" noob status will garner a bit of extra sympathy / effort on our part.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Trying to solve this problem
Next Thread in C Forum Timeline: Need help with Data Structure and Algorithms





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC