0

Well, I've been working on this for an embarrassing amount of time and just can't work out the kinks. After days of research and re-watching videos, scouring through my notes and reddit, I think I've gotten close to a working program for 'greedy.c'.

Here is my code and the resulting errors.

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


float f;
int ni;

int main(void)
{

do
{
printf("Change due: \n");
float f = GetFloat();
}
while (f < 0);

int ni = roundf(f * 100);

int currency [] = {25, 10, 5, 1};

for (int i = 0; 0 < ni % currency; i++ && (ni - currency))
{
    printf("%i \n", i);
}
}

15:7: error: unused variable 'f'

float f = GetFloat(); ^

greedy.c:23:24: error: invalid operands to binary expression ('int' and 'int *')

ni % currency; ~~ ^ ~~~~~~~~

23:47: error: invalid operands to binary expression...

(ni - currency)) ~~ ^ ~~~~~~~~

Thanks everyone for your time!

Below is my new code. All that is missing is a way to continuously prompt the user for a value that is greater than or equal to 0.

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

int main(void)
{
printf("Change due: \n");
float f = GetFloat();
int ni = roundf(f * 100);
// Prompts user for input, then rounds the resulting float by 100 and produces an int.

int coin_counter = 0;
// Set the number of coins initially to '0'.

int quarter = ni / 25;
// Declare a new int for every coin type. Probably a more elegant way to do this.
ni = ni % 25;
// Here I am calling up the variable 'ni' and changing its value as I move through the function.
// The variable "ni" becomes the remainder of the modulo of each coin type as I work through the function.
int dime = ni / 10;
ni = ni % 10;

int nickel = ni / 5;
ni = ni % 5;

int penny = ni / 1;
ni = ni % 1;
// As sometimes the variables are returned as '0', we simply add '0' of that coin type to the final number of coins.

printf("%i, \n", coin_counter + quarter + dime + nickel + penny);
// This prints the total number of coins returned by simply adding the amount of each coin type needed, which were calculated in order of largest to smallest.
}
1

You have a bit of work to do.

Just try to push you in the right direction.

  1. You have variables declared outside of the main function (ni, f) , unless it is unavoidable you want to give variables local scope (declare them within a function).
  2. Currency is an array of ints, so the line ni - currency if we were to substitute the variables for actual values would look something like "1500 - {25, 10, 5, 1}" (assigning a random value to ni) which doesn't make sense to us as people and makes even less sense to the compiler. So what are you trying to do with that line, relook up the videos on arrays and indexing.
  3. You are going to want to look up how for loops work as well, that is going to be your next problem.
    1. Biggest suggestion is start commenting your code before you do anything else, figure out exactly how you solve the problem in plain English, maybe even get some coins, and work through your algorithm physically. Break it down into steps exactly what needs to happen. These are like headings in an essay and then under each heading write the code that will accomplish that step.

Hope that helps.

5
  • Thanks for that, I really did have a lot of work to do. I have found a solution that handles everything but continuously prompting the user for a float when the input is less than 0. This is a problem of scope, I'm pretty sure. I've tried 'do-while' and just 'while' loops, to no avail. Mar 13 '15 at 3:28
  • What behaviour do you want it to do (in plain English)? Loops have an exit condition, the code above would do exactly what you just said. Loop until value is greater than 0. Mar 13 '15 at 3:33
  • The existing code only prompts the user for a value once and runs through the program regardless of whether the value is negative or positive. I am attempting to implement a way to continuously prompt the user for a value greater than or equal to 0. Mar 13 '15 at 3:35
  • Missed your "new code" the do while loop at the top of your "old code" does what you are asking. Mar 13 '15 at 3:41
  • Aha, finally figured it out. It was a problem of scope; I was trying to initialise variable f as a float inside and outside of the do-while loop, while it only has to be declared as a float once in this case. Thanks for the assistance. Mar 13 '15 at 4:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .