Tuesday, August 13, 2013

Feeling Confident

I have this tendency. I will sit down and start writing a program, then get halfway through it and realize the entire logic of the thing is fucked.

I started on this exercise around 9:30 or 10 this morning (though it has taken so long partially due to the nature of being at work and actually having to... well... work). The exercise is to write a program that will take an original dollar amount, and split it into twenties, tens, fives, and ones.

Oh man. When I wrote it the first time? I thought I had too many variables to begin with, so I tried to make the variables I had do crazy things. I should have saved it. I can't remember exactly what I did, but it ended up with hilarious answers like:

$20 bills: 3
$10 bills: 0
$5 bills: -13
$1 bills: -137

I couldn't help but laugh and delete the whole thing, ahahaha...

After tweaking and pouring over my graphing calculator and a few paper scraps with notes on them, I did get it working though! As of 11:45, here is my working USD splitting program:


#include <stdio.h>
#define twenty 20
#define ten 10
#define five 5
#define one 1

int main (void)
{
   int original_amount;
   int twenty_bills, ten_bills, five_bills, one_bills;
   int orig_after_twen, orig_after_ten, orig_after_five;

   printf("Please enter the original dollar amount: ");
   scanf("%d", &original_amount);

   twenty_bills = original_amount/twenty;
   orig_after_twen = original_amount - (twenty_bills * twenty);

   printf("$20 bills: %d\n", twenty_bills);

   ten_bills = orig_after_twen/ten;
   orig_after_ten = orig_after_twen - (ten_bills * ten);

   printf("$10 bills: %d\n", ten_bills);

   five_bills = orig_after_ten/five;
   orig_after_five = orig_after_ten - (five_bills * five);

   printf("$5 bills: %d\n", five_bills);

   one_bills = orig_after_five/one;

   printf("$1 bills: %d\n", one_bills);

   return 0;
}

5 comments:

  1. Well thought out, but I do have a few tips to help you out.

    1) The Modulus Operator "%" give you the remainder of the division. So "119/20" gives you 5, "119%20" gives you 19.

    2)You have more variables than are needed. You could use a temp_amount in place of the orig_after_* variables and then just reassign the value.

    int main (void){
    int original_amount;
    int twenty_bills, ten_bills, five_bills, one_bills;
    int temp_amount;

    printf("Please enter the original dollar amount: ");
    scanf("%d", &original_amount);

    twenty_bills = original_amount/20;
    temp_amount = original_amount%20;

    printf("$20 bills: %d\n", twenty_bills);

    ten_bills = temp_amount/10;
    temp_amount %= 10;

    printf("$10 bills: %d\n", ten_bills);

    five_bills = temp_amount/5;
    temp_amount %= 5;

    printf("$5 bills: %d\n", five_bills);

    one_bills = temp_amount/1;

    printf("$1 bills: %d\n", one_bills);

    return 0;
    }

    ReplyDelete
    Replies
    1. Yep. I have been going overboard with variables in these hahaha. It didn't really occur to me that the same variable could be reassigned mid-program until I was playing with the program after this one, hehe...

      Unfortunately, when I revised the code with these changes, it returned an erroneous response... For the dollar amount "112", it gave me

      $20: 5
      $10: 0
      $5: 0
      $1: 2

      For some reason, it didn't catch the $10 bill.

      I can't really follow the process... but changed the orig_after_* silliness all to temp_amount and left the rest of the program the same, and the result was correct. Maybe because they are int types?

      Thank you for simplifying those variables, by the way!! I thought there would be issues with using temp_amount to define a new value for temp_amount, but I see that it remains the same until that line is executed.

      five_bills = temp_amount/five;
      temp_amount = temp_amount - (five_bills * five);

      Very interesting!

      Delete
    2. This comment has been removed by the author.

      Delete
    3. I copy pasted my code into a compiler and it ran proper with the $112 amount. Not sure what was causing it to fail out for you, but it was more the point than the function anyway.

      Yes the code runs everything to the right of the assignment= prior to assigning the new value to the variable. The process can be done the same way without using the name in the line too.

      The following would use the current value and perform the operation on the value afterword. These lines run in order would have the following results.

      int var = 10;
      var -= 5; //result var = 5
      var += 5; //result var = 10
      var *= 5; //result var = 50
      var /= 5; //result var = 10
      var %= x; //result var = 0

      Delete
    4. Yeah, true. I really appreciate the advice!! That is a much more succinct way of expressing those statements. I'll try to remember to use them whenever applicable, and when I get to that part of my textbook, I can say, "Haaha! I already know that part!" XD

      Delete