Cviceni 11.2 - mincovna
Forum Apl Inf :: C
Strana 1 z 1
Cviceni 11.2 - mincovna
- kód:
#include <stdio.h>
#include <stdlib.h>
int coins_process(unsigned int amount, unsigned int **currency);
int main(int argc, char **argv)
{
unsigned int *coins = NULL;
unsigned int amount = 0;
int count = 0;
printf("Exercise 11.2 - mincovna\n");
printf("Enter amount: ");
scanf("%u", &amount);
count = coins_process(amount, &coins);
printf("Coins: ");
for(int i = 0; i < count; i++) {
printf("%6u", coins[i]);
}
fflush(stdin);
getchar();
free(coins);
return 0;
}
int coins_process(unsigned int amount, unsigned int **currency)
{
unsigned int *currencies = *currency; //Just for better work
unsigned int coins_coins[] = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000};
size_t index_coin = sizeof(coins_coins) / sizeof(unsigned int) - 1;
int count_coins = 0;
while(amount > 0) {
if((int)(amount - coins_coins[index_coin]) >= 0)
{
count_coins++;
currencies = (unsigned int*)realloc(currencies, count_coins * sizeof(unsigned int));
currencies[count_coins - 1] = coins_coins[index_coin];
amount -= coins_coins[index_coin];
} else {
index_coin--;
}
}
*currency = currencies;
return count_coins;
}
adam.korinek- Poèet pøíspìvkù : 25
Join date : 18. 11. 14
Forum Apl Inf :: C
Strana 1 z 1
Povolení tohoto fóra:
Nemůžete odpovídat na témata v tomto fóru
|
|