0

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?

4
  • 2
    Are you allowed to create struct s or classes, or even Tuples? Commented Apr 3, 2017 at 18:21
  • The best thing to do would be to initialize the values in your 2 arrays in order. I can't give you code because I don't know C# but for the logic: make a copy of your prices array, say pricesUnordered. after re-arranging price, loop over it and do something like for( int i = 0; i < Items.length; i++) { oldIndexOfMyValue = pricesUnordered.indexOf(value); newQuantity.push(Quantity[oldIndexOfMyValue]); } Commented Apr 3, 2017 at 18:30
  • Johnny Mopp, technically I am, but I've never worked with structs or Tuples - I'll look into those later today, thanks. Commented Apr 3, 2017 at 19:26
  • FrenchMajesty, your logic is interesting - I'll definitely take a closer look at it when I get home from work, thank you. Commented Apr 3, 2017 at 19:27

1 Answer 1

1

You got two seperate arrays that are related, wich is not a good thing. Make a Struct, Class or Tupel containing both values. Optionally also a custom comparer. Make a array of that type.

Then it would be as simple as calling Array.Sort().

In caess where there is no single order (sometimes you want to sort by prices times quantity. Sometimes single item price), Array.Sort() has overrides that allow you to specify the comparer to be used.

You could also go for Linq, if you have experience with it. But getting both values into one type is a requirement. And the perfect time to learn that.

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

2 Comments

If I were to create another class for this, how would the code look? Would I just move those arrays into the constructor of the other class, or set them as properties? After that, what would I do from there?
The laziest way* to initialise a struct, class or tupel is via Object Initialisers (msdn.microsoft.com/en-us/library/bb384062.aspx). Writing a custom constructor that takes both arguments can be helpfull. But it will only be nessesary if the values have a private setter or are in some other way readonly. *Not judging. being lazy when writing code is a good trait for a programmer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.