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

In summary, the program provided is attempting to multiply two matrices but is not giving the correct output. The problem lies in the display function, where the semicolons after the "for" statements are preventing proper looping. Additionally, the code could benefit from better indentation and the use of local variables. It is recommended to use a text editor or IDE specifically designed for programming, rather than a word processor, and to use printlines for debugging purposes.
  • #1
rclakmal
76
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
  • #2


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


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


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]);
 
  • #5


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
 
  • #6


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


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.
 
  • #8
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:
  • #9


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

1. What is the purpose of C programming?

C programming is a popular computer language used for developing various software applications and operating systems. It is a powerful and efficient language that allows for low-level manipulation of computer hardware, making it ideal for tasks such as system programming and device drivers.

2. What are some common errors in C programming?

Some common errors in C programming include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code violates the rules of the language and cannot be compiled. Logical errors refer to mistakes in the program's logic, causing it to produce incorrect results. Runtime errors occur during program execution and can lead to crashes or unexpected behavior.

3. How can I improve my C programming skills?

To improve your C programming skills, it is essential to practice regularly and work on various projects. Reading books and online resources can also help you understand different concepts and techniques. Additionally, you can join online communities or attend programming workshops to learn from experienced programmers and exchange ideas.

4. How can I debug my C code?

Debugging is the process of finding and fixing errors in your code. In C programming, you can use tools such as debuggers or print statements to identify and trace the source of the error. It is also helpful to break down your code into smaller sections and test each one individually to locate the bug.

5. How can I get help with my C programming assignments?

If you need help with your C programming assignments, you can seek assistance from online resources, such as tutorials, forums, or programming communities. You can also ask your peers or instructors for guidance. It is important to understand the concepts and try to solve the problem yourself before seeking help.

Similar threads

  • Programming and Computer Science
Replies
4
Views
602
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
1
Views
909
Back
Top