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).

Recommended Answers

All 16 Replies

so far i've completed the menu.

# include <iostream>
using namespace std;

int main()
{
    
    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");
    
system("pause");   
return 0;
}

i need guidance on hw to do the loop to input elements of the matrix or matrices frm user. pls help me to proceed.

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.

Member Avatar for iamthwee

so far i've completed the menu.

# include <iostream>
using namespace std;

int main()
{
    
    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");
    
system("pause");   
return 0;
}

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.

i hv developed following code for the addition function:

# include <iostream>
using namespace std;

void add_matrices(int a[][3], int b[][3], int result[][3]);
void print_matrix(int a[][3]);
    

int main()
{
    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");

{   
    int option;
    int p[3][3];
    int q[3][3];
    int r[3][3];
   add_matrices(p, q, r);

   printf("\nMatrix 1:\n");
   print_matrix(p);

   printf("\nMatrix 2:\n");
   print_matrix(q);

   printf("\nResult:\n");
   print_matrix(r);

}
}
{
    for (option=0; option<6; option++)
    scanf("%d",& option);
    
{
    if (option==1)
    switch (operations)
    {    
    case 'Addition': 
    { 
void add_matrices(int a[][3], int b[][3], int result[][3])
{
   int i, j;
   for(i=0; i<3; i++)
   {
	 for(j=0; j<3; j++)
	 {
	    result[i][j] = a[i][j] + b[i][j];
     }
   }
}

void print_matrix(int a[][3])
{
   int i, j;
   for (i=0; i<3; i++)
   {
	 for (j=0; j<3; j++)
	 {
	    printf("%d\t", a[i][j]);
	 }
	 printf("\n");
   }

}
}
system("pause");
return 0;   
}

pls correct me, as im getting error while compling.

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

# include <iostream>
using namespace std;

void matrix_addition (int a[3][3], int b[3][3], int result[3][3]);
void matrix_subtraction (int a[3][3], int b[3][3], int result[3][3]);
void matrix_multiply (int a[3][3], int b[3][3], int result[3][3]);
void matrix_transpose (int a[3][3],int result[3][3]);
void print_matrix(int a[3][3]);

int main()
{
    int option;

/*Print the operations that to be performed*/

    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");
{

/*Get the option*/
for (option=1;option<5;option++)
scanf("%d,& option");
}
/* Get the option*/
if (option==1)
matrix_addition;
else if (option==2)
matrix_subtraction;
else if (option==3)
matrix_multiply;
else if (option==4)
matrix_transpose;
else
exit;

/*Get the input for two matrices*/
/*Suppose the option is transpose, the input is one matrice*/
   int p[3][3]; 
   int q[3][3]; 
   int r[3][3];

   matrix_addition(p, q, r);
   matrix_subtraction(p,q,r);
   matrix_multiply(p,q,r);
   matrix_transpose(p,r);
    
/*Switch case for each operation*/  
   switch (matrix_operations)
   case 1:/* add function*/
   case 2:/* subtraction function*/
   case 3:/*multiply function*/
   case 4:/*transpose function*/
   default: exit;
   
/*Print the input matrices & output*/   
   printf("\nMatrix 1:\n");
   print_matrix(p);

   printf("\nMatrix 2:\n");
   print_matrix(q);

   printf("\nResult:\n");
   print_matrix(r);
   
system("pause");
return 0;   
}

Is this a correct approach? im working on writing functions for the operations.

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.

/* # include <iostream> This is not for C language */
/* using namespace std; This is not for C language */

#include <stdio.h> /* This header file is for C language */
#include <stdlib.h> /* for the exit() function */

void matrix_addition (int a[3][3], int b[3][3], int result[3][3]);
void matrix_subtraction (int a[3][3], int b[3][3], int result[3][3]);
void matrix_multiply (int a[3][3], int b[3][3], int result[3][3]);
void matrix_transpose (int a[3][3],int result[3][3]);
void print_matrix(int a[3][3]);

int main( void )
{
    int option = 0; /* initialize option */

/*Print the operations that to be performed*/

    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");
/* { wrong place for curly bracket */

/*Get the option*/
    for (option=1;option<5;option++)/*It will run until user input a 4 or bigger*/
        scanf("%d",&option);        /* Compare with yours */
/* } This curly bracket doesn't work here */
/* Get the option*/
    if (option==1) /* After loop option is always 5 or bigger */
        matrix_addition;   /* function expects three 2D integers-arrays paramenters */
    else if (option==2)    /* Never will be a 2 */
        matrix_subtraction; /* function needs three 2D integers-arrays parameters */
    else if (option==3)   /* Never will be a 3 */
        matrix_multiply; /* missing parameters */
    else if (option==4)  /* Never will happen */
        matrix_transpose; /* missing parameters */
    else
        exit( 1 );/* exit is a function declared in <stdlib.h> you need to included it expects an int as argument */

/*Get the input for two matrices*/
/*Suppose the option is transpose, the input is one matrice*/
   int p[3][3]; 
   int q[3][3]; 
   int r[3][3];

   matrix_addition(p, q, r);
   matrix_subtraction(p,q,r);
   matrix_multiply(p,q,r);
   matrix_transpose(p,r);
    
/*Switch case for each operation*/  
   switch (matrix_operations)  /* matrix_operation is undeclared */
   { /* needed the bracket */
           case 1:/* add function*/
               break; /* needed or it will execute next case */
           case 2:/* subtraction function*/
               break; /* needed */
           case 3:/*multiply function*/
               break; /* needed */
           case 4:/*transpose function*/
               break; /* needed */
           default: exit( 1 );  /* look above for info about exit() */
    } /* needed the bracket */
   
/*Print the input matrices & output*/   
   printf("\nMatrix 1:\n");
   print_matrix(p); 
      
   printf("\nMatrix 2:\n");
   print_matrix(q);

   printf("\nResult:\n");
   print_matrix(r);
   
system("pause");
return 0;   
}
#include <stdio.h> /* This header file is for C language */
#include <stdlib.h> /* for the exit() function */

void matrix_add (int A[3][3], int B[3][3], int C[3][3]);
void matrix_subtract (int A[3][3], int B[3][3], int B[3][3]);
void matrix_multiply (int A[3][3], int B[3][3], int C[3][3]);
void matrix_transpose (int A[3][3]);
void print_matrix(int A[3][3]);

int main()
{
    int counter = 0; /* initialize counter */
    int option;/*initialize options available*/
    int A[3][3]; 
    int B[3][3]; 
    int C[3][3];
    int i;
    int j;
    int k;
    int n;
    int temp;
    int matrix_operation; /*initialize matrix_operation*/
    
/*Print the operations that to be performed*/

    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");

/*As buffer*/
    for (counter=0;counter<n;counter++)
        scanf("%d",&counter);        

/* Get the option*/
    if (option==1) 
        matrix_add();   
    else if (option==2)    
        matrix_subtract(); 
    else if (option==3)   
        matrix_multiply(); 
    else if (option==4)  
        matrix_transpose(); 
    else
        exit( 1 );

/*Get the input for two matrices*/
/*Suppose the option is transpose, the input is one matrice*/
   
    
/*Switch case for each operation*/  
   switch (matrix_operations)
   
   { 
           case 1:/*addition function*/
           for (i = 0; i < n; i++)
                  for (j = 0; j < n; j++)
                        A[i][j] = B[i][j] + C[i][j];
               break; 
           case 2:/*subtraction function*/
           for (i = 0; i < n; i++)
                  for (j = 0; j < n; j++)
                        A[i][j] = B[i][j] - C[i][j];
               break; 
           case 3:/*multiplication function*/
           for (i = 0; i < n; i++)
                  for (j = 0; j < n; j++)
                        for (k = A[i][j] = 0; k < n; k++)
                              A[i][j] += B[i][k] * C[k][j];
               break; 
           case 4:/*transpose function*/
            for (i = 0; i < n-1; i++)
                  for (j = i+1; j < n; j++){
                        temp = A[i][j];
                        A[i][j] = A[j][i];
                        A[j][i] = temp;
               break; 
           default: exit( 1 );  
    } 
   
/*Print the input matrices & output*/   
   printf("\nMatrix 1:\n");
   print_matrix(B); 
      
   printf("\nMatrix 2:\n");
   print_matrix(C);

   printf("\nResult:\n");
   print_matrix(A);
   
system("pause");
return 0;   
}

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.

Member Avatar for iamthwee

What are you asking? To check if your code is correct, do the same calculations on paper match your output?

If they don't then you have answered your question. (Either you don't know how to solve this on paper, or your program is wrong)

int n; /* n is not initialized, unwanted data lives here. It needs to be an integer that will stop the for loop when you want */


    for (counter=0;counter<n;counter++) /* by virtue of n not been initialized, we don't know when this loop will stop */
        scanf("%d",&counter);   /* will keep asking for an int until the user inputs one that is larger than whatever lives in `n' */    

/*Almost not change that any of theses will ever be true, except the last else */
/* Still those functions are missing the proper arguments */
    if (option==1) 
        matrix_add();   /* matrix_add( A, B, C ); or whatever the correct multidimentional variables are */
    else if (option==2)    
        matrix_subtract(); /* matrix_subtract( A, B, C ); */
    else if (option==3)   
        matrix_multiply(); /* matrix_multiply( A, B, C ); */
    else if (option==4)  
        matrix_transpose(); /* matrix_transpose( A ); */
    else
        exit( 1 );
commented: Fine analysis. +11
#include <stdio.h> /* This header file is for C language */
#include <stdlib.h> /* for the exit() function */

/*Function prototype*/
int matrix_add(int a[3][3],int b[3][3],int result[3][3]);
int matrix_subtract(int a[3][3],int b[3][3],int result[3][3]);
int matrix_multiply(int a[3][3],int b[3][3],int result[3][3]);
int matrix_transpose(int a[3][3], int result[3][3]);
int print_matrix(int c[3][3]);

int main()
{
    int counter = 0; /* initialize counter */
    int option;/*initialize options available*/
    int a[3][3]; 
    int b[3][3]; 
    int c[3][3];

/*Print the operations that to be performed*/

    printf(" Welcome to User Menu of Matrix Operations\n\n");
    printf("--------------------------------------------\n");
    printf("\t\tMAIN MENU\n");
    printf("--------------------------------------------\n");
    printf("\t1. MATRIX ADDITION\n");
    printf("\t2. MATRIX SUBTRACTION\n");
    printf("\t3. MATRIX MULTIPLICATION\n");
    printf("\t4. MATRIX TRANSPOSE\n");
    printf("\t5. TO EXIT\n");
    printf("--------------------------------------------\n");
    printf("Please enter your option <1/2/3/4/5>:\n");

/*Print the input matrices & output*/  
{ 
   printf("\nMatrix 1:\n");
   print_matrix(a); 

   printf("\nMatrix 2:\n");
   print_matrix(b);

   printf("\nResult:\n");
   print_matrix(c);
} 


/*As buffer*/

while (counter= 0)
{
/* Get the option*/
    if (option==1) 
    {
     matrix_add(a,b,c);
    }
    else if (option==2) 
    {
     matrix_subtract(a,b,c);   
    }
    else if (option==3)
    {
    matrix_multiply(a,b,c);
    }   
    else if (option==4)
    {  
    matrix_transpose(a,c);
    }
    else
    {
        exit( 1 );
    }

/*Get the input for two matrices*/
/*Suppose the option is transpose, the input is one matrice*/


/*Switch case for each operation*/  
   switch (matrix_operations)

   { 
           case 1:/*addition function*/
           {
           printf("Enter elements of Matrix 1:\n");
           scanf("%d",& int a[3][3]);
           printf("Enter elements of Matrix 2:\n");
           scanf("%d",& int b[3][3]);
           matrix_add(a,b,c)
           }
               break; 
           case 2:/*subtraction function*/
           {
           printf("Enter elements of Matrix 1:\n");
           scanf("%d",& int a[3][3]);
           printf("Enter elements of Matrix 2:\n");
           scanf("%d",& int b[3][3]);
           matrix_subtract(a,b,c)
           }
               break; 
           case 3:/*multiplication function*/
           {
           printf("Enter elements of Matrix 1:\n");
           scanf("%d",& int a[3][3]);
           printf("Enter elements of Matrix 2:\n");
           scanf("%d",& int b[3][3]);
           matrix_multiply(a,b,c)
           }
               break; 
           case 4:/*transpose function*/
           {
           printf("Enter elements of Matrix 1:\n");
           scanf("%d",& int a[3][3]);
           matrix_transpose(a,c) 
           }
               break; 
           default: 
           {
           printf("End of Program!\n");  
           exit( 1 );
           }

    } 

system("pause");
return 0;   
}

/*Function definition*/
int matrix_add(int a[3][3], int b[3][3], int result[3][3])
{
   int i, j;
   for(i=0; i<3; i++)
   {
     for(j=0; j<3; j++)
     {
        result[i][j] = a[i][j] + b[i][j];
     }
   }
   return (a+b);
} 

int matrix_subtract(int a[3][3],int b[3][3], int result[3][3])
{
     int i,j;
     for (i=0;i<3;i++)
     {
         for(j=0;j<3;j++)
         {
           result[i][j]= a[i][j]- b[i][j];
         }
     }
     return (a-b);
}

int matrix_multiply(a[3][3],b[3][3],int result[3][3])
{ 
     int i,j,k;
     {
     for (i = 0; i < n; i++)
                  for (j = 0; j < n; j++)
                        for (k = a[i][j] = 0; k < n; k++)
                        {
                             result[i][j] += b[i][k] * c[k][j];
                        }
     }
     return (a*b);
}

int matrix_transpose(int a[3][3],int result[3][3])
{
     int i,j,n,temp;
     {
     for (i = 0; i < n-1; i++)
                  for (j = i+1; j < n; j++)
                  {
                        temp = b[i][j];
                        b[i][j] = b[j][i];
                        b[j][i] = temp;
                  }
     }
     return (b);
}
int print_matrix(int c[3][3])
{
   int i, j;
   for (i=0; i<3; i++)
   {
     for (j=0; j<3; j++)
     {
        printf("%d\t", a[i][j]);
     }
     printf("\n");
   }
}
}

i have done some changes in the code. pls guide to correct it as im unable to compile it. previously i used void which indicates the function does not receive any parameters. so i have done changes in function prototype with return values.pls advice me on this. thank you.

Off the top -- watch out for unintended assignment.

while ( counter= 0 )

Comparison is == .

int matrix_add(int)
{
   int i, j;
   for(i=0; i<3; i++)
   
	 for(j=0; j<3; j++)
	 
	    result[i][j] = a[i][j] + b[i][j];
	 
   
   return (a+b);
 
}

i have correted my coding and i found that there is error on this function definition. with the message expected primary-expression before"int" and expected ';' before "int". it is indicating first 2 lines.what is the correction need to be done?

The problem is something before that function.

As a sanity check you should get into the habbit of always using braces even when not required by the compiler. It will help prevent a lot of bugs.

int matrix_add(int)
{
   int i, j;
   for(i=0; i<3; i++)
   {   
	 for(j=0; j<3; j++)
	 {
	      result[i][j] = a[i][j] + b[i][j];
          }
   }	 
   
   return (a+b);
 
}

good job guys.
your discussions here are very helpful.
lucky to land on ur sight. =)

may ur good deeds return hundred folds...

commented: Yeah, and thanks for reading the rules about digging up dead threads -4
commented: Clutter +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.