# Mini-Challenge #3

Oct 30th, 2020 (edited)
1,041
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. int len=0;
5.
6. void printArray(int arr[],string name) {
7.     cout << name << ": [";
8.     for (int i=0; i<len-1; ++i) cout << arr[i] << ", ";
9.     cout << arr[len-1] << "]" << endl;
10. }
11.
12.
13. void printCoins(int coins[]) {
14.     int pennies=0, nickels=0, quarters=0, dimes=0;
15.     for (int i=0; i<len; ++i) {
16.         if (coins[i]==25) quarters++;
17.         else if (coins[i]==10) dimes++;
18.         else if (coins[i]==5) nickels++;
19.         else pennies++;
20.     }
21.     function<void(int,string)> show = [&](int cnt, string name) {
22.         if (cnt<1) return;
23.         cout << name << ": " << cnt << endl;
24.     };
25.     show(quarters,"Quarters");
26.     show(dimes,"Dimes");
27.     show(nickels,"Nickels");
28.     show(pennies,"Pennies");
29.
30. }
31.
32. int* centsToCoins(int cents) {
33.     int pennies=0, nickels=0, quarters=0, dimes=0;
34.     quarters = cents/25;
35.     cents%=25;
36.     dimes = cents/10;
37.     cents%=10;
38.     nickels = cents/5;
39.     cents%=5;
40.     pennies = cents;
41.     int total = pennies + nickels + quarters + dimes;
42.     int* a = new int[total];
43.     for (int i=0; i<quarters; ++i) a[i] = 25;
44.     for (int i=quarters; i<quarters+dimes; ++i) a[i]=10;
45.     for (int i=quarters+dimes; i<quarters+dimes+nickels; ++i) a[i]=5;
46.     for (int i=quarters+dimes+nickels; i<quarters+dimes+nickels+pennies; ++i) a[i]=1;
47.     len=total;
48.     return a;
49. }
50.
51. int main() {
52.     cout << "Enter number of cents: ";
53.     int n; cin >> n;
54.     int* coins = centsToCoins(n);
55.     //printArray(coins,"Coins");
56.     printCoins(coins);
57.     return 0;
58. }
59.
RAW Paste Data