r/C_Programming • • 4d ago

Question I need help understand what a question is asking me to do in uni exercise

I can't post an image to it, but it says in normal arrays : declare 3 arrays A, B, C all of size 5

The elements of C are the scaler products of A and B (∑︁A[i] × B[i])

And in 2d arrays :

C is there product, (matrix multiplication : C[i][j] = sum of A[i][k] * B[k][j] )

Can someone explain to me what they asking to make

1 Upvotes

7 comments sorted by

2

u/Due-Mortgage-6854 4d ago

Make 3 arrays: A, B, and C.

  • 1D: Multiply matching spots (C[i] = A[i] * B[i]).
  • 2D: Do matrix multiplication. Multiply row i of A by column j of B, add them up, and save in C[i][j].

1

u/Red_Bird57 4d ago

Thanks

1

u/alpicola 4d ago

Note that declaring C[i] = A[i] * B[i] will only compute C[i] once. There is no built-in way to declare that C[i] is always that product. That means if you make changes to A[i] or B[i], you also need to recompute C[i] at the same time.

1

u/Red_Bird57 4d ago

No worries about that A and B don't change values in the exercise, we are mostly doing beginners stuff

6

u/gnix0 4d ago

Since others already went straight to the point, I'll just leave a quick advice. On such problems, try to grab a piece of paper and sketch a little bit on it. Draw the matrices, try to get any little visual representation for helping out while you try to understand the problem and eventually move to the solution. Helped me a bunch when I took Linear Algebra, and became a habit I carry to this day!

1

u/ephemere_sloboda 1d ago

The exercise is about loops.

To calculate the scalar product, you need one loop with index i to cycle over the members of A and B.

To calculate matrix product, you need two nested loops (index i and j) to reach every position in the 2d array C, and a third nested loop (k) to swipe the row/column of A and B.