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

1
  • 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? Commented Nov 21, 2021 at 3:28

1 Answer 1

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]

Sign up to request clarification or add additional context in comments.

2 Comments

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.
You might be thinking of matrix multiplication: 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.