This is my first post on stackoverflow, so forgive any formatting mistakes.
I have a project named BOGOTotal - basically, its job is to take a decimal array of prices, sort them in ascending order, and then determine which prices will be bought and which prices will be free. Anyways, that's just a bit of backstory, but I need something more specific.
I've got two arrays - one of the prices, and one of the quantities of those prices. For example, in my Items array (should have been named "prices"), Items[0] is set to 2.20m, and my Quantities array, Quantites[0] is set to 5. Meaning I have five of the same item that are priced at $2.20.
//create items array
decimal[] Items = new decimal[5];
Items[0] = 2.20m;
Items[1] = 1.50m;
Items[2] = 8.40m;
Items[3] = 4.60m;
Items[4] = 3.75m;
//create quantity array
int[] Quantity = new int[Items.Length];
Quantity[0] = 5;
Quantity[1] = 2;
Quantity[2] = 1;
Quantity[3] = 3;
Quantity[4] = 6;
I then had to sort the Items array in ascending order.
Array.Sort(Items);
However, when I sorted my Items array, the relation of each item to its quantity is now lost, because Items[0] and Quantity[0] no longer are related. Now, instead of Items[0] = 2.20m, it has become Items[0] = 1.50m. Now, I have 5 items that are $1.50 instead of $2.20.
Here's where I had my idea - I would go ahead and calculate the prices of the old arrays by creating a firstPrices array, putting them in a for loop, and saying
decimal[] firstPrices = new decimal[Items.Length];
//calculate prices before sorting - will match back up afterward
for (int i = 0; i < Items.Length; i++)
{
firstPrices[i] = Items[i] * Convert.ToDecimal(Quantity[i]);
}
Here comes the hard part: I'm trying to re-align (for lack of a better word) each quantity to its Item - meaning I'm trying to make the item that is $2.20 match back up with its correct quantity (being 5). However, I'm having a hard time doing this. I tried a nested for loop within another for loop that tested each quantity by multiplying it by the current Item and then comparing it to that spot in firstPrices:
//i would be newItems
//j would be quantity
for (int i = 0; i < Items.Length; i++)
{
for (int j = 0; j < Items.Length; j++)
{
if (Items[i] * Quantity[j] == firstPrices[i])
{
Quantity[i] = Quantity[j];
break;
}
}
}
When it found a match, I set it to break out of the nested loop and increment "i", which goes to the next item in the Items array. Is there something I'm doing wrong here?
structs orclasses, or evenTuples?