Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-28-2012, 03:30 AM   PM User | #1
jtho117
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
jtho117 is an unknown quantity at this point
No arrays allowed

HI, I need to create a program that displays the amount of change due... without an array and I don't know how. This is what I have.

Code:
/* Change Dispenser */

#include <stdio.h>
#include <math.h> 
 
void say_hello (void);
double change_due ( double change, double dollar[9]);         /* PCF prototypes$
int main()
{
        
double purchase_total, total_paid, change, chaange, dollar[9];
char repeat;            /* Local variables are declared */

 for( ; ; )
{
        say_hello();                    /* PCF 1, no parmeters or arguments */
        
        printf("Please enter total of purchase:$ ");   /* Obtain total */
        scanf("%lf", &purchase_total);
        
        printf(" Please amount tendered:$ ");  /* Obtain amount paid */   
        scanf("%lf", &total_paid);

 change = total_paid - purchase_total; /* Calculate chnage due */
        chaange = change;
        
        change_due(change, dollar);     /* PCF 2, argument, and 2 paramaters */
        
        printf(" Your change due:$%.2lf \n %.lf - 50('s)\n %.lf - 20('s)\n");
        printf(" %.lf - 10('s)\n %.lf - 5('s)\n %.lf('s)\n");
        printf("%.lf - quarter(s)\n %.lf - dime(s)\n %.lf - nickel(s)\n");
        printf("%.lf - penny(s)\n, chaange, dollar[0], dollar[1],");
        printf("dollar[2], dollar[3], dollar[4], dollar[5], dollar[6],");
        printf("dollar[7], dollar[8]");
        
        printf( "Enter y or n to repeat." );
        scanf("%c", &repeat);
        if ( !repeat) break;
}
        return (0);
}
                
        void say_hello(void)
       {
        printf(" Hi welcome to my Change Machine program! ");
       }
        
double change_due(double change, double dollar[9])
{
        for(50.00 <= change, dollar[0] = 0; 50.00 <= change; dollar[0]++)
        {
                change -= 50.00;
        }
 for(20.00 <= change, dollar[1] = 0; 20.00 <= change; dollar[1]++)
        {
                change -= 20.00;
        }
        for(10.00 <= change, dollar[2] = 0; 10.00 <= change; dollar[2]++)
        {
                change -= 10.00;
        }
 for(5.00 <= change, dollar[3] = 0; 5.00 <= change; dollar[3]++)
        {
                change -= 5.00;
        }
        for(1.00 <= change, dollar[4] = 0; 1.00 <= change; dollar[4]++)
        {
                change -= 1.00;
        }
        for(0.25 <= change, dollar[5] = 0; 0.25 <= change; dollar[5]++)
        {
                change -= 0.25;
        }
for(0.10 <= change, dollar[6] = 0; 0.10 <= change; dollar[6]++)
        {
                change -= 0.10;
        }
        for(0.05 <= change, dollar[7] = 0; 0.05 <= change; dollar[7]++)
        {
                change -= 0.05;
        }
 for(0.01 <= change, dollar[8] = 0; 0.01 <= change; dollar[8]++)
        {
                change -= 0.01;
        }
                
        return(0);
jtho117 is offline   Reply With Quote
Old 02-28-2012, 04:03 PM   PM User | #2
kamillion
New to the CF scene

 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
kamillion is an unknown quantity at this point
Interesting..., have you thought about using a linked list?
kamillion is offline   Reply With Quote
Old 02-29-2012, 06:09 AM   PM User | #3
jtho117
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
jtho117 is an unknown quantity at this point
What is a linked list? I am new to programming, is that like pointers and such?
jtho117 is offline   Reply With Quote
Old 02-29-2012, 08:46 AM   PM User | #4
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Quote:
Originally Posted by jtho117 View Post
What is a linked list? I am new to programming, is that like pointers and such?
http://en.wikipedia.org/wiki/Linked_list
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 02-29-2012, 03:00 PM   PM User | #5
kamillion
New to the CF scene

 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
kamillion is an unknown quantity at this point
Quote:
Originally Posted by jtho117 View Post
What is a linked list? I am new to programming, is that like pointers and such?
Lol, Yes, they involve pointers. Basically a linked list is an object (class) with one or two pointers referencing the previous and or next object (or node) in the series. You can use this in place of an array. I don't know if you've covered classes, or objects yet in your class (im assuming). But a better question would be why are you using an array in this code. I honestly don't see the reason for it or any data structure for that matter.

Hint: use the functions to calculate change instead of an array.
//call functions and assign returned value of that function to variable
fifty = fifty_dollar();
..
..
..
penny = penny();

//create functions
fifty_dollar(){
...
return x
}

etc_dollar(){
...
}

I hope this helps
kamillion is offline   Reply With Quote
Users who have thanked kamillion for this post:
jtho117 (03-25-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 PM.


Advertisement
Log in to turn off these ads.