Help with C++ Matrix Calculator Printing Wrong Results

0
42
Asked By CodeWizard42 On

I'm trying to build a matrix calculator in C++, but I'm getting some weird results when I print the outputs. Can someone explain why this might be happening? Here's a snippet of my code: In the main, I call the `funcion1` function and am expecting to prompt the user for sizes of two matrices, followed by the values of those matrices, and then I want to multiply them together. However, when I input small values for matrices like 1x1 or 2x2, the results are unexpectedly massive, often exceeding a million! What could be going wrong?

3 Answers

Answered By SyntaxSorcerer88 On

It sounds like you might have a bug in how you're indexing your arrays. In this part of your `funcion1`:

```cpp
for(int i=0; i<c1;i++){
for(int j=0; j<f1;j++){
cin>>matriz1[f1][c1];
}
}
```
You're using `f1` and `c1` as indexes which are meant to store the size, not the current index value. You should be indexing with `i` and `j`. Try changing that to:

```cpp
cin>>matriz1[i][j];
```
This should help fix the values of the matrices being read in.

User12345 -

That makes sense! I was confused about what to use for indexing. I'll update my code and see if I still have the problem.

Answered By DebugDude101 On

Definitely check the dimensions of your matrices before multiplying. In your code, it looks like you're checking if `c1` (columns of first matrix) equals `f2` (rows of second matrix) before multiplication, which is good. But make sure that during the actual computation you're using the correct indices in your multiplication logic.

Answered By MatrixMaster21 On

Another potential issue could be that your multiplication logic isn't set up correctly. After filling in your matrices, make sure that when you're accessing the values, the dimensions match for multiplication. Just a tip: always double-check your loop limits as well as the index values when summing the products!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.