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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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);

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    Interesting..., have you thought about using a linked list?

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What is a linked list? I am new to programming, is that like pointers and such?

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  • Users who have thanked kamillion for this post:

    jtho117 (03-26-2012)


  •  

    Posting Permissions

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