Skip to main content
added 36 characters in body
Source Link
JoeH
  • 15
  • 7

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!

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.

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!

Source Link
JoeH
  • 15
  • 7

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.