# Thread: [HELP] Changing cents into individual denominations - Programme for Uni

1. ## [HELP] Changing cents into individual denominations - Programme for Uni

Hi guys,
I'm new to this forum and to programming. I started this semester and I need a bit of help with my second assignment. I tried to do something tricky (well, at least for me) and it backfired on me. I haven't actually learned how to use arrays yet but I know the basic principle of it and decided to try and implement one to improve my code.

I'm sure the thing is a bug ridden mess but I would particularly like to point your attention to the function sortDenomination(). Is what I am trying to do in the loop possible?

The reason why I want to do it this way and not with if statements for each denomination is because I can then easily apply this to the second part of the assignment which sees us split a double into dollars and cents and then process each separately. All I would have to differ for each is change the money_loop variable to include the last two denominations in the array. That and it would make me feel clever, which I'm not feeling right now.

Ryan.

Code:
```#include <stdio.h>

/*Reads cents input from the user.*/
{
printf("Please enter the amount of cents between 5-95:\n");
{
printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
}
return;
}

/*Supposed to sort through all cents by denomination (50c, 20c ect.)*/
void sortDenomination(int sort_cents, int money_loop)
{
int loop_count;
int denomination;
denomination = 50;
denomination = 20;
denomination = 10;
denomination = 5;
denomination = 2;
denomination = 1;
for (loop_count = 0; loop_count >= money_loop; loop_count++)
{
if (sort_cents > denomination[loop_count])
{
sort_cents = getDenomination(sort_cents, denomination[loop_count]);
}
}
return;
}

/*Finds number of coins of a denomination and returns remaining cents to be processed again*/
int getDenomination(int remaining_cents, int coin_denomination)
{
int denomination_amount;
denomination_amount = remaining_cents / coin_denomination;
remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
printf("Please give the customer %d%*c", denomination_amount);
printf("of the %d%*c coins\n", coin_denomination);
return(remaining_cents);
}

int main()
{
int cents;
/*This is so I can adjust the length of the for loop for both dollars and cents*/
int cents_loop = 4;
getCents(cents);
sortDenomination(cents, cents_loop);
return(0);
}``` 2. Wait, I'm an idiot. I'm scanning for chars. brb

EDIT: I fixed it, it complies but it didn't make a single bit of difference. I can enter a number and then the programme ends. 3. I haven't looked through your code closely, but having a quick glance at sortDenomination I see this:
Code:
```    int denomination;
denomination = 50;
denomination = 20;
denomination = 10;
denomination = 5;
denomination = 2;
denomination = 1;```
You presumably don't mean to set only the first array location over and over again, but mean to set 0, 1, 2, etc.

Actually, this is better done like so:
Code:
`    int denomination = {50, 20, 10, 5, 2, 1};` 4. Ah thanks oogabooga. Your exactly right.
I'll give a shot and report my findings.

EDIT: While certainly that was a problem apparently there are others. Thanks for pointing that out though. 5. I dont think the way you read the values is right. Or atleast not right in C for certaib. As your not aware of pointer ( I assume ). I would suggest the way you read the be like

Code:
```int getCents( void )
{

printf("Please enter the amount of cents between 5-95:\n");
{
printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
}
}

int cents = getCents();```

ssharish 6. Hi ssharish,
You're correct, I was passing by reference (well, at least I was trying to) which I believe is a feature of C++. I'm not sure if I was doing it correctly but that method has work fine in the past. I implemented the change you suggested just in case and still the same result I'm afraid. I can enter the integer but nothing prints, the programme simply ends.

I'm not sure exactly where everything is going wrong but I assume it's the loop? 7. Just gonna give this a bumpity bump. Back to the top with you! 8. Bumping a thread is generally frowned upon. And you should post your current code, which would have given you a good reason to post again! 9. Ah thanks oogabooga, I didn't know about that. Sorry, I'll keep that in mind for the future.

Here's what I got:
Code:
```#include <stdio.h>

/*Reads cents input from the user.*/
int getCents(void)
{
printf("Please enter the amount of cents between 5-95:\n");
{
printf("Error - please enter an amount between 5-95 cents that is divisible by 5\n");
}
}

/*Finds number of coins of a denomination and returns remaining cents to be processed again*/
int getDenomination(int remaining_cents, int coin_denomination)
{
int denomination_amount;
denomination_amount = remaining_cents / coin_denomination;
remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
printf("Please give the customer %d%*c", denomination_amount);
printf("of the %d%*c coins\n", coin_denomination);
return(remaining_cents);
}

/*Supposed to sort through all cents by denomination (50c, 20c ect.)*/
void sortDenomination(int sort_cents, int money_loop)
{
int loop_count;
int denomination = {50, 20, 10, 5, 2, 1};
for (loop_count = 0; loop_count >= money_loop; loop_count++)
{
if (sort_cents > denomination[loop_count])
{
sort_cents = getDenomination(sort_cents, denomination[loop_count]);
}
}
return;
}

int main()
{
int cents;
/*This is so I can adjust the length of the for loop for both dollars and cents*/
int cents_loop = 4;
cents = getCents();
sortDenomination(cents, cents_loop);
return(0);
}```

EDIT: One small amendment.
Code:
```printf("Please give the customer %d", denomination_amount);
printf("of the %d coins\n", coin_denomination);``` 10. Code:
```while (read_cents < 5 && read_cents > 95)

/*
Can an integer be both less than five AND greater than 95?
*/```
Code:
```for (loop_count = 0; loop_count >= money_loop; loop_count++)

/*
Keep in mind how a for loop works.

1. initialization (first part)
2. check condition (second part)
3. if condition is true, execute code/block
4. update variable(s) (third part)
5. go back to #2

The important thing to note is that the condition is checked first before
the body of the loop is executed.  In your case, "money_loop" is 4, and
"loop_count" is 0, so "loop_count >= money_loop" is false and the loop
never executes.
*/```
Your "sortDenomination" and "getDenomination" functions appear to be broken. I would advise more planning, step by step on paper, before committing the algorithm to code.

For instance, add the following line:

Code:
```int getDenomination(int remaining_cents, int coin_denomination)
{
int denomination_amount;
denomination_amount = remaining_cents / coin_denomination;
remaining_cents = (coin_denomination * denomination_amount) - remaining_cents;
printf("remaining cents:  %d\n",remaining_cents); /* <<< add this line */
printf("Please give the customer %d%*c", denomination_amount);
printf("of the %d%*c coins\n", coin_denomination);
return(remaining_cents);
}```
... enter "95", and see what your calculations are doing. 11. Thanks Matticus, it's working!
In my defense I actually wrote out the getDenomintion() function correctly on the algorithm and desk checked it but apparently 3-in-the-morning me decided it was a good idea to swap things around when writing the code! Also, o
n the for loop my thinking was that the loop continued until that statement was satisfied, I have no idea where I got that from. You got me dead to rights on that error check loop though, that was very silly of me *sheepish grin*.

Thanks to every one who helped me, hopefully I wont balls up the second part and have to come running back to you all. Popular pages Recent additions 