Johnny Redburn

04-30-2004, 10:15 AM

This is the question i require to do for part 2b of my assignment:

Part a) // Ive dont this one already. well I hope its ok :D

You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step.

Part b) // this is the one im doing now //code is below

You are to extend your solution to part (a) above by allowing the user to enter the amount of change as dollars and cents as a double (real number). Valid dollar values are 100, 50, 20, 10, 5 and 1. You program should output the correct combination of dollar and cent denominations required.

In Part b you will need to explicitly convert a double to an integer to break the inputted double down to its component parts as integers. You can use the following code as the basis for doing this:

float change;

int dollar, cents;

...

dollar = (int) change;

cents = (int) rint(((change - dollar)*100));

Note to use the rint() function you will need to include the math.h file.

Again your solution for this problem should be modular.

Here is what ive done so far:

#include <stdio.h>

#include <math.h>

struct change

{

int hundollar, fifdollar, twendollar, tendollar, fivedollar, onedollar, fifties, twenties, tens, fives;

};

int getIn(void)

{

char ch[10];

int c=0;

do

{

printf("\n");

printf("\n");

printf("Please enter the change amount (increments of 5c minimum): ");

fflush(stdout);

fgets(ch,sizeof ch,stdin);

c=atoi(ch);

printf("\n");

} while (c<5 || (c%5)>0);

return c;

}

void cch(const int change, struct change *t)

{

int cur=change;

t->hundollar=cur/100;

cur%=100;

t->fifdollar=cur/50;

cur%=50;

t->twendollar=cur/20;

cur%=20;

t->tendollar=cur/10;

cur%=10;

t->fivedollar=cur/5;

cur%=5;

t->onedollar=cur/1;

cur%=1;

t->fifties=cur/50; //How do I get this to a floating point number (double)?

cur%=50;

t->twenties=cur/20; //How do I get this to a floating point number (double)?

cur%=20;

t->tens=cur/10; //How do I get this to a floating point number (double)?

cur%=10;

t->fives=cur/5; //How do I get this to a floating point number (double)?

}

void print(const int change, const struct change t)

{

printf("To make change for $%d, you will need:\n",change);

printf("%d One hundred dollar note%s.\n%d Fifty dollar note%s.\n",t.hundollar,t.hundollar > 1 ? "s" : "",t.fifdollar, t.fifdollar > 1 ? "s" : "");

printf("%d Twenty dollar note%s.\n%d Ten dollar note%s.\n",t.twendollar,t.twendollar > 1 ? "s" : "",t.tendollar, t.tendollar > 1 ? "s" : "");

printf("%d Five dollar note%s.\n%d One dollar coin%s.\n",t.fivedollar,t.fivedollar > 1 ? "s" : "",t.onedollar, t.onedollar > 1 ? "s" : "");

printf("%d Fifty cent piece%s.\n%d Twenty cent piece%s.\n",t.fifties,t.fifties > 1 ? "s" : "",t.twenties, t.twenties > 1 ? "s" : "");

printf("%d Tens cent piece%s.\n%d Five cent piece%s.\n",t.tens,t.tens > 1 ? "s" : "",t.fives, t.fives > 1 ? "s" : "");

}

int main(void)

{

int c;

struct change chng={0};

c = getIn();

cch(c,&chng);

print(c,chng);

return 0;

}

I was wondering how I would change it from an integer to a double like it says in the question. The second program I did was similar to this except it only used cents, so i could use int the whole way through

Do I have to change everything to a floating point number? even the dollars?

also I wasnt to sure what rint() was :o

I stayed up all night do this type of stuff and im tired and confused hehe :confused:

Part a) // Ive dont this one already. well I hope its ok :D

You are asked to write a simple program that will accept an integer value in the range of 5-95 and in increments of 5 at a time, representing the number of cents to give to a customer in their change. The program should calculate how many coins of each denomination and display this to the user. Valid coin values are 50, 20, 10 and 5. Your solution (program and algorithm) should be modular in nature. This requires the submission of a high-level algorithm and suitable decompositions of each step.

Part b) // this is the one im doing now //code is below

You are to extend your solution to part (a) above by allowing the user to enter the amount of change as dollars and cents as a double (real number). Valid dollar values are 100, 50, 20, 10, 5 and 1. You program should output the correct combination of dollar and cent denominations required.

In Part b you will need to explicitly convert a double to an integer to break the inputted double down to its component parts as integers. You can use the following code as the basis for doing this:

float change;

int dollar, cents;

...

dollar = (int) change;

cents = (int) rint(((change - dollar)*100));

Note to use the rint() function you will need to include the math.h file.

Again your solution for this problem should be modular.

Here is what ive done so far:

#include <stdio.h>

#include <math.h>

struct change

{

int hundollar, fifdollar, twendollar, tendollar, fivedollar, onedollar, fifties, twenties, tens, fives;

};

int getIn(void)

{

char ch[10];

int c=0;

do

{

printf("\n");

printf("\n");

printf("Please enter the change amount (increments of 5c minimum): ");

fflush(stdout);

fgets(ch,sizeof ch,stdin);

c=atoi(ch);

printf("\n");

} while (c<5 || (c%5)>0);

return c;

}

void cch(const int change, struct change *t)

{

int cur=change;

t->hundollar=cur/100;

cur%=100;

t->fifdollar=cur/50;

cur%=50;

t->twendollar=cur/20;

cur%=20;

t->tendollar=cur/10;

cur%=10;

t->fivedollar=cur/5;

cur%=5;

t->onedollar=cur/1;

cur%=1;

t->fifties=cur/50; //How do I get this to a floating point number (double)?

cur%=50;

t->twenties=cur/20; //How do I get this to a floating point number (double)?

cur%=20;

t->tens=cur/10; //How do I get this to a floating point number (double)?

cur%=10;

t->fives=cur/5; //How do I get this to a floating point number (double)?

}

void print(const int change, const struct change t)

{

printf("To make change for $%d, you will need:\n",change);

printf("%d One hundred dollar note%s.\n%d Fifty dollar note%s.\n",t.hundollar,t.hundollar > 1 ? "s" : "",t.fifdollar, t.fifdollar > 1 ? "s" : "");

printf("%d Twenty dollar note%s.\n%d Ten dollar note%s.\n",t.twendollar,t.twendollar > 1 ? "s" : "",t.tendollar, t.tendollar > 1 ? "s" : "");

printf("%d Five dollar note%s.\n%d One dollar coin%s.\n",t.fivedollar,t.fivedollar > 1 ? "s" : "",t.onedollar, t.onedollar > 1 ? "s" : "");

printf("%d Fifty cent piece%s.\n%d Twenty cent piece%s.\n",t.fifties,t.fifties > 1 ? "s" : "",t.twenties, t.twenties > 1 ? "s" : "");

printf("%d Tens cent piece%s.\n%d Five cent piece%s.\n",t.tens,t.tens > 1 ? "s" : "",t.fives, t.fives > 1 ? "s" : "");

}

int main(void)

{

int c;

struct change chng={0};

c = getIn();

cch(c,&chng);

print(c,chng);

return 0;

}

I was wondering how I would change it from an integer to a double like it says in the question. The second program I did was similar to this except it only used cents, so i could use int the whole way through

Do I have to change everything to a floating point number? even the dollars?

also I wasnt to sure what rint() was :o

I stayed up all night do this type of stuff and im tired and confused hehe :confused: