3 for loops for multiplying arrays

Dec 24, 2019 at 5:07pm
closed account (1Ck93TCk)
Hi all, I'm working on a program that uses two arrays - the first array is 2d and has two columns of numbers. I'm supposed to populate the second array by multiplying the two numbers of each row and input them to the corresponding row of the second array. I've tried the code below (which I found on a different forum), and it's giving me garbage numbers.

I hope someone can explain the loops to me a little better. I understand the outer loop is for rows and the middle loop is for columns, but I'm not wrapping my head around what the inner loop does and why this can't be done with just two loops.

Thanks in advance for any help with this!!

jmb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 void calcWeekly(double payData[EMP_QTY][COL], double weeklyPay[EMP_QTY])
{
    int i = 0;
    int j = 0;
    int k = 0;

    for (int i = 0; i < EMP_QTY; i++)
    {
        for (int j = 0; j < COL; j++)
        {
            weeklyPay[i] = 0;
            
            for (int k = 0; k < EMP_QTY; k++)
            {
            weeklyPay[i] += payData[i][k] * payData[k][j];
            }

      cout << weeklyPay[i] << endl;
      }
}
}

Here's the data file I'm reading from:
1
2
3
4
5
6
7
8
9
10
30 15.43
42 17.34
35 16.23
36 17.00
38 18.00
23 15.34
33 16.55
40 18.36
34 17.97
28 18.23

Dec 24, 2019 at 5:41pm
if you are not sure of what you need to be doing, here is the math: https://www.mathsisfun.com/algebra/matrix-multiplying.html

if you can do that in 2 loops, go ahead. It is usually possible to collapse loops, often all the way down to a single loop, but this makes the code incomprehensible more often than not.

Dec 25, 2019 at 6:57pm
closed account (1Ck93TCk)
Thank you for that link!! I haven't had the time to sit down and figure out my program, but I took a look at the link and I think it's exactly what I've been looking for.
Dec 26, 2019 at 9:39am
@jmb, for your given input data what exactly do you expect the output to be?

Both "your" code and @jonnin's link correspond to matrix multiplication. That is all well and good, but it doesn't correspond to
populate the second array by multiplying the two numbers of each row and input them to the corresponding row of the second array
.

Just read the file line by line until the file is exhausted, multiplying two numbers together and pushing them back into a vector. That requires one loop, not three; one vector, not two arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

vector<double> getData( istream &in )
{
   vector<double> result;
   double hours, rate;
   while( in >> hours >> rate ) result.push_back( hours * rate );
   return result;
}

int main()
{
// ifstream in( "data.txt" );
   istringstream in( "30 15.43\n"
                     "42 17.34\n"
                     "35 16.23\n"
                     "36 17.00\n"
                     "38 18.00\n"
                     "23 15.34\n"
                     "33 16.55\n"
                     "40 18.36\n"
                     "34 17.97\n"
                     "28 18.23\n" );

   vector<double> pay = getData( in );
   for ( auto p : pay ) cout << fixed << setprecision( 2 ) << p << '\n';
}


462.90
728.28
568.05
612.00
684.00
352.82
546.15
734.40
610.98
510.44

Last edited on Dec 26, 2019 at 10:01am
Dec 26, 2019 at 2:05pm
I've tried the code below (which I found on a different forum), and it's giving me garbage numbers.
Never use code from the internet unless you understand how it works [edit: see jonnin's insights below] . As lastchance pointed out, the code you found does matrix multiplication, which is different from what you have. It won't even do that: line 15 accesses payday[i][k] where k is a row index and is being used to access columns.

You only need one loop. Something like this:
1
2
3
4
5
6
7
void calcWeekly(double payData[EMP_QTY][2], double weeklyPay[EMP_QTY])
{
    for (int i = 0; i < EMP_QTY; i++) {
      // Add code to compute weeklyPay[i] from payData[i][0] and payData[i][1]
      cout << weeklyPay[i] << endl;
      }
}
Last edited on Dec 26, 2019 at 8:20pm
Dec 26, 2019 at 7:47pm
Never use code from the internet unless you understand how it works.

Ill mildly disagree. If you can verify that it works completely, the how may not matter; I have a couple of SHA and MD5 and similar codes that I don't fully understand or care how it works down in the details but I verified that they give the same result as other tools that do the same thing, something you can't always do.

However, I suggest you try to write it yourself. Getting it off the web for your problem and validating it is going to be as much or more work than just doing it.
Dec 27, 2019 at 8:14pm
closed account (1Ck93TCk)
dhayden,

Thank you for simplifying things for me. I had a feeling I was making it way too complicated. I made the change that you suggested and all is working fine now. I've been doing practice programs to get comfortable working with arrays, and this forum has been incredibly helpful. Thanks everyone!

jmb
Topic archived. No new replies allowed.