0

I am attempting to take a row from a 2d array, and multiply by a column in another 2d array. Then put those values into a third 2d array. The arrays being pulled from are called recipe[][] and prices[][]. The one I'm trying to put the values in is called forecastPrices[][].

Right now I just have:

for(int i = 0 ; i < numberOfProducts ; i++)
{
    for(int j = 0 ; j < numberOfProducts ; j++)
    {
        forecastCost[i][j] = someNumber;
    }

}

I know that this code only goes through the array and assigns some number to each point. but it's all I have

  • *Take a row from a 2d array, and multiply by a column in another 2d array*, would you be more specific about what do you mean by that? – Ranoiaetep Nov 21 '21 at 03:28

1 Answers1

0

Assuming all arrays have the same dimensions and type, you can use:

for(int i = 0 ; i < numberOfProducts ; i++)
{
    for(int j = 0 ; j < numberOfProducts ; j++)
    {
        forecastCost[i][j] = recipe[i][j] * prices[j][i];
    }

}

Where you loop through rows in recipe[i][j] and columns in prices[j][i]

Luke Trenaman
  • 306
  • 1
  • 6
  • they are not the same size, and I am trying to multiply rows from one array by columns of another. I have the appropriate values to traverse the other arrays, I am just stumped logically on how to do that. – s k y l i n e Nov 21 '21 at 02:45
  • You might be thinking of matrix multiplication: https://stackoverflow.com/questions/25250188/multiply-two-matrices-in-c I don't think it's possible to just multiply rows by columns if the dimensions are mismatched. – Luke Trenaman Nov 21 '21 at 15:57