0

Context;

I'm trying to solve Cash by;

  1. Taking positive integer user input, saves as float "ch" for change. (this as an implementation detail)

  2. Create an array "coin_val" with coin value by descending order (so 25c, 10c, 5c, 1c). This array has a parameterised index integer "i" initialised to 0.

  3. do while-loop that stops when "i" is > 3. step 4 is nested in this loop.

  4. while-loop that stops if the current "coin_val" is greater than the current total "ch" and increments a coin counting int, "coin", by 1 when "coin_val" is less than "ch".

  5. (After step 4 while-loop, and nested in the do-while-loop (step 3)) Increments "i".

  6. Once the do while-loop is complete (and the change has been counted out) the "coin" total prints.

I'm pretty sure I don't need to be implementing arrays for this pset but doing 4 of the same while-loops for each coin value seems stupid and repetitive, and it seems like a good thing to practice arrays on before pset2. (I'm trying this because I watched the week 2 lecture on arrays, but I stupidly missed this part of pset1, so I'm back to get it done before I try pset2). I could probably make the array and the coin counting loop into an implementation detail also, and make main more succinct, but I can't get this set up to work so I haven't tried yet.

Here's what I'm on about;

    // array
    float coin_val[] = {0.25, 0.10, 0.05, 0.01};
    int i = 0;

    // get user input for change
    float ch = get_pos_float();

    int coin = 0;
    // while index position of array is <= length of array
    do
    {   
        // inner loop that deducts current coin from change, ends when coin is larger than change
        while(ch > i)
        {
            ch = ch - coin_val;
            coin++;
        }
        coin_val[i++];
    }
    while(i < 3);

    printf("Coin total: %i\n", coin);

My problem is that the compiler returns;

error: expression result unused [-Werror,-Wunused-value]
        coin_val[i++];
        ~~~~~~~~ ~~~^

This might be a simple problem, although I don't feel like I fully understand how incrementing arrays works yet, so I don't understand where I'm going wrong.

I would greatly appreciate any help, I think my solution is better, and don't want to settle for writing repetitive code just because it's easier. Similarly, if this is a lesson for later in the course and something I don't need to worry about yet, please tell me to stop being so enthusiastic and solve the problem the lazy way. Cheers!

0

I figured this out;

After searching a few sites I found that you can't increment an index, but you can increment a pointer, and assign the array value to the pointer. (I had no idea what a pointer was, but I found out.)

ref: https://www.studytonight.com/c/programs/pointer/pointer-increment-and-decrement

The change in code from above is;

get_pos_float function is now get_pos_int (this takes user input as a float, checks that it's a positive, multiplies by 100 and rounds to nearest whole number, then returns that number as int "ch").

Incrementing of array is now decrementing of array (and array is now in ascending order). It was probably better incrementing, so that I wouldn't have to hardcode the pointer to the end of the array, but incrementing was missing a coin in the coin count for some reason and this worked.

Array was a float, but is now an int (thanks to get_pos_float becoming get_pos_int).

implemented a pointer, "*ptr", and used it to decrement through the items in the array.

Code;

    int coin_val[] = {1, 5, 10, 25}; 
    int *ptr;
    ptr = coin_val + 3;
    int coin = 0;
    // user input
    int ch = get_pos_int();
    // loop for checking/ changing coin value
    do
    {      
        // loop for subtracting present coin value
        while(ch >= *ptr) 
        {
            ch = (ch - *ptr);
            coin++;
        }
        ptr--; 
    } 
    while(*ptr > 0);
    printf("Coin total: %i\n", coin);

It's not perfect, I haven't made an implementation detail out of the array and coin counter yet, but it works with all the tests I've run on it, and I think it's a better solution than writing the same while loop for each coin value. I also learned some stuff about arrays and pointers which is cool, and caused my first stack overflow!

| improve this answer | |
  • I like how a moment ago you were worried about arrays being too complex and now you are happy to dive into pointers :P That said, I feel for the sake of your sanity if you are using a pointer to index into an array, setting an explicit array integer may be easier to think about. In this example it doesn't make a lot of difference, but just think ahead a little to when you are dealing with hundreds of lines of codes. Being explicit and simple are often best – Andrew Micallef Apr 23 at 20:43
  • Thanks for the input with this. I didn't find Arrays too complex, I just couldn't understand how to properly increment through them. I am very new to coding, with no experience before CS50, so jumping into pointers was more of "what does the internet say will make this thing work" as opposed to "how and why is this not working", meaning I probably need to better understand pointers and probably more about arrays. I appreciate your input on thinking a little ahead when dealing with more code, but I'm very much at like "don't know what I don't know". Early days yet though! Thanks man! – JoeH Apr 23 at 22:44
  • I see, well incrementing through the array is simply a matter of setting an index variable, such as i in your original code, then using the construction coin_val[i] to reference a specific item in your array. In your case you can actually refactor your code to do this in a single for loop. I'm sorry if Im being a bit vague, I want to give you hints without the answer – Andrew Micallef Apr 24 at 0:10
0

You are very much on the right track with this one. I would not worry about the fact that you are implementing an array solution before arrays are covered in the lectures. What matters is that you understand when and how to use them. If your program passes check50 then you wrote a good program.

It looks like you are not doing anything with coin_val... do you maybe want to add it to something?

| improve this answer | |
  • Oops, yeah, sorry. I was messing about with that (it obviously didn't work) and forgot to change it back, I just corrected it from ch = ch - i; to ch = ch - coin_val;. Also thank you for the confidence boost, I'm new to this, so that was kind of you. I'm trying what you recommended now, will update if it works! – JoeH Apr 22 at 12:41
  • Yeah sorry I jumped the gun on this one, re incrementing. There was nothing wrong with the expression coin_val[i++] that is a succinct way of getting the next item in an array. Part of the problem was that you were not using that value for anything. It needed something like X = coin_val[i++] where X is some float variable that you use to keep track of this coins value (or maybe update with X -= coin_val[i++]). But it seams like you figured it out and went an order above the problem spec. So congrats :P – Andrew Micallef Apr 23 at 20:48

You must log in to answer this question.

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