-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeCounter.c
More file actions
38 lines (32 loc) · 1.24 KB
/
changeCounter.c
File metadata and controls
38 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int coinCntr(int coinVal, int coinCount, int *change2); //initializes the function
int main(void) {
int coinCount=0;
float change;
printf("I'm a change calculator!\nI calculate the minimum necessary number of coins to make change\n");
do {
printf("How much change is needed? ");
change= GetFloat();
}
while (change<0); //keeps asking for change until non-negative is given
int change2 = round(change*100); //converts change into workable int
coinCount=coinCntr(25, coinCount, &change2);
coinCount=coinCntr(10, coinCount, &change2);
coinCount=coinCntr(5, coinCount, &change2);
coinCount=coinCntr(1, coinCount, &change2);
printf("You will need %d coins.\n",coinCount);
}
/*This is a function that subtracts as many possible values of
a given coin unit, CoinVal, until the number is too small to subtract more.
For each subtraction it increments coinCount by one. * and & are used on change2
to have an actual effect on the variable.
*/
int coinCntr(int coinVal, int coinCount, int *change2) {
while (*change2>=coinVal) {
*change2 -=coinVal;
coinCount++;
}
return coinCount;
}