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

Click For Summary
SUMMARY

The forum discussion focuses on a C programming issue where the user encounters a problem with matrix multiplication code. The primary error identified is the presence of semicolons after the for-loop declarations, which disrupts the intended looping behavior. The discussion also emphasizes the importance of proper code indentation and suggests using a suitable text editor like Kate or an IDE like NetBeans for better coding practices. Additionally, it highlights the need for local variable definitions within functions to avoid unintended interference.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Knowledge of matrix operations in programming
  • Familiarity with debugging techniques in C
  • Experience with text editors and IDEs for coding
NEXT STEPS
  • Learn about C programming best practices for code readability
  • Explore the use of local variables in C functions
  • Research debugging techniques in C, including print debugging
  • Investigate text editors and IDEs suitable for C programming, such as Kate and NetBeans
USEFUL FOR

Beginner C programmers, students learning matrix operations, and anyone looking to improve their coding practices and debugging skills in C.

rclakmal
Messages
76
Reaction score
0
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 read();
void multiply();
void display();
int main()
{
read();
multiply();
display();
return 0;
}

void read()
{
printf("Enter your matrix A:\n");

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

printf("Enter your matrix B:\n");

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()
{
printf("Here is your multiplied matrix:\n");
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 don't know how to find the error ...im totally stuck can someone please check this code and find the error for me ...
 
Technology news on Phys.org


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 a lot ...anyway I am 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 example, instead of:

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]);
 


rclakmal said:
yr yr i got it ...thanks a lot ...anyway I am 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 don't 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.
 
rclakmal said:
yr these ideas are so helpful to me for improving my programming knowledge ...but .....i don't 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

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

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 :-)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
Replies
47
Views
5K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
14
Views
3K
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K