14,388,640 members
Rate this:
See more:
i don't know what's wrong with my code

What I have tried:

```#include <stdio.h>
#include <stdlib.h>

int main()
{
int a1,a2,b1,b2,i,j;
printf("enter size of arrays ::\n");
scanf("%d%d%d%d",&a1,&a2,&b1,&b2);
while(a2!=b1) // condition of matrices multiplication//
{
printf("enter suitable dimensions!!\n");
scanf("%d%d%d%d",&a1,&a2,&b1,&b2);
}
int A[a1][a2];
int B[b1][b2];
int C[a1][b2];
// input elements of array//
printf("enter elements of first array row by row ::\n");
for(i=0; i<a1; i++)
{
for(j=0; j<a2; j++)
{
scanf("%d",&A[a1][a2]);
}
printf("\n");
}
printf("enter elements of second array row by row ::\n");
for(i=0; i<b1; i++)
{
for(j=0; j<b2; j++)
{
scanf("%d",&
B[b1][b2]);
}
printf("\n");
}
// three counters to iterate through three matrices //
int element=0,k=0;
for (i=0; i<a1; i++)
{
for (j=0; j<b2; j++)
{
while(k<b1)
{
element+=(A[i][k]*B[k][j]);
k++;

}

C[i][j]=element;
element=0;
k=0;
}

}
printf("the product matrix is ::");
for(i=0; i<a1; i++)
{
for(j=0; j<b2; j++)
{
printf("%d  ",C[i][j]);
}

}

return 0;
}```

Posted
Updated 20-Oct-17 10:55am
[no name] 20-Oct-17 14:03pm

Can you please mention at least what you
a.) Expect to get?
b.) And what you get?
And why you are not happy with the difference of a.) and b.)
.....

Rate this:

## Solution 3

Quote:
scanf("%d",&A[a1][a2])
Should be
`scanf("%d", &A[i][j]);`
likewise
Quote:
scanf("%d",&B[b1][b2]);
Should be
`scanf("%d", &B[i][j]);`
Rate this:

## Solution 1

Compiling does not mean your code is right!
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
```Input   Expected output    Actual output
1            2                 1
2            4                 4
3            6                 9
4            8                16```
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
```int Double(int value)
{
return value * value;
}```

Once you have an idea what might be going wrong, start using teh debugger to find out why. Put a breakpoint on your line:
```int element=0,k=0;
```

and run your app in the debugger. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
Rate this:

## Solution 2

Quote:
i don't know what's wrong with my code

Then watch your code executing and see what match your expectation, and what it don't. The debugger is here exactly for this.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.