The line: Amount_Req <-- Num_Ord * Product_Recipes(Product_ID, Ingredient_ID) is what is confusing me.
In theory the array structure is (1,2) for Product_Receipes. However in the test data we aren't given (1,2) as variable, instead we are given (2,1). Is this an error in the code or am i deskchecking this incorrectly. If i were to use the test data (2,1) i'd return this answer:
Yeah I got to the stage with Product_Recipies(1,2).
I think there's a mistake with the test data given, given that they're all in the form (n,1), I'm assuming that 1 is the product ID. Given the code Product_Recipes(Product_ID, Ingredient_ID) and the fact that it'd make more sense to have Product_ID first (given a product, retrieve the ingredients).
Or the mistake could be with the line Product_Recipes(Product_ID,Ingredient_ID), where it should have been Product_Recipes(Ingredient_ID,Product_ID).
Whether we switch the bit in the code around, or the test data around, it doesn't matter. I'm going to switch the test data around to be in the form (1,n) and leave the code as given.
Product_Recipes(1,1) 0.15
Product_Recipes(1,2) 0
Product_Recipes(1,3) 0.20
What does the code do then?
In the first loop we set Qty(1), Qty(2), Qty(3) all to zero. (Qty would be an array)
What does this mean? Maybe it means that we don't have any of the ingredients at the start, or maybe it's just initialising the array (or both).
Then we get to the next loop, and Ingredient_ID is set to 1. Then we enter a while loop. Ingredient_ID is incremented. This is probably another bug in the code, we skip over Ingredient 1. I'm assuming these kind of errors are there on purpose and that we're supposed to pick them out.
Amount_Req <-- Num_Ord * Product_Recipes(1,2)
Amount_Req <-- 10 * 0 = 0Since Product_Recipes(1,2) is zero, which implies that Ingredient 2 is not required. I think that reasoning makes sense.
Qty(Ingredient_ID) <-- Qty(Ingredient_ID) + Amount_Req
Qty(2) <-- 0 + 0
Qty(2) <-- 0If the ingredient was required, we would add the values of the amount that we already have and the amount that we need. This is assuming that the first loop wasn't simply just initialising the array, but actually setting values for how much of each ingredient we had (and that happened to be 0).
I'm not too sure what this final Qty(Ingredient_ID) value actually represents (is it simply the amount we require? Surely it's not that because we could just use Amount_Req or just not bother to add Qty(Ingredient_ID) + Amount_Req together. I actually think it would make more sense to check if Qty(Ingredient_ID) >= Amount_Req to figure out we have enough of that particular ingredient on us or not, and then output information about how much more of a particular ingredient we need to get.
Then we hit, UNTIL Ingredient_ID = Num_Ingredients, this evaluates false so we repeat.
Ingredient_ID is incremented,
Amount_Req <-- Num_Ord * Product_Recipes(1,3)
Amount_Req <-- 10 * 0.20
Amount_Req <-- 2Qty(Ingredient_ID) <-- Qty(Ingredient_ID) + Amount_Req
Qty(3) <-- 0 + 2
Qty(3) <-- 2Here Ingredient_ID = Num_Ingredients so we exit the loop.
Then we do that again until we finish reading through the entire file?