User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 429,898 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,348 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 215 | Replies: 3
Reply
Join Date: May 2008
Posts: 1
Reputation: nony0905 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nony0905's Avatar
nony0905 nony0905 is offline Offline
Newbie Poster

help w/ matrix!!please!!

  #1  
May 16th, 2008
#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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: help w/ matrix!!please!!

  #2  
May 16th, 2008
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?



.
Last edited by jephthah : May 16th, 2008 at 2:10 pm.
Why so serious?
Reply With Quote  
Join Date: Feb 2008
Location: Seattle
Posts: 713
Reputation: jephthah is a jewel in the rough jephthah is a jewel in the rough jephthah is a jewel in the rough 
Rep Power: 4
Solved Threads: 46
jephthah's Avatar
jephthah jephthah is offline Offline
Master Poster

Re: help w/ matrix!!please!!

  #3  
May 16th, 2008
oh, and by the way.

welcome to Daniweb.

Why so serious?
Reply With Quote  
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 287
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 24
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz in Training

Re: help w/ matrix!!please!!

  #4  
May 16th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 9:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC