I cannot get my program to calculate the change to 100 in the notes that my client puts. In C

Asked

Viewed 55 times

0

I need a program that calculates change for a $100 bill. He asks to enter which notes I want the change and so calculates how many notes of each I need to change 100. Numbers must always be integers . If you enter 0 first, you must ask for another value. Values must exit in ascending order. (ex. INPUT 10 20 - OUTPUT 20 20 20 20 10 10) I cannot develop the pro algorithm logic separates the right amount from each cell. If anyone can help I would be very grateful, I am desperate already haha. Any help is valid. : ) I started trying the individual values that need each note to reach 100, but does not evolve.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n, i, j, troco, n2=2, n5=5, n10=10, n20=20, n50=50, qn2, qn5, qn10, qn20, qn50;

printf("Digite o número da variedade de notas: ");
scanf("%d", &n);

int vet[n];

printf("Digite o valor das notas: \n ");

for(i=0, i<n; i++){
   scanf("%d", vet[i]);
  
}

for(j=0; j<n; j++){ \\ troco individual de cada nota para torcar 100
    if(vet[i]==n2){
        qn2 = (100/ vet[i]);  
    }if(vet[i]==n5){
        qn5 = (100/ vet[i]);  
    }if(vet[i]==n10){
        qn10 = (100/ vet[i]);  
    }if(vet[i]==n20){
        qn20 = (100/ vet[i]);  
    }if(vet[i]==n50){
        qn50 = (100/ vet[i]);  
    }
    
}


return 0;

}

  • Note that your solution is meaningless. If the value reported for n is 2 and you report 10 for vet[0] and 20 for vet[1] then you will get 10 for qn10 and 5 for qn20 which will be quantities that do not match the expected result. You didn’t define how to compose the result, it says that for the example input, the output would be 20 20 20 20 20 20 20 10 10 10 but why not 20 10 10 10 10 10 or 20 20 20 20 20 10 10 10 10? Or any response will be accepted?

  • So, n should be the amount of the variety, that is, if I want my change in 10s and 20s, then my n should be 2.I thought I would read the notes, store it in the array and then read its size as well. The result should come out in ascending order. The intention of N2, N5, N10 and so on is to know the value of the individual change of each value for 100, for 10 real I have 10 notes to reach 100, for 20 real I have 5 20 notes to reach 100 and so on. I started thinking like this to see if it evolved in logic you know, use as resolution feature and not as output.

  • You need to think more about the solution. For example you accept any input but if the input is 20 50 there will be no solution with both the notes.

  • In this case I print a message. It is a condition, but the difficulty is even in doing the counting. I am trying here, who knows, faith. :)

  • Think mathematically, for each value vi reported there must be an amount qi, greater than zero, such that the sum of qi*vi is 100.

1 answer

-1

It is almost what you want, adapt to the output in the desired format.

#include <stdio.h>

int main() {
    int n, i, j, troco, notas[5]={2, 5, 10, 20, 50}, qtd[5]={0, 0, 0, 0, 0}, soma;

    printf("Digite o número da variedade de notas: ");
    scanf("%d", &n);

    int vet[n];

    printf("Digite o valor das notas: \n ");

    for(i=0; i<n; i++){
        scanf("%d", &vet[i]);
        while (vet[i] == 0) {
            printf("Valor inválido. Reinforme: ");
            scanf("%d", &vet[i]);
        }
        j = 0;
        while ((j < 5) && (notas[j] != vet[i]))
            j++;
        if (j < 5)
            qtd[j] = 1;
        else {
            printf("Valor de nota (%d) inválido\n", vet[i]);
            return -1;
        }
    }
    for (j=0; j<5; j++)
        printf("\t%d\t%d\n", notas[j], qtd[j]);
    troco = 100;
    do {
        soma = 0;
        for (j=0; j<5; j++)
            soma += notas[j]*qtd[j];
        if (soma < troco) {
            for (j=4; j>=0; j--) {
                if (qtd[j] != 0)
                    if ((soma + notas[j]) <= troco) {
                        qtd[j]++;
                        j = -1;
                    }
            }
        } 
    } while (soma < troco);
    if (soma == troco) {
        for (i=4; i>=0; i--)
            if (qtd[i] > 0)
                printf("Nota: %d\tQuantidade: %d\n", notas[i], qtd[i]);
    }
    else {
        printf("Impossível compor o valor\n");
    }
    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.