Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C program, Please help.

    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
    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:

    Code:
    #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
    I stayed up all night do this type of stuff and im tired and confused hehe

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Johnny Redburn
    I was wondering how I would change it from an integer to a double like it says in the question.
    They've given you the code to do that.

    Quote Originally Posted by Johnny Redburn
    also I wasnt to sure what rint() was
    http://www.mkssoftware.com/docs/man3/rint.3.asp

    So basically you need a new function to work on the dollar, and you pass cents to cch() and you're done.

    Hope that helps,
    Sadiq.

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply

    Ive tryed to change the program to accept dollars and cents (first program just needed cents, so i didnt need floating point numbers)
    but theres lots of errors and really need some help
    can someone please look through this and give me some pointers?

    This is what ive done so far:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    struct change		
    	
    	{
    	double hundollar, fifdollar, twendollar, tendollar, fivedollar, onedollar, fifties, twenties, tens, fives;
    	};
    
    double getIn(void)		//GET INPUT FROM USER AND CHECKING IF VALID
    
    	{
    	char ch[10];
    	double 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<0.5 || (c%5)>0);
        
       	return c;
    }
    
    void dollars(const double change, struct change *t)		//CHECKING DOLLAR INPUT AGAINST DIFFERING VALUE AMOUNTS FROM HIGHEST TO LOWEST.
    
    {
    	double 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;
    }
    
    void cents(const double change, struct change *t)		//CHECKING CENTS INPUT AGAINST DIFFERING VALUE AMOUNTS FROM HIGHEST TO LOWEST.
    
    {
        	double cur=change;
        
       	t->fifties=cur/0.50;       
       	cur%=0.50;
        
       	t->twenties=cur/0.20;	
       	cur%=0.20;
        
       	t->tens=cur/0.10;		
       	cur%=0.10;
        
    	t->fives=cur/0.5;		
    }
    
    void toint(const double change)		//CHANGING DOUBLE TO INT, TO ROUND OFF AMOUNT FOR DISPLAY.
    
    {
    
    	int = (int) chng;					
    	cents = (int) rint(((change - dollars)*100));
    
    }
    
    void print(const int change, const struct change t)	//DISPLAYING RESULTS TO THE USER, IF GREATER THAN ONE PLURAL ADDED.
    
    {
        	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)		//MAIN BODY, DECLARING VARIABLE AS A DOUBLE TO ACCEPT LARGE FLOATING POINT NUMBERS FROM THE USER
    
    {
        	double c;
     	
    	struct change chng={0};
        
        	c = getIn();
        	dollars(c,&chng);	
    	cents(c,&chng);		
    	toint(c);		//change for double to integer to round off?
        	
    	print(c,chng);
    
        	return 0;
    }
    thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •