-3

I'm trying to complete this problem but I cant figure out how to get rid of the errors. Other code that I've looked at that uses arrays and functions looks the same and works but my code wont work for some reason. These are the errors that I'm getting:

Error E2121 Q1.cpp 27: Function call missing ) in function main()
Error E2449 Q1.cpp 32: Size of 'fifty' is unknown or zero
Error E2356 Q1.cpp 32: Type mismatch in redeclaration of 'fifty(int)'
Error E2344 Q1.cpp 10: Earlier declaration of 'fifty(int)'
Error E2063 Q1.cpp 32: Illegal initialization
Error E2293 Q1.cpp 32: ) expected
Error E2141 Q1.cpp 71: Declaration syntax error
#include <math.h>
#include <stdio.h>
#include <iostream>

void fifty(int money);
void twenty(int money);
void ten(int money);
void five(int money);


int main()
{
    int money[5]; /*this contians all info about change and the money that you want change for*/

    printf("Enter cash amount: ");

    scanf("%d", &money[1]);

    fifty(money[5]); /*Passing the array to each module*/

    twenty(money[5]);

    ten(money[5]);
    five(money[5]);
    /*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" money[1], money[2], money[3], money[4]);
    return(0);
}

void fifty(int money, int 5)
{
    /*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]++;
    }

    return;
}

void twenty(int money, int 5)
{

    if (money[0] > 20) {
        money[0] = money[0] - 20;
        money[2]++;
    }
    return;
}

void ten(int money, int 5)
{

    if (money[0] > 10) {
        money[0] = money[0] - 10;
        money[3]++;
    }
    return;
}

void five(int money, int 5)
{

    if (money[0] > 5) {
        money[0] = money[0] - 5;
        money[4]++;
    }
    return;
}
bruno
  • 32,421
  • 7
  • 25
  • 37
  • Other code that uses arrays does not look the same at all; you need to read more carefully (including [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)). – molbdnilo May 07 '19 at 10:33
  • What prevents you from simply comparing the working code to the broken code? – Lightness Races in Orbit May 07 '19 at 11:25

2 Answers2

4

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.

bruno
  • 32,421
  • 7
  • 25
  • 37
1

Oh dear. There are a very large number of errors in this.

#include <math.h>
#include <stdio.h>
#include <iostream>

void fifty(int money);

This declares a function which takes a single integer. You actually need to pass an array of five integers. Unfortunately you can't simple declare a function which takes an array of exactly five integers, you just have declare it as taking a pointer to integers:

void fifty(int* money);
void twenty(int* money);
void ten(int* money);
void five(int* money);


int main()
{
    /*this contians all info about change and the money that you want change for*/
    int money[5];

Typo (should be "contains"), and you have defined an array, but you haven't initialized it - so it will contain indeterminate values. You need to initialize it so it contains zeros (some of the changes I make further down actually mean you don't need to initialize it - because I set the variable rather than incrementing):

    int money[5] = {};
    printf("Enter cash amount: ");

    scanf("%d", &money[1]);

This will store the value in money[1]. You actually want money[0] - so:

    scanf("%d", &money[0]);

    fifty(money[5]); /*Passing the array to each module*/

That doesn't pass the array, it passes the single element at offset 5 - which doesn't exist. The only entries are money[0], money[1], money[2], money[3], money[4].

    fifty(money); // Pass the whole array to each function
    twenty(money);
    ten(money);
    five(money);
    /*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" money[1], money[2], money[3], money[4]);

You need a comma after the string:

    printf("50 = %d, 20 = %d, 10 = %d, 5 = %d", money[1], money[2], money[3], money[4]);
    return(0);
}

void fifty(int money, int 5)

That is not how you declare a function which takes an array as argument:

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]++;
    }
    return;

Well yes. That works in a way (once we have initialized the array to zeros) - but what happens if I entered 440 at the prompt - this will only output a single $50 bill - I want eight!

Instead you need to increase money[1] by money[0]/50 (or even better, just set it to that value), and then set money[0] to the remainder. The nice effect is that you then don't need the if (because divide rounds down).

You also don't need the return - but you can leave it if you like.

void fifty(int* money)
{
   money[1] = money[0]/50;
   money[0] = money[0]%50;  // Must do this second.
    return;
}

void twenty(int *money)
{

    if (money[0] > 20) {
        money[0] = money[0] - 20;
        money[2]++;
    }
    return;
}

Hang on! We've seen this code before! Except that it used an offset of 1 and an amount of 50 last time. Time for a function. This is a bit nasty because using int for both types means that it is far too easy to get the arguments the wrong way round. The right solution is to make money be a struct, and use a pointer to member variable for `offset - but I suspect that's a bit too much machinery at the moment.

void change(int* money, int bill, int offset)
{
   money[offset] = money[0]/bill;
   money[0] = money[0]%bill;  // Must be second.
}

void fifty(int* money)
{
   change(money, 1, 50);
}

void twenty(int* money)
{
   change(money, 2, 20);
}

void ten(int* money)
{
   change(money, 3, 10);
}

void five(int* money)
{
   change(money, 4, 5);
}

Finally, as an example of just how easy it is to get the arguments to change the wrong way round, the code above is exactly how I typed it - and I did just that. Fixing the arguments is left as an exercise for the reader.