1

I am trying to write some logic which allows me to store values into a 2 dimensional array. Within the function below, I would like to store the current coins[i] value, with the corresponding coin variable as a pair. However, I am not exactly sure how I can this can be done. Reason being is that I would like to loop through the array once it has been populated and print out the current coins[i] value along with the corresponding coin variable which indicates the amount of times used to dispense change.

Function:

int counter = 0;
int coin;
for (int i = 0; i < coins.Length; i++)
{
    coin = Math.Min(quantities[i], (int)(change / coins[i]));
    counter += coin;
    change -= coin * coins[i];
    // want to store [coins[i], coin] in an 2Darray
}
Console.WriteLine("Number of coins = {0}", counter);

If there is another way that this can be done, please be sure to provide suggestions. Bare in mind I cannot use anything from the Collection classes. All answers are appreciated.

2
  • I'm not sure whether I understood the question properly; why don't you use an actual two-dimensional array? msdn.microsoft.com/en-us/library/2yd9wwz4.aspx Commented Apr 22, 2014 at 11:41
  • I want to use a two-dimensional array, however I am unsure how this can be done. And how to populate it with the values I have specified above. Commented Apr 22, 2014 at 11:45

3 Answers 3

1

As the discussion might be a bit hard to follow - is the code below what you are looking for?

// suppose the definition of array 'coins' is somewhere else

int counter = 0;
int coin;

int[] change_given = new int[coins.Length]; // will be the same length as 'coins'

for (int i = 0; i < coins.Length; i++)
{
    coin = Math.Min(quantities[i], (int)(change / coins[i]));
    counter += coin;
    change -= coin * coins[i];
    // want to store [coins[i], coin] in an 2Darray
    change_given[i] = coin;
}

for (int j = 0; j < change_given.Length; j++)
{
     Console.WriteLine("Number of coins of type {0} returned: {1}", j, change_given[j]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is exactly what I would like to do. Would this be the simplest way of doing it?
I would consider this the simplest way of doing it. It could be done with a 2-dimensional array array[2][coins.Length], where array[0][i] ('upper row') would be the input and array[1][i] ('lower row') would be the output, but this approach seems rather artificial to me. However, you really take a serious effort to find an optimal implementation.
0

You can do this by using a 2 dimentional array having coins.Length as the length and 2 as the width of the array.

int[,] x = new int[coins.Length, 2];
for (int i = 0; i < coins.Length; i++)
{
        ... your code    
        x[i, 0] = coin;
        x[i, 1] = coins[i];
}

2 Comments

Ok, I understand clearly the part where you have defined the 2D array, however as for the following code: x[i, 0] = coin; && x[i, 1] = coins[i];. I am not sure what they do.
You can imagine that your array is a table. Let's say that new int[5, 2]; represents a table with 5 rows and 2 columns. x[2,0] represents the value on the third row and first column. x[i, 0] = coin; affects the first column of the row number 'i+1' with your value.
0

As described here, a two-dimensional array can be defined by, for instance,

int[,] array = new int[4, 2];

and accessed by

array[i][j] = SomeValue;

in fact it is addressed the same way as in your commented-out code.

4 Comments

Yep that makes sense, however in the position array[i] I would like to store the variable coins[i], and the position array[j] I would like to store the variable coin. How can I do this?
Perhaps I am missing your point; perhaps you don't need a two dimensional array in the first place, but a second one-dimensional array to store the number of coins selected for a specific type of coin?
Yes, I was originally thinking that and it can be done that way certainly. Although, I thought that later once I have to print out the 2D array, it would be easier to loop through just one instead of dealing with 2 separate arrays?
Well, if the array is two-dimensional, most probably you would iterate with two nested loops. I don't quite understand why a two-dimensional array here would yield any advantage over an additional one-dimensional array. As I unserstood it, conceptually you have two one-dimensional arrays for the problem in question: one containing the input (coins received per type) and the output (coins returned per type).

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.