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

Recommended Answers

All 3 Replies

You know, I was going to help you, because this is an easy problem...

But then i get here and see not a single code block or conditional statement is indented. I mean, look at your code, look at all those opening and closing braces lined up like little tiny firing squad victims against a wall.

Does this look good to you? Does this look readable? And you're not the only one, by any stretch ... this happens all the time. What is wrong with you people?

More importantly, what is wrong with your instructors? What kind of professional lets you get away with writing code like this?

Or am i just going insane? Can you hear the sound of my head banging against particle board?

.

oh, and by the way.

welcome to Daniweb.

:P

Well, I would like to start by giving the same advice I recived here. Dont use conio.h and the turbo c compiler.
And Never Ever Use void main() search void main in this website & you woud know why.

And as for your question, you read values till rows & cols & print the answers till MAX_ROWS & MAX_COLS

And finaly, you should learn to intend as jephthah said It will help you a lot.

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.