Use the if with arrays

3

For a university job I have to make a distributor. Now in one part I have to calculate how many coins I will return to give the change.

For example I have a water drink that costs € 2.20 . The person pays with 3 so their change would be 0.80 . The program does that without any problem.

When the person takes the order, they enter (through a listbox containing the values of each currency) how many coins of each value they receive.

And now I have to calculate and show how many coins and value when I'm going to return the change.

I'm trying it this way:

  
  • I made 3 arrays
  •   

1 two-dimensional array to store the value of the coin and the amount   of coins of each value.

     

And a simple decimal array with possible values of the change

int posiblesValores [] (Read here bedragen . It's a program in Dutch)

posiblesValores= new decimal [19];

and to each one I gave these values:

bedragen[0]= 1.90m  ;
bedragen[1]= 1.80m  ;
bedragen[2]= 1.70m  ;
bedragen[3]= 1.60m  ;
bedragen[4]= 1.50m  ;
bedragen[5]= 1.40m  ;
bedragen[6]= 1.30m ;
bedragen[7]= 1.20m  ;
bedragen[8]= 1.10m ;
bedragen[9]= 1.00m  ;
bedragen[10]= 0.90m ;
bedragen[11]= 0.80m  ;
bedragen[12]= 0.70m  ;
bedragen[13]= 0.60m  ;
bedragen[14]= 0.50m ;
bedragen[15]= 0.40m  ;
bedragen[16]= 0.30m  ;
bedragen[17]= 0.20m  ;
bedragen[18]= 0.10m  ;

Array bidimensional = decimal cambio[,]= new decimal [5,2] (Change in Dutch. teruggave )

teruggave[0, 0] = 2.00m;
teruggave[1, 0] = 1.00m;
teruggave[2, 0] = 0.50m;
teruggave[3, 0] = 0.20m;
teruggave[4, 0] = 0.10m;

Then I made a method with if :

wisselgeld (value to be returned)

With this I try that for example if the value to return is equal to 1.90 then show me that I have to return a coin of 1 , one of 0.50 and two of 0.20 .

The problem is when I run the program for each value that I have to return it gives me the same screen whenever it is the first change of if bone as if value to return always out 1.90 .

Already verify and the program takes well the value of the change and the value of array of possible values and compares it well. For example, if 0.30 calculates:

wisselgeld = 0.30 bedragen[0] = 1.90

if (wisselgeld == bedragen[0]) //NO SE CUMPLE LA CONDICIÓN
{
    teruggave[1, 1] = 1;
    teruggave[2, 1] = 1;
    teruggave[3, 1] = 2;
}

(in teruggave[0-5,1] I keep the amounts of coins)

My question is because if you compare well and for example it says 0.30 is not equal to 1.90 the program of the value to array teruggave [ ,1] as if it were equal. It is the same with any exchange value. Always assign values to array as if it were equal to 1.90 .

Could it be that there is another way to do this and I am doing it very complicated? Why does not the if work for me here?

This is the method with if :

void TerugGaveBedrag()
{            
    if (wisselgeld == bedragen [0])
    {
        teruggave[1, 1] = 1;
        teruggave[2, 1] = 1;
        teruggave[3, 1] = 2;               
    }
    else if (wisselgeld == bedragen [1])
    {
        teruggave[1, 1] = 1;
        teruggave[2, 1] = 1;
        teruggave[3, 1] = 1;
        teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[2])
     {
        teruggave[1, 1] = 1;
        teruggave[2, 1] = 1;
        teruggave[3, 1] = 1;           
     }
     else if (wisselgeld == bedragen[3])
     {
        teruggave[1, 1] = 1;
        teruggave[2, 1] = 1;
        teruggave[4, 1] = 1;           
     }
     else if (wisselgeld == bedragen[4])
     {
        teruggave[1, 1] = 1;
        teruggave[2, 1] = 1;        
     }
     else if (wisselgeld == bedragen[5])
     {
        teruggave[1, 1] = 1;                
        teruggave[3, 1] = 2;                
     }
     else if (wisselgeld == bedragen[6])
     {
        teruggave[1, 1] = 1;
        teruggave[3, 1] = 1;
        teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[7])
     {
        teruggave[1, 1] = 1;
        teruggave[3, 1] = 1;
     }
     else if (wisselgeld == bedragen[8])
     {
        teruggave[1, 1] = 1;
        teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[9])
     {
        teruggave[1, 1] = 1;
     }
     else if (wisselgeld == bedragen[10])
     {
        teruggave[2, 1] = 1;
        teruggave[3, 1] = 2;
     }
     else if (wisselgeld == bedragen[11])
     {
         teruggave[2, 1] = 1;
         teruggave[3, 1] = 1;
         teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[12])
     {
         teruggave[2, 1] = 1;
         teruggave[3, 1] = 1;
     }
     else if (wisselgeld == bedragen[13])
     {
         teruggave[2, 1] = 1;
         teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[14])
     {
         teruggave[2, 1] = 1;
     }
     else if (wisselgeld == bedragen[15])
     {
         teruggave[3, 1] = 2;
     }
     else if (wisselgeld == bedragen[16])
     {
          teruggave[3, 1] = 1;
          teruggave[4, 1] = 1;
     }
     else if (wisselgeld == bedragen[17])
     {
          teruggave[3, 1] = 1;
     }
     else 
     {
          teruggave[4,1] = 1;
     }   
}
    
asked by Adriana Rodriguez 28.11.2018 в 09:49
source

2 answers

2

We are going to simplify your problem to the maximum, because you are taking a totally complex course.

For this, we just have to know what kind of coins you have and how much is the change that you have to return.

Your function, will receive the amount of change you need (which you said works and well), and will be adding coins until you have more change to return.

I will respect your change array (it seems perfect to me if you still can not use classes, lists or dictionaries in the uni)

teruggave[0, 0] = 2;
teruggave[1, 0] = 1;
teruggave[2, 0] = 0.50;
teruggave[3, 0] = 0.20;
teruggave[4, 0] = 0.10;

And I'm going to use it in this class. Keep in mind, that before entering the amount of coins in the should be at 0

Therefore, you should have some function or something to clean it beforehand (make teruggave[0, X] = 0; for all X)

public void DevolverMonedas(decimal ValorACalcular)
{
    int MonedaMaxima = 0;
    While (ValorACalcular > 0)
    {
        if (teruggave[MonedaMaxima, 0] <= ValorACalcular)
        {
            teruggave[MonedaMaxima, 1]++;
            ValorACalcular -= teruggave[MonedaMaxima, 0];
        }
        else
        {
            MonedaMaxima++;
        }
    }
    for(int i = 0; i<5; i++)
    {
        Console.WriteLine($"Cantidad de monedas de {teruggave[i, 0]} = {teruggave[i, 1]}");
    }
}

Let's analyze the function

  • Declares MonedaMaxima, that is to know in which position of the change array we are.

  • While we have change to return, we look for coins.

  • If the currency is of a lower value, we add one to that, subtract the value of the same from the change to be returned.

  • If the currency is larger, we move in the coin array because it does not work.

  • We show the change to return when leaving the while.

  • Notice that if for example, enter 0.8, what happens is the following

    • in the first round, it compares 0.8 with 2, it gives it bigger so it only makes MonedaMaxima ++
    • second round, compare 0.8 with 1, give it bigger so just go here MonedaMaxima ++
    • third round, compare 0.8 with 0.5, give it less, add 1 to the position of 0.5, subtract 0.5 from 0.8, you have 0.3
    • fourth round, compare 0.3 with 0.5, give higher, make MonedaMaxima ++
    • fifth round, compare 0.3 with 0.2, give it less, add 1 to the position of 0.2, subtract 0.2 to 0.3, you have 0.1

    And you can already give an idea of how it goes.

        
    answered by 28.11.2018 в 14:41
    1

    Your way of trying to solve the problem is extremely complex and does not adapt well to changes (for example, adjust to foreign currencies with different division). The easiest thing is to write a routine, let's call it ProducirCambio() , which takes from parameters the sum of the change to be returned and a field with the different available currencies. The routine will fill a dimension of the field with the amount of each coin to be returned and it does so using a for loop, discounting the change to return the value of each coin that is returned until there is nothing left.

    // Parametros:
    //  - cambio: monto a devolver en monedas.
    //  - monedas: array de dos dimensiones de tamaño
    //      [n, 2] donde n es la cantidad de monedas
    //      distintas; [i, 0] debe contener el valor
    //      de la moneda, [i, 1] deber ser 0 y será
    //      cambiado a tener la cantidad de monedas
    //      del valor especificado a devolver.
    public static void
    ProducirCambio(decimal cambio, decimal[,] monedas)
    {
        // Ya no se usan monedas de €0,01 y €0,02 en
        // Holanda, por eso ajustamos el cambio a un
        // valor divisible por €0,05.
        int ajustado = (int)(cambio * 20M);
        cambio = (decimal)ajustado / 20M;
    
        // Pasar por cada valor de moneda y agregar
        // a la devolución las cantidades necesarias.
        for (int i = 0; i < monedas.GetLength(0); i++)
        {
            decimal m = monedas[i, 0];
            for (; cambio >= m; cambio -= m)
                monedas[i, 1]++;
            if (cambio == 0M) break;
        }
    }
    

    This routine could be called like this:

    decimal[] monedas = new decimal[]
        { 2M, 1M, 0.5M, 0.2M, 0.1M, 0.05M };
    decimal[,] devolucion =
        new decimal[monedas.Length, 2];
    for (int i = 0; i < monedas.Length; i++)
        devolucion[i, 0] = monedas[i];
    ProducirCambio(cambio, devolucion);
    for (int i = 0; i < monedas.Length; i++)
        Console.WriteLine("{1} monedas de {0}",
                          devolucion[i, 0],
                          devolucion[i, 1]);
    

    EDIT: To demonstrate the ease of introducing changes to this solution ( refactoring ), I altered the ProducirCambio() routine so that it takes only the values of the change and of the coins as parameters and returns the two-dimensional field as a result:

    // Parametros:
    //  - cambio: monto a devolver en monedas.
    //  - monedas: array de los valores de las monedas
    //      disponibles.
    public static decimal[,]
    ProducirCambio(decimal cambio, decimal[] monedas)
    {
        // Ya no se usan monedas de €0,01 y €0,02 en
        // Holanda, por eso ajustamos el cambio a un
        // valor divisible por €0,05.
        int ajustado = (int)(cambio * 20M);
        cambio = (decimal)ajustado / 20M;
    
        decimal[,] devolucion =
            new decimal[monedas.Length, 2];
    
        // Pasar por cada valor de moneda y agregar
        // a la devolución las cantidades necesarias.
        for (int i = 0; i < monedas.Length; i++)
        {
            decimal m = monedas[i];
            devolucion[i, 0] = m;
            devolucion[i, 1] = 0;
            for (; cambio >= m; cambio -= m)
                devolucion[i, 1]++;
            if (cambio == 0M) break;
        }
    
        return devolucion;
    }
    

    This routine could be called like this:

    decimal[] monedas = new decimal[]
        { 2M, 1M, 0.5M, 0.2M, 0.1M, 0.05M };
    decimal[,] devolucion =
        ProducirCambio(cambio, monedas);
    for (int i = 0; i < monedas.Length; i++)
        Console.WriteLine("{1} monedas de {0}",
                          devolucion[i, 0],
                          devolucion[i, 1]);
    
        
    answered by 28.11.2018 в 15:13