In main
a comma is missing in
printf("50 = %d, 20 = %d, 10 = %d, 5 = %d" money[1], money[2], money[3], money[4]);
must be
printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
In
void fifty(int money, int 5)
you cannot name a parameter with a number, what did you expect ?
Also the return;
at the end of your void functions is useless
In main you do not initialize money[0]
but all the other functions test its value, the behavior is undefined. Probably you wanted do do
scanf("%d", &money[0]);
I also encourage you to check a value was enter through the scanf, checking it returns 1. Note you are in C++ so you can also use istream operator >>
In the other function you increment entries in money but these ones are not initialized, probably you wanted :
int money[5] = {0};
initializing the 5 elements to 0
And because you need to access to several elelemnts in the array in the other function you do not have to give a specific element but all the array, so for instance :
fifty(money[5]);
must be
fifty(money);
and the same for the other calls
Note you suppose there is only 0 or 1 times each of the values, for instance
void fifty(int money)
{
/*This is getting the whole array from main() and modifying the values of each index*/
if (money[0] > 50) {
money[0] = money[0] - 50;
money[1]++;
}
}
if money is 100 you must set money[1]
to 2 rather than 1, for that :
void fifty(int money[])
{
/*This is getting the whole array from main() and modifying the values of each index*/
money[1] = money[0]/50;
money[0] %= 50;
}
and to do that for each value produces a similar definition for all the function, this is useless, you can define only one function getting the value and the index to set, for instance :
void count(int money[], int value, int index)
{
money[index] = money[0]/value;
money[0] %= value;
}
and rather than to call
fifty(money);
twenty(money);
ten(money);
five(money);
you can do
count(money, 50, 1);
count(money, 20, 2);
count(money, 10, 3);
count(money, 5, 4);
or use any other way like a loop
Note that you do not indicate the number of value 1, seems strange
A proposal taking into account my remarks :
#include <stdio.h>
void count(int money[], int index, int value);
int main()
{
int money[5]; // useless to initialize it because I just assign entries
printf("Enter cash amount: ");
if (scanf("%d", &money[0]) != 1) {
puts("invalid input, abort");
return -1;
}
count(money, 50, 1);
count(money, 20, 2);
count(money, 10, 3);
count(money, 5, 4);
/*The next two lines should print out how many of each denomination is needed*/
printf("Change to be given: \n");
printf("50 = %d, 20 = %d, 10 = %d, 5 = %d\n", money[1], money[2], money[3], money[4]);
return(0);
}
void count(int money[], int value, int index)
{
money[index] = money[0]/value;
money[0] %= value;
}
Compilation and execution :
pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Werror e.cc
pi@raspberrypi:/tmp $ ./a.out
Enter cash amount: 275
Change to be given:
50 = 5, 20 = 1, 10 = 0, 5 = 1
pi@raspberrypi:/tmp $
And using iostream rather than stdio :
#include <iostream>
void count(int money[], int index, int value);
int main()
{
int money[5]; // useless to initialize it because I just assign entries
std::cout << "Enter cash amount: ";
if (! (std::cin >> money[0])) {
std::cout << "invalid input, abort" << std::endl;
return -1;
}
count(money, 50, 1);
count(money, 20, 2);
count(money, 10, 3);
count(money, 5, 4);
std::cout << "Change to be given: \n"
<< "50 = " << money[1]
<< ", 20 = " << money[2]
<< ", 10 = " << money[3]
<< ", 5 = " << money[4] << std::endl;
return(0);
}
void count(int money[], int value, int index)
{
money[index] = money[0]/value;
money[0] %= value;
}
And finally a version where the values are indicated in an array allowing to change only one line of code to change the list of bills :
#include <iostream>
void count(int money[], int value, int index);
int main()
{
const int value[] = { 50, 20, 10, 5, 1 }; // list of biils
int money[sizeof(value)/sizeof(value[0]) + 1];
std::cout << "Enter cash amount: ";
if (! (std::cin >> money[0])) {
std::cout << "invalid input, abort" << std::endl;
return -1;
}
for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i)
count(money, value[i], i+1);
std::cout << "Change to be given: \n";
for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i) {
if (i != 0)
std::cout << ", ";
std::cout << value[i] << " = " << money[i+1];
}
std::cout << std::endl;
return(0);
}
void count(int money[], int value, int index)
{
money[index] = money[0]/value;
money[0] %= value;
}
Compilation and execution :
pi@raspberrypi:/tmp $ ./a.out
Enter cash amount: 276
Change to be given:
50 = 5, 20 = 1, 10 = 0, 5 = 1, 1 = 1
Now count is called at only one location in the code, so it can be removed moving its body to replace its call :
#include <iostream>
int main()
{
const int value[] = { 50, 20, 10, 5, 1 }; // list of bills
int money[sizeof(value)/sizeof(value[0]) + 1];
std::cout << "Enter cash amount: ";
if (! (std::cin >> money[0])) {
std::cout << "invalid input, abort" << std::endl;
return -1;
}
for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i){
money[i + 1] = money[0]/value[i];
money[0] %= value[i];
}
std::cout << "Change to be given: \n";
for (size_t i = 0; i != sizeof(value)/sizeof(value[0]); ++i) {
if (i != 0)
std::cout << ", ";
std::cout << value[i] << " = " << money[i+1];
}
std::cout << std::endl;
return(0);
}
For instance if I want to also take into account the bills of 100 I just have to replace const int value[] = { 50, 20, 10, 5, 1 };
by const int value[] = { 100, 50, 20, 10, 5, 1 };
without having to do other changes in the code. That way to do is very important for the maintainability of the code.