Error in inner product of vectors and index

Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to calculating the inner product of vectors derived from two matrices in C++. Participants explore debugging techniques and logic errors in the code, specifically focusing on the indexing and loop conditions used in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes a bug in their code involving the inner product calculation between rows of two matrices, A and B.
  • Another participant suggests using print statements or a debugger to track variable values during execution to identify the error.
  • A participant expresses difficulty in understanding the code logic and seeks hints on potential issues.
  • Concerns are raised about the loop condition where the index variable x is set to start at 1, with a suggestion that it should iterate up to 5 instead of 4.
  • A later reply confirms that changing the loop condition to x<5 resolves the issue, leading to correct output.
  • Participants discuss the importance of understanding the algorithm and the value of debugging practices for learning programming.

Areas of Agreement / Disagreement

Participants generally agree that the loop condition was incorrect and that debugging practices are essential. However, there is no consensus on the initial understanding of the algorithm or the debugging process, as some participants struggled with the logic.

Contextual Notes

Participants mention the need for complete code for effective debugging, highlighting that code fragments can lead to misunderstandings. There are also references to potential logic errors that may not be easily identified through print statements alone.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in debugging techniques and matrix operations, may find this discussion beneficial.

Herbststurm
Messages
30
Reaction score
0
Hello

I found a bug in my code and can't figuring out the error. I tried debugging by showing the output of each variable step by step but I can't find my error. Here is what I have and what I want to do:

I have a matrix A:
0000
0101
1010
1111

And I have a matrix B:
10000
21000
30100
41100
20010
21010
40110
41110
30001
41001
30101
41101
40011
41011
40111
41111

The matrix B has 16 rows and 5 coloumns. The matrix A has 4 rows and 4 coloumns. Now I declare a matrix C that has 4 rows and 16 coloumns.

What I want to do is to calculate the inner product of each row from B with a corresponding row from A. With corresponding I mean that the first coloumn of B shoud define the row from A that I want to multiply. So the B matrix has in fact also four-dimensional vectors and the first element corresponds to the row of A. One could say this first coloumn of B is an index for choosing the row of A. Because C++ start counting by zero I substract one for my index. Here is my code:
Code:
std::vector< std::vector<int> > C(4, std::vector<int>(16));
std::vector<int> index(4);
std::vector<int> vectorA(4);
std::vector<int> vectorB(4);

for( int y = 0; y < 16; y++)
{
     
    for(int i=0; i<4; ++i){
    vectorA[i] = A[ B[y][0]-1 ][i];
    }

    for( int x = 1; x < 4; x++)
    {

        vectorB[x -1] = B[y][x];

    }C[ B[y][0] -1][index[ B[y][0] -1] ] = inner_product(vectorA.begin(), vectorA.end(), vectorB.begin(), 0);

    index[B[y][0]-1] += 1;
}
This results in my matrix C:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 3 1 2 1 2 2 3 0 0 0 0 0 0 0

The first two rows are correct but row three and four are false.
The correct solution has to be (maybe except of ordering in row 3 and 4):

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
4 3 3 2 3 2 3 2 2 0 0 0 0 0 0 0

Where is my problem? Please help, it drives me crazy :(

Thanks and greetings.
 
Technology news on Phys.org
Why not place print statements inside the for loops and let the computer show you what it is doing at each step or better yet if you have a debugger step through your code and compare your understanding of the process with what the computer is doing?
 
Hi

I did exactly this but can't figure it out. I guess there must be a problem in my coding logic?
 
What I'm saying is you need to vet each value the computer prints out and agree that its what you expect to see. Only then will you either find your error or realize that maybe your understanding of the algorithm is wrong.
 
I have to learn a lot and will spend more time with this. I understand what you mean and tried it. Can you give me a hint what could be wrong in my code? For someone who knows C++ this should be an easy beginners question?
 
Not understanding your code but the loop where you set x=1 shouldn't it use x<5?
 
Herbststurm said:
For someone who knows C++ this should be an easy beginners question?

It might be an easy question if you give a complete program that can be compiled. It's hard to debug code fragments.
 
@jedishrfu

You are totally right. With x<5 the program works correct. Now it makes sense. But I have to say I am a little sad that I can't figure this out by printing the variables step by step :( I think I will write little programs where I know exactly what's happening and make explicit error in the loops for learning and studying debugging. Thanks for help
 
Yes, that's an excellent way to learn a language usually though it will be compile mistakes that occur.

However, there are some logic errors like this that can be exasperating but you collect these gems in your head and vow not to make the same error again.

In your problem here, I would have printed the loop values and the other values so that I could step thru each operation and then it might have occurred to me that wait I didn't use all the elements of B.

Also I'd suggest learning to use a debugger that way you an skip the print statements and focus on your code. This also eliminates the potential problem of a bug in your print statements.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K