C programming help nedded ?what is the wrong of this code?

C programming help nedded ??what is the wrong of this code?

#include<stdio.h>
int matA[3][4],matB[4][3],matAB[3][3],i,j,k;
void multiply();
void display();
int main()
{
multiply();
display();
return 0;
}

{

for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
scanf("%d",&matA[j]);
}}

for (i=0;i<4;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&matB[j]);
}}
}

void multiply()
{
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
matAB[j]=0;
for (k=0;k<4;k++)
{
matAB[j]=matAB[j]+matA[k]*matB[k][j];
}}}}

void display()
{
for (i=0;i<3;i++);
{
for (j=0;j<3;j++);
{
printf("%d",matAB[j]);
}
printf("\n");
}}

i wrote this multiply matA 3*4 by matB 4*3 ...........its compiling but doesn't give me the required output??/it gives me a 0..........i dont know how to find the error ...........im totally stuck can someone please check this code and find the error for me ........

Aside from the code not being very well-written, here is the problem:

"for (i=0;i<3;i++);
{
for (j=0;j<3;j++);
{
printf("%d",matAB[j]);
}
printf("\n");
}}"

Try the following:

"
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf("%d",matAB[j]);
}
printf("\n");
}}"

The semicolons after the "for" statements basically kill the looping... so there is no looping at all, it sees the semicolon and assumes that's the loop. It's a no-op loop.

yr yr i got it ...............thanks alot .........anyway im a newbie to programming .these are my first programmes ..so u have said that the "code is not well written".can u please show me the places where i have used bad methods or wrong algorithms .if u can do it it will be a great help.

Since you asked, I would suggest you write your program with proper indenting. You may already have done so, but if you post with code delimiters bracketing your code, the indents will show, and will help others appreciate your code in less time.

for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
scanf("%d",&matA[j]);
}}

you write:
Code:
  for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
{
scanf("%d",&matA[i][j]);
}
}

In my case I would forget the braces when there is only one line after the loop (but may be objectionalble to many people):
Code:
  for(i=0;i<3;i++) for(j=0;j<4;j++)  scanf("%d",&matA[i][j]);

sylas

yr yr i got it ...............thanks alot .........anyway im a newbie to programming .these are my first programmes ..so u have said that the "code is not well written".can u please show me the places where i have used bad methods or wrong algorithms .if u can do it it will be a great help.

The indentation idea is really crucial. The layout of a program doesn't affect how it compiles, but it makes a huge difference for reading and maintaining it. (And contrary to mathmate, I strongly advise being absolutely consistent on indentation, with steely determination. Include braces {} every single time you have a loop or an if statement -- as you have done.)

Another thing is that you should start using some local variables. That is, put variable definitions inside your functions, instead of all at the top.

Ultimately you will want to use parameters, but for now you can leave the arrays matA, matB and matAB at the top of the program for simplicity.

But variables i, j and k would be better defined inside each function where they are used. This prevents problems with unintended interference and side effects. It doesn't matter here... but it will, it will...

Cheers -- sylas

yr these ideas are so helpful to me for improving my programming knowledge ......but ..............i dont know how to set indentations in the programme.i use KWRITE for writing my C programmes .so can u please tell me that is there an easy way to put indentation to the program without applying them manually one by one.

There are products on the market for formatting source code from external sources.
If you want to pursue a career in programming, you are better off learning how to do it manually, using spaces or tabs, so that you know what you want.
Anyway, here are some suggested links:
http://en.wikipedia.org/wiki/Prettyprint
http://uncrustify.sourceforge.net/
I have not used these myself, so I cannot comment on the quality or reliability of these products. Google on 'c beautifier' and you will have a much wider choice.

yr these ideas are so helpful to me for improving my programming knowledge ......but ..............i dont know how to set indentations in the programme.i use KWRITE for writing my C programmes .so can u please tell me that is there an easy way to put indentation to the program without applying them manually one by one.

There's your problem: you're using a word processor to do the job of a text editor. They are very different things.

You should switch a real text editor right away. You already have one: you're on a KDE desktop (I infer), so instead of Kwrite, use Kate:

http://kate-editor.org/kate

http://kate-editor.org/files/images/kate-editing-homepage_0.preview.png [Broken]

Or get an IDE (integrated development environment) like NetBeans: (on the download, you only need the C/C++ modules)

http://www.netbeans.org/features/cpp/

http://www.netbeans.org/images/v6/5/screenshots/cpp-cut.png [Broken]

Whatever you do, stay away from "expert" editors like vi, vim, and emacs. These have extremely steep learning curves.

Last edited by a moderator:

And hey dude if you get errors try debuging by printlining. Add some statemnts that shows the state of the variables or at which line of execution your programs at.
I've learned to apreciate this method because it makes it easy to spot bugs and is way more fun that using a debugger.

Wel then cheers and happy coding :-)