#Why is each element of the first row in my array off by 0.5?

239 messages · Page 1 of 1 (latest)

rapid herald
#

I am preforming matrix multiplication;

void matrix_multiply(int m, int n, int k, double A[m][n], double B[n][k], double C[m][k]){
    
    int i; // feeling like an i day
    int v;
    int q;
    
    for(i = 0; i < m; i++){
        for(v = 0; v < k; v++){
            C[i][v] = 0; // clear any garbage
            for(q = 0; q < n; q++){
                C[i][v] += (A[i][q] * B[q][v]);
            } 
            
        }
    }
    
    

}```

of the following two 2d-arrays:
```c
    double M1[3][3] = { {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9} };

    double M2[3][3] = { {1, 1, 1},
                        {0, 2, 2},
                        {0, 0, 3}    };

And I get the following output: (see screenshot)

tired tundraBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

rapid herald
#

Like its only happens in that one case, every other time I try multiplying others it works

#

and is not off by 0.5

#

(this is what the output should be)

gaunt scroll
rapid herald
#

huh

#

but how

#

how would mine be broken

gaunt scroll
#

I have no idea, I plugged in your exact function. Idk if you do any weird subtraction when you print? Lol

rapid herald
gaunt scroll
#

Well, I would have to see print_matrix because otherwise I just see a black box (pun intended)

rapid herald
#

lol

gaunt scroll
#

What IDE or text editor are you using by the way, looks like it's in a terminal

rapid herald
#

its my schools custom made IDE

#

i tried restarting it

#

and it still had the same output

gaunt scroll
#

oh fancy. I like the contrast, wonder if I could do it with sublime

rapid herald
#

did it work for you?

gaunt scroll
#

I showed you the printout

rapid herald
#

sorry I forgot

#

I did not send

gaunt scroll
#

;compile

#include <stdio.h>

void matrix_multiply(int m, int n, int k, double A[m][n], double B[n][k], double C[m][k]){
    
    int i; // feeling like an i day
    int v;
    int q;
    
    for(i = 0; i < m; i++){
        for(v = 0; v < k; v++){
            C[i][v] = 0; // clear any garbage
            for(q = 0; q < n; q++){
                C[i][v] += (A[i][q] * B[q][v]);
            } 
            
        }
    }
    
    

}
void print_matrix(int rows, int columns, double M[rows][columns]){
    for(int i = 0; i < rows; i++){
        for(int j=0; j < columns; j++){
            printf("%7.2f ", M[i][j]);
        }
        printf("\n");
    }
}
int main(){
    double M1[3][3] = { {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9} };

    double M2[3][3] = { {1, 1, 1},
                        {0, 2, 2},
                        {0, 0, 3}    };
    double output[3][3] = {{0}};
    
    matrix_multiply(3,3,3,M1,M2,output);
    
    int num_rows = sizeof(output) / sizeof(output[0]);
    int num_cols = sizeof(output[0]) / sizeof(output[0][0]);
    
    print_matrix(num_rows,num_cols,output);
    return 0;
}
tight valveBOT
#
Program Output
1.00    5.00   14.00 
   4.00   14.00   32.00 
   7.00   23.00   50.00
#
Program Output
1.00    5.00   14.00 
   4.00   14.00   32.00 
   7.00   23.00   50.00
rapid herald
#

wtf

#

what in the actuall hecc

gaunt scroll
#

What lol

rapid herald
#

why does mine not work then wtf

gaunt scroll
#

Ah, yeah that's problematic isn't it

#

Have access to a debugger, or gdb?

rapid herald
#

no :9

#

:(

gaunt scroll
#

Umm how do you compile your code

#

Are you on windows or linux

rapid herald
#

im on windows but im compiling through a vm i think on linux

gaunt scroll
#

Using WSL or are you actually on a vm like VMware or something

rapid herald
#

I am not entirely sure

#

probs not VMware

#

idk

gaunt scroll
#

The entire application is proprietary university software?

rapid herald
#

yes

gaunt scroll
#

The virtual machine so to speak?

rapid herald
#

I guess so

gaunt scroll
#

Is there a shell or are you clicking on GUI shit

rapid herald
#

wait nvm sorry not allowed to leak that

#

i forgot

gaunt scroll
#

leak?

rapid herald
#

welll

#

idk if im alowed to share it idk

#

im sure its ok

#

maybe?

gaunt scroll
#

Well, sorry I already opened it, but it looks like you have a terminal (I call it a shell too)

#

You're using gcc! You are compiling your code manually

rapid herald
#

oh idk what that is

gaunt scroll
#

It's a whole other world amigo. But the shell does look some form of linux or something

#

Clean your program and compile again:
gcc -Wall -sdt=c18 -g3 -o vector_tester vector_tester.c matrix_and_vector.c -lm

#

Then afterwards:
gdb ./vector_tester

rapid herald
#

clean my program?

gaunt scroll
#

By the way, your school should teach you how to use makefiles or Cmake

rapid herald
#

eventually

gaunt scroll
#

rm *.o vector_tester
This should clean up all your object files and your executable

rapid herald
#

rm: cannot remove '*.o': No such file or directory

gaunt scroll
#

what's ls show?

rapid herald
#
rm: cannot remove 'vector_tester': No such file or directory```
#

oh wait

#

this will be under matrix_tester

gaunt scroll
#

Go to the directory you build your program in

#

Yeah glad I didn't make you remove random shit lmao

rapid herald
#

o wait thats my .h lol

gaunt scroll
# rapid herald this?

mi amigo, where is the directory you ran the command:
gcc -Wall -sdt=c18 -o vector_tester vector_tester.c matrix_and_vector.c -lm

#

You sent a screenshot earlier, I saw you have a terminal on the bottom of your screen. That had this command in it

#

You need to type in your terminal for me

rapid herald
#

ok im there

gaunt scroll
#

Run the command ls and show me the printout

#

Not the whole screen, just the terminal

rapid herald
#

there are like all my files ever

gaunt scroll
#

Okay

#

find . -type f -name "vector_tester*"

#

Run that command

rapid herald
#

it did something

gaunt scroll
#

what's the output amigo

rapid herald
#
./vector_tester.c```
gaunt scroll
#

Okay so you're in the directory. Looks like you don't have any object files and the executable vector_tester is not compiled

rapid herald
#

is that a W

gaunt scroll
#

Run the command in the same directory:
gcc -Wall -sdt=c18 -g3 -o vector_tester vector_tester.c matrix_and_vector.c -lm

rapid herald
#

gcc: error: unrecognized command-line option ‘-sdt=c18’; did you mean ‘-std=c18’?

#

wait wut

#

lol

#

ah

#

yes sorry

gaunt scroll
#

okay if you can't fix that, just delete -std=c18 idk what you need from that one

rapid herald
#

i see

#

sorry im just really tired

#

i saw it after i posted

#

rn im just brain off dumping everything

gaunt scroll
#

It's okay. Did you run it and did it work

#

find . -type f -name "vector_tester*" run this command again

rapid herald
#

it did not work

#

'tis the same

#

so there must be something randomly wrong with it

gaunt scroll
#

What is the gcc command saying?

rapid herald
#

nothing it just compiled normally

#

oh i meant like

#

the numbers

#

are still 0.5 off

gaunt scroll
#

Oh okay

#

We'll fix it. Run the find command again. I wanted to also have you run the program, but it's good you checked already

rapid herald
#

yay

#

we will get this

#

but like realisticly

#

how on earth can I even get a decimal point when multiplying whole numbers

gaunt scroll
#

I don't know, that's why we have a debugger

#

We can set things like watchpoints that should tell you when the value changes

#

Or we can step through it line by line and examine values

#

You need to find the path to the executable veector_tester which I believe is in your working directory.
Then run the command gdb ./vector_tester

rapid herald
#

btw

#

its on

#

matrix_tester

gaunt scroll
#

oh

rapid herald
#

vector testor is the first half of it

gaunt scroll
#

Okay well the important thing is, build everything with that -g3 flag I put in my initial command

#

Seems like you have another program I got confused with because your -o vector_tester is saying to link and make an executable file named vector_tester

#

So when you build your matrix program, make sure to also have the -g3. Otherwise debugger will have issues

rapid herald
#

okay

#

ill do that now

gaunt scroll
#

You might have to clean the files again, removing the object files and the executable. Idk

#

But just try what I said, and try running the gdb

rapid herald
#

whats gdb?

gaunt scroll
#

Another command, like gcc

rapid herald
#

it ran

gaunt scroll
#

gnu c compiler, gnu debugger

#

start

rapid herald
#

bash: start: command not found

#

D:

gaunt scroll
#

.. gdb should run, and then you should be in a new shell

#

start isn't a bash command (your terminal)

rapid herald
#

oh weird

gaunt scroll
#

Your terminal runs the gdb command and it opens a new shell. In your current terminal, run which gdb

rapid herald
#

it just compiled like normally

#

when I ran gdb

#

it never made a new shell

gaunt scroll
#

gdb doesn't compile, gcc compiles

rapid herald
#

ohh

gaunt scroll
#

did you run gdb ./matrix_tester

rapid herald
gaunt scroll
#

YES

#

THAT'S THE GDB SHELL

rapid herald
#

yaaay

#

ok now what do I do

gaunt scroll
#

Find the file name and line number that corresponds to this:

#

When you find that information, type this in the shell:
b FILE_NAME:LINE_NUMBER
Example: matrix_tester.c:42

rapid herald
#

matrix_and_vector.c

#

line 409

gaunt scroll
#

b matrix_and_vector.c:409

#

then run continue after

rapid herald
gaunt scroll
#

okay

#

start

#

then continue

#

or maybe you have to say run then continue

rapid herald
gaunt scroll
#

Okay look, you've stopped code execution at the line that's printed

rapid herald
#

wut

#

epic

gaunt scroll
#

you know what, let me show you some real magic

#

Actually we'll hold off on that, might use that later

#

Just run this:
p A[i][q]
p B[q][v]

rapid herald
#

two seperate lines or all in one?

#

probs a dumb question sorry

gaunt scroll
#

Then
p A[i][q]*B[q][v]

#

All separate lines

#

p C[i][v]

rapid herald
gaunt scroll
#

p i
p q

rapid herald
gaunt scroll
#

p A[0][0] @ len ?

rapid herald
gaunt scroll
#

Oh woops

#

Not len but 3

#

OH WAIT
p (double *)A[i][q] maybe that works??

rapid herald
gaunt scroll
#

p ((double (*)[3]) A)[i][q]

#

Okay I've never seen that syntax, and I'll admit that's a little bit cancerous

rapid herald
#

wait

#

so there is a 0.5 in memory

#

somewhere

#

i know a little bit about pointers

#

whats it doing in there

gaunt scroll
#

Yes, that's an element in your A array

#

The first one, at [0][0]

rapid herald
#

ah i see i see

gaunt scroll
#

Wait no, that screenshot.. where are you initializing these arrays??

#

I guess i don't see M1 and M2

rapid herald
gaunt scroll
#

That's very weird.
d 1
finish
in your shell

#

Then afterwords
p ((double (*)[3]) M1)[0][0]

rapid herald
#

this might be dumb but would running it on a different machine make a difference

gaunt scroll
#

So yeah just a sanity check, they are both saying the same thing. Same address, etcetera

rapid herald
#

no it wont

gaunt scroll
#

I mean, I ran it on two separate "machines" and got the "right" value, so idk probably

rapid herald
#

sorry yeah dumb question

#

its on a vm tth

#

tho

#

it did not make a difference

gaunt scroll
#

🤷‍♂️ Yeah we checked it out, your matrix in the source code says 1, but at runtime it's saying 0.5. I'd bring it up to your teacher, TA, or professor

#

Might need something more involved, I tried getting deep into it, weird bug. Maybe somebody else will read this at some point and give input

rapid herald
#

I can message my prof rn

#

but I wanna sound like I know what Im doing

#

so i need you to do my words for me

#

hahahahhaha

#

cus honestly idk what the hell we just did

gaunt scroll
#

Well figuring it out is my homework to you, good luck fam salute

rapid herald
#

shit

gaunt scroll
#

Everything I mentioned you can search online and it'll go in detail. you'll sound smarter because you'll be smarter, you got dis

#

I only speak from experience 😜

rapid herald
#

its due tomorrow and I have so much other stuff I gotta do

#

well

#

the quick fix

#

is to add 0.5 to everything

gaunt scroll
#

To everything? I can't think off the top of my head right now why the output would all be off by 0.5, but I think adjusting the inputs completely will be too much. Maybe one or two elements are off? Like the first one

#

Idk, good luck, experiment with it. Or say fuck it and email the prof

rapid herald
#

rip

#

well thanks for the help

#

well, like time spend trying to

#

!solved

tired tundraBOT
#

Thank you and let us know if you have any more questions!

rapid herald
#

but not really solved

gaunt scroll
#

I probably wouldn't have marked it solved lol. Sorry, weird bug

rapid herald
#

oh

gaunt scroll
#

idk who cares

rapid herald
#

me D;

gaunt scroll
#

message somebody else in class