Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 05-06-2005, 10:44 AM   #1
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
matrices with C

I created this program that is supposed to read two 3x3 matrices and then sum and rest the values of the first matrix against the second and store the results y another two matrices, finally it has to print the resulting matrices, when I compile the program it doesn`t find any errors but the program does not promt for the value of the firt two matrices

# include
# include

void main (void)

{
int a,b;
int e[3][3];
int f[3][3];
int g[3][3];
int h[3][3];
clrscr();

printf ("\n Programa que suma y resta dos matrices");
printf ("\n Digite los datos");
for (a=1; a>=3; a++)
{
for (b=1; b>=3; b++)
{
printf ("\n e[%d][%d]= ",a,b);
scanf ("%d",&e[a][b]);
}
}
for (a=1; a>=3; a++)
{
for (b=1; b>=3; b++)
{
printf ("\n f[%d][%d]= ",a,b);
scanf ("%d",&f[a][b]);
}
}
for (a=1; a>=3; a++)
{
for (b=1; b>=3; b++)
g[a][b]=e[a][b]+f[a][b];
}
for (a=1; a>=3; a++)
{
for (b=1; a>=3; b++)
h[a][b]=e[a][b]-f[a][b];
}
printf ("\n La suma de las matrices es igual a: ");
for (a=1; a>=3; a++)
{
for (b=1; a>=3; b++)
printf ("\n g[%d][%d]",a,b);
}
printf ("\n La Resta de las matrices es igual a:");
for (a=1; a>=3; a++)
{
for (b=1; b>=3; b++)

printf ("\n h[%d][%d]",a,b);
}
getch ();
}
easg is offline   Reply With Quote
Old 05-06-2005, 11:32 AM   #2
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
for (a=1; a>=3; a++)
for (b=1; b>=3; b++)


All your "for loops" are wrongly written:

">=" means greater than or equal to

in your case
a>=3
or
b>=3
will create an infinite loop!
a and b will have 1 added to it infinitely!

you should have used

a<=3
or
b<=3
or
a<4
or
b<4

alfie2 is offline   Reply With Quote
Old 05-06-2005, 12:27 PM   #3
Member (9 bit)
 
Join Date: Nov 2002
Posts: 502
That was the problem, sometimes the little things has the anwers, so I am not so lost in c programming, right?


# include
# include

void main (void)

{
int a,b;
int e[3][3];
int f[3][3];
int g[3][3];
int h[3][3];
clrscr();

printf ("\n Programa que suma y resta dos matrices");
printf ("\n Digite los datos");
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)
{
printf ("\n e[%d][%d]= ",a,b);
scanf ("%d",&e[a][b]);
}
}
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)
{
printf ("\n f[%d][%d]= ",a,b);
scanf ("%d",&f[a][b]);
}
}
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)
g[a][b]=e[a][b]+f[a][b];
}
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)
h[a][b]=e[a][b]-f[a][b];
}
printf ("\n La suma de las matrices es igual a: ");
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)
printf ("\n g[%d][%d], [%d] ",a,b,g[a][b]);
}
printf ("\n La Resta de las matrices es igual a:");
for (a=0; a<=2; a++)
{
for (b=0; b<=2; b++)

printf ("\n h[%d][%d], [%d]",a,b,h[a][b]);
}
getch ();
clrscr ();
}
easg is offline   Reply With Quote
Old 05-07-2005, 04:20 AM   #4
Member (9 bit)
 
Join Date: Feb 2005
Posts: 392
not so lost, at all.
I can pick this sorta things up because I am in the programming business for 20 odd years, doh, I gave up my age.
started programming with FORTRAN(when I was studying to be an electronic engineer), then PASCAL,C,Smalltalk...C++ so on; learning never stops, there is always something new.

Last edited by alfie2; 05-07-2005 at 04:37 AM.
alfie2 is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:22 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2