need help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 10
Reputation: shoby is an unknown quantity at this point 
Solved Threads: 0
shoby shoby is offline Offline
Newbie Poster

need help

 
0
  #1
Aug 15th, 2007
C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice.
displaying following menu.
------------------
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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need help

 
0
  #2
Aug 15th, 2007
OK, so what have YOU managed to achieve so far?
http://www.daniweb.com/forums/announcement118-2.html

This isn't somewhere where you can just dump your assignment and come back after your party expecting a complete answer for you to hand in. You're going to have to pull your own weight.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 10
Reputation: shoby is an unknown quantity at this point 
Solved Threads: 0
shoby shoby is offline Offline
Newbie Poster

Re: need help

 
0
  #3
Aug 16th, 2007
so far i've completed the menu.
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. printf(" Welcome to User Menu of Matrix Operations\n\n");
  8. printf("--------------------------------------------\n");
  9. printf("\t\tMAIN MENU\n");
  10. printf("--------------------------------------------\n");
  11. printf("\t1. MATRIX ADDITION\n");
  12. printf("\t2. MATRIX SUBTRACTION\n");
  13. printf("\t3. MATRIX MULTIPLICATION\n");
  14. printf("\t4. MATRIX TRANSPOSE\n");
  15. printf("\t5. TO EXIT\n");
  16. printf("--------------------------------------------\n");
  17. printf("Please enter your option <1/2/3/4/5>:\n");
  18.  
  19. system("pause");
  20. return 0;
  21. }
i need guidance on hw to do the loop to input elements of the matrix or matrices frm user. pls help me to proceed.
Last edited by Ancient Dragon; Aug 16th, 2007 at 9:23 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,437
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: need help

 
0
  #4
Aug 16th, 2007
next you need to put all that code inside an infinite loop so that it will get repeated until someone selects the 5th menu item. Then after printing the menu you need to get user input by calling cin.get(). You also need to write four functions, one function for each of the menu items and call the appropriate function after getting user input.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: need help

 
0
  #5
Aug 16th, 2007
Originally Posted by shoby View Post
so far i've completed the menu.
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. printf(" Welcome to User Menu of Matrix Operations\n\n");
  8. printf("--------------------------------------------\n");
  9. printf("\t\tMAIN MENU\n");
  10. printf("--------------------------------------------\n");
  11. printf("\t1. MATRIX ADDITION\n");
  12. printf("\t2. MATRIX SUBTRACTION\n");
  13. printf("\t3. MATRIX MULTIPLICATION\n");
  14. printf("\t4. MATRIX TRANSPOSE\n");
  15. printf("\t5. TO EXIT\n");
  16. printf("--------------------------------------------\n");
  17. printf("Please enter your option <1/2/3/4/5>:\n");
  18.  
  19. system("pause");
  20. return 0;
  21. }
i need guidance on hw to do the loop to input elements of the matrix or matrices frm user. pls help me to proceed.
I'm afraid we can't really help you until you actually code something, and by something I mean something related to the actual problem.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 10
Reputation: shoby is an unknown quantity at this point 
Solved Threads: 0
shoby shoby is offline Offline
Newbie Poster

Re: need help(c prog. for matrix operation )

 
0
  #6
Aug 19th, 2007
i hv developed following code for the addition function:
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. void add_matrices(int a[][3], int b[][3], int result[][3]);
  5. void print_matrix(int a[][3]);
  6.  
  7.  
  8. int main()
  9. {
  10. printf(" Welcome to User Menu of Matrix Operations\n\n");
  11. printf("--------------------------------------------\n");
  12. printf("\t\tMAIN MENU\n");
  13. printf("--------------------------------------------\n");
  14. printf("\t1. MATRIX ADDITION\n");
  15. printf("\t2. MATRIX SUBTRACTION\n");
  16. printf("\t3. MATRIX MULTIPLICATION\n");
  17. printf("\t4. MATRIX TRANSPOSE\n");
  18. printf("\t5. TO EXIT\n");
  19. printf("--------------------------------------------\n");
  20. printf("Please enter your option <1/2/3/4/5>:\n");
  21.  
  22. {
  23. int option;
  24. int p[3][3];
  25. int q[3][3];
  26. int r[3][3];
  27. add_matrices(p, q, r);
  28.  
  29. printf("\nMatrix 1:\n");
  30. print_matrix(p);
  31.  
  32. printf("\nMatrix 2:\n");
  33. print_matrix(q);
  34.  
  35. printf("\nResult:\n");
  36. print_matrix(r);
  37.  
  38. }
  39. }
  40. {
  41. for (option=0; option<6; option++)
  42. scanf("%d",& option);
  43.  
  44. {
  45. if (option==1)
  46. switch (operations)
  47. {
  48. case 'Addition':
  49. {
  50. void add_matrices(int a[][3], int b[][3], int result[][3])
  51. {
  52. int i, j;
  53. for(i=0; i<3; i++)
  54. {
  55. for(j=0; j<3; j++)
  56. {
  57. result[i][j] = a[i][j] + b[i][j];
  58. }
  59. }
  60. }
  61.  
  62. void print_matrix(int a[][3])
  63. {
  64. int i, j;
  65. for (i=0; i<3; i++)
  66. {
  67. for (j=0; j<3; j++)
  68. {
  69. printf("%d\t", a[i][j]);
  70. }
  71. printf("\n");
  72. }
  73.  
  74. }
  75. }
  76. system("pause");
  77. return 0;
  78. }
pls correct me, as im getting error while compling.
Last edited by stymiee; Aug 20th, 2007 at 11:34 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 13
Reputation: ananthxp is an unknown quantity at this point 
Solved Threads: 0
ananthxp's Avatar
ananthxp ananthxp is offline Offline
Newbie Poster

Re: need help

 
0
  #7
Aug 20th, 2007
Be cool. dont get panic of programming first.
proceed like this.
1. print the operations that you are going to perform i.e. Add,sub,..
2.get the option(1->addition 2->subtraction 3-> multiplication 4-> transpose)
3.get the input for two matrices if suppose the option is transpose no need to get two matrices just one.
4.put a switch case for each operation
in case 1 call addition function
in case 2 call subtraction and so on
5. print the input matrices and your output
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 10
Reputation: shoby is an unknown quantity at this point 
Solved Threads: 0
shoby shoby is offline Offline
Newbie Poster

Re: need help

 
0
  #8
Aug 20th, 2007
  1. # include <iostream>
  2. using namespace std;
  3.  
  4. void matrix_addition (int a[3][3], int b[3][3], int result[3][3]);
  5. void matrix_subtraction (int a[3][3], int b[3][3], int result[3][3]);
  6. void matrix_multiply (int a[3][3], int b[3][3], int result[3][3]);
  7. void matrix_transpose (int a[3][3],int result[3][3]);
  8. void print_matrix(int a[3][3]);
  9.  
  10. int main()
  11. {
  12. int option;
  13.  
  14. /*Print the operations that to be performed*/
  15.  
  16. printf(" Welcome to User Menu of Matrix Operations\n\n");
  17. printf("--------------------------------------------\n");
  18. printf("\t\tMAIN MENU\n");
  19. printf("--------------------------------------------\n");
  20. printf("\t1. MATRIX ADDITION\n");
  21. printf("\t2. MATRIX SUBTRACTION\n");
  22. printf("\t3. MATRIX MULTIPLICATION\n");
  23. printf("\t4. MATRIX TRANSPOSE\n");
  24. printf("\t5. TO EXIT\n");
  25. printf("--------------------------------------------\n");
  26. printf("Please enter your option <1/2/3/4/5>:\n");
  27. {
  28.  
  29. /*Get the option*/
  30. for (option=1;option<5;option++)
  31. scanf("%d,& option");
  32. }
  33. /* Get the option*/
  34. if (option==1)
  35. matrix_addition;
  36. else if (option==2)
  37. matrix_subtraction;
  38. else if (option==3)
  39. matrix_multiply;
  40. else if (option==4)
  41. matrix_transpose;
  42. else
  43. exit;
  44.  
  45. /*Get the input for two matrices*/
  46. /*Suppose the option is transpose, the input is one matrice*/
  47. int p[3][3];
  48. int q[3][3];
  49. int r[3][3];
  50.  
  51. matrix_addition(p, q, r);
  52. matrix_subtraction(p,q,r);
  53. matrix_multiply(p,q,r);
  54. matrix_transpose(p,r);
  55.  
  56. /*Switch case for each operation*/
  57. switch (matrix_operations)
  58. case 1:/* add function*/
  59. case 2:/* subtraction function*/
  60. case 3:/*multiply function*/
  61. case 4:/*transpose function*/
  62. default: exit;
  63.  
  64. /*Print the input matrices & output*/
  65. printf("\nMatrix 1:\n");
  66. print_matrix(p);
  67.  
  68. printf("\nMatrix 2:\n");
  69. print_matrix(q);
  70.  
  71. printf("\nResult:\n");
  72. print_matrix(r);
  73.  
  74. system("pause");
  75. return 0;
  76. }

Is this a correct approach? im working on writing functions for the operations.
Last edited by stymiee; Aug 20th, 2007 at 11:30 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: need help

 
1
  #9
Aug 20th, 2007
Added comments where things are not right.
Did not pay attention to logic of program.

Need work on how to place {} correctly as a block.
Need to work on usage of switch.
Need to work calling functions.
Need to work in proper formatting.


  1. /* # include <iostream> This is not for C language */
  2. /* using namespace std; This is not for C language */
  3.  
  4. #include <stdio.h> /* This header file is for C language */
  5. #include <stdlib.h> /* for the exit() function */
  6.  
  7. void matrix_addition (int a[3][3], int b[3][3], int result[3][3]);
  8. void matrix_subtraction (int a[3][3], int b[3][3], int result[3][3]);
  9. void matrix_multiply (int a[3][3], int b[3][3], int result[3][3]);
  10. void matrix_transpose (int a[3][3],int result[3][3]);
  11. void print_matrix(int a[3][3]);
  12.  
  13. int main( void )
  14. {
  15. int option = 0; /* initialize option */
  16.  
  17. /*Print the operations that to be performed*/
  18.  
  19. printf(" Welcome to User Menu of Matrix Operations\n\n");
  20. printf("--------------------------------------------\n");
  21. printf("\t\tMAIN MENU\n");
  22. printf("--------------------------------------------\n");
  23. printf("\t1. MATRIX ADDITION\n");
  24. printf("\t2. MATRIX SUBTRACTION\n");
  25. printf("\t3. MATRIX MULTIPLICATION\n");
  26. printf("\t4. MATRIX TRANSPOSE\n");
  27. printf("\t5. TO EXIT\n");
  28. printf("--------------------------------------------\n");
  29. printf("Please enter your option <1/2/3/4/5>:\n");
  30. /* { wrong place for curly bracket */
  31.  
  32. /*Get the option*/
  33. for (option=1;option<5;option++)/*It will run until user input a 4 or bigger*/
  34. scanf("%d",&option); /* Compare with yours */
  35. /* } This curly bracket doesn't work here */
  36. /* Get the option*/
  37. if (option==1) /* After loop option is always 5 or bigger */
  38. matrix_addition; /* function expects three 2D integers-arrays paramenters */
  39. else if (option==2) /* Never will be a 2 */
  40. matrix_subtraction; /* function needs three 2D integers-arrays parameters */
  41. else if (option==3) /* Never will be a 3 */
  42. matrix_multiply; /* missing parameters */
  43. else if (option==4) /* Never will happen */
  44. matrix_transpose; /* missing parameters */
  45. else
  46. exit( 1 );/* exit is a function declared in <stdlib.h> you need to included it expects an int as argument */
  47.  
  48. /*Get the input for two matrices*/
  49. /*Suppose the option is transpose, the input is one matrice*/
  50. int p[3][3];
  51. int q[3][3];
  52. int r[3][3];
  53.  
  54. matrix_addition(p, q, r);
  55. matrix_subtraction(p,q,r);
  56. matrix_multiply(p,q,r);
  57. matrix_transpose(p,r);
  58.  
  59. /*Switch case for each operation*/
  60. switch (matrix_operations) /* matrix_operation is undeclared */
  61. { /* needed the bracket */
  62. case 1:/* add function*/
  63. break; /* needed or it will execute next case */
  64. case 2:/* subtraction function*/
  65. break; /* needed */
  66. case 3:/*multiply function*/
  67. break; /* needed */
  68. case 4:/*transpose function*/
  69. break; /* needed */
  70. default: exit( 1 ); /* look above for info about exit() */
  71. } /* needed the bracket */
  72.  
  73. /*Print the input matrices & output*/
  74. printf("\nMatrix 1:\n");
  75. print_matrix(p);
  76.  
  77. printf("\nMatrix 2:\n");
  78. print_matrix(q);
  79.  
  80. printf("\nResult:\n");
  81. print_matrix(r);
  82.  
  83. system("pause");
  84. return 0;
  85. }
Last edited by Aia; Aug 20th, 2007 at 11:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 10
Reputation: shoby is an unknown quantity at this point 
Solved Threads: 0
shoby shoby is offline Offline
Newbie Poster

Re: need help

 
0
  #10
Aug 27th, 2007
  1. #include <stdio.h> /* This header file is for C language */
  2. #include <stdlib.h> /* for the exit() function */
  3.  
  4. void matrix_add (int A[3][3], int B[3][3], int C[3][3]);
  5. void matrix_subtract (int A[3][3], int B[3][3], int B[3][3]);
  6. void matrix_multiply (int A[3][3], int B[3][3], int C[3][3]);
  7. void matrix_transpose (int A[3][3]);
  8. void print_matrix(int A[3][3]);
  9.  
  10. int main()
  11. {
  12. int counter = 0; /* initialize counter */
  13. int option;/*initialize options available*/
  14. int A[3][3];
  15. int B[3][3];
  16. int C[3][3];
  17. int i;
  18. int j;
  19. int k;
  20. int n;
  21. int temp;
  22. int matrix_operation; /*initialize matrix_operation*/
  23.  
  24. /*Print the operations that to be performed*/
  25.  
  26. printf(" Welcome to User Menu of Matrix Operations\n\n");
  27. printf("--------------------------------------------\n");
  28. printf("\t\tMAIN MENU\n");
  29. printf("--------------------------------------------\n");
  30. printf("\t1. MATRIX ADDITION\n");
  31. printf("\t2. MATRIX SUBTRACTION\n");
  32. printf("\t3. MATRIX MULTIPLICATION\n");
  33. printf("\t4. MATRIX TRANSPOSE\n");
  34. printf("\t5. TO EXIT\n");
  35. printf("--------------------------------------------\n");
  36. printf("Please enter your option <1/2/3/4/5>:\n");
  37.  
  38. /*As buffer*/
  39. for (counter=0;counter<n;counter++)
  40. scanf("%d",&counter);
  41.  
  42. /* Get the option*/
  43. if (option==1)
  44. matrix_add();
  45. else if (option==2)
  46. matrix_subtract();
  47. else if (option==3)
  48. matrix_multiply();
  49. else if (option==4)
  50. matrix_transpose();
  51. else
  52. exit( 1 );
  53.  
  54. /*Get the input for two matrices*/
  55. /*Suppose the option is transpose, the input is one matrice*/
  56.  
  57.  
  58. /*Switch case for each operation*/
  59. switch (matrix_operations)
  60.  
  61. {
  62. case 1:/*addition function*/
  63. for (i = 0; i < n; i++)
  64. for (j = 0; j < n; j++)
  65. A[i][j] = B[i][j] + C[i][j];
  66. break;
  67. case 2:/*subtraction function*/
  68. for (i = 0; i < n; i++)
  69. for (j = 0; j < n; j++)
  70. A[i][j] = B[i][j] - C[i][j];
  71. break;
  72. case 3:/*multiplication function*/
  73. for (i = 0; i < n; i++)
  74. for (j = 0; j < n; j++)
  75. for (k = A[i][j] = 0; k < n; k++)
  76. A[i][j] += B[i][k] * C[k][j];
  77. break;
  78. case 4:/*transpose function*/
  79. for (i = 0; i < n-1; i++)
  80. for (j = i+1; j < n; j++){
  81. temp = A[i][j];
  82. A[i][j] = A[j][i];
  83. A[j][i] = temp;
  84. break;
  85. default: exit( 1 );
  86. }
  87.  
  88. /*Print the input matrices & output*/
  89. printf("\nMatrix 1:\n");
  90. print_matrix(B);
  91.  
  92. printf("\nMatrix 2:\n");
  93. print_matrix(C);
  94.  
  95. printf("\nResult:\n");
  96. print_matrix(A);
  97.  
  98. system("pause");
  99. return 0;
  100. }

pls provide me guidance. this prog should ask elements of input matrix and produce output which include both input and resultant in matrix form. i've done the following code with functions for the matrix operations. correct me if im wrong. tq.
Last edited by Ancient Dragon; Aug 27th, 2007 at 7:10 am. Reason: corrected code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC