#include<stdio.h>
#include<conio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void add(int a[][MAX_COLS], int b[][MAX_COLS], int result[][MAX_COLS]);
void displayValues(int(*)[MAX_COLS]);
void diff(int a[][MAX_COLS], int b[][MAX_COLS], int result[][MAX_COLS]);
void main()
{
int val;
int i,j;
int choice;
int cols,rows;
int operation;
int table1[MAX_ROWS][MAX_COLS];
int table2[MAX_ROWS][MAX_COLS];
int result[MAX_ROWS][MAX_COLS];
clrscr();
printf("Press 1 to enter values for Table1... ");
scanf("%d", &choice);
if(choice==1)
{
printf("Enter no.of rows: ");
scanf("%d", &rows);
printf("Enter no. of cols: ");
scanf("%d" ,&cols);
if(rows<=MAX_ROWS && cols<=MAX_COLS)
{
printf("\nTable Created\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("\nEnter value for ROW %d, COL %d: ",i,j); //user input row x col
scanf("%d", &val);
table1[i] [j]=val;
}
}
printf("\n");
} //END IF
}
printf("Press 2 to enter values for Table2... ");
scanf("%d", &choice);
if(choice==2)
{
printf("Enter no.of rows: ");
scanf("%d", &rows);
printf("Enter no. of cols: ");
scanf("%d" ,&cols);
if(rows<=MAX_ROWS && cols<=MAX_COLS)
{
printf("\nTable Created\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("\nEnter value for ROW %d, COL %d: ",i,j); //user input row x col
scanf("%d", &val);
table2[i][j]=val;
}
}
printf("\n");
} //END IF
}
printf("Generated table......\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%3d ", table1[i] [j]);
}
printf("\n");
}
printf("\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%3d ", table2[i] [j]);
}
printf("\n");
}
/////////*** FOR THE MENU*****/////
printf("\n");
printf("---MENU---\n");
printf("1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication\n");
printf("Enter Operation: ");
scanf("%d", &operation);
switch(operation)
{
case 1: printf("The sum of 2 matrices is:\n");
add(table1,table2,result);
break;
case 2: printf("The Difference of 2 matrices is:\n");
diff(table1,table2,result);
break;
}
getch();
} //END MAIN
void add(int a[][MAX_COLS], int b[][MAX_COLS], int result[][MAX_COLS])
{
int i,j;
for (i=0;i<MAX_ROWS;i++)
{
for(j=0;j<MAX_COLS;j++)
{
result[i][j]=a[i][j]+b[i][j];
}
}
displayValues(result);
}
void displayValues(int(*t)[MAX_COLS]) //read and display elements
{
int i,j;
for(i=0;i<MAX_ROWS;i++)
{
for(j=0;j<MAX_COLS;j++)
{
printf("%4d ", t[i][j]);
}
printf("\n");
}
}
void diff(int a[][MAX_COLS], int b[][MAX_COLS], int result[][MAX_COLS])
{
int i,j;
for(i=0;i<MAX_ROWS;i++)
{
for(j=0;j<MAX_COLS;j++)
{
result[i][j]=a[i][j]-b[i][j];
}
}
displayValues(result);
}
***when i run it, the 3rd matrix displays a lot of numbers, and i still have to multiply them. please help.