Showing posts with label omgthisissocool. Show all posts
Showing posts with label omgthisissocool. Show all posts

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

Monday, August 12, 2013

Quietly Lurking, Quietly Working

Oh man, I've been pulled in so many different directions lately.

Over the past few weeks, I have:

  • Watched Last of Us from start to finish as my fiance played it (WOW, what an amazing game)
  • Resubbed to World of Warcraft (ugh, Mists is terrible)
  • Started playing Guild Wars 2 again
  • WRITTEN A LOT OF STUFF
  • Watched Minority Report, Tron, Tron: Legacy, The Fifth Element, Daybreakers, and kept up with Breaking Bad and Dexter
  • Messed around with C
  • Done some really fulfilling sketches for character designs
I've been really really busy. But the main reason I'm posting today is to say, "LOOK, LOOK!! I'm coding things on my own!!"

Take a gander! :)

These are exercises in my textbook -- which I have since decided that taking the notes down in my notebook is time-consuming enough... I don't think I'll be uploading them to this blog anymore. Instead, I'll be sharing the source that I write, when I feel like it. C: I'm stingy, I know.

So, the program I'm particularly fond of at this time is this exercise. I was prompted to find the volume of a sphere using a constant. So I did:

#include <stdio.h>
#define RADIUS 10.0f

int main(void)
{
   float volume;

   volume = (4.0f/3.0f * 3.14159 * (RADIUS * RADIUS * RADIUS));

   printf("Radius = %.1f\n", RADIUS);
   printf("Volume = %.3f\n", volume);

   return 0;

}

It was pretty nifty! Compiled and performed just fine out of Xcode (which is what I use on Mac because I haven't had time to investigate any others -- on the Zombie, I've just been using Mousepad to make changes so far). But I wondered if there was another way to do it. So I experimented...

#include <stdio.h>

int main(void)
{
   float radius, volume;

   radius = 10.0f
   volume = (4.0f/3.0f * 3.14159 * (radius * radius * radius));

   printf("Radius = %.1f\n", radius);
   printf("Volume = %.3f\n", volume);

   return 0;

}

Wouldn't you know! It compiled and worked just fine, too!! Gave the same answer I calculated manually on my calculator (which is 4188.787, rounded as the final decimal repeats). I'm so thrilled!

The second part of the exercise stated to alter the program so the user enters the radius before the calculation is made. Here goes:

#include <stdio.h>

int main(void)
{
   float radius, volume;

   printf("Enter radius of sphere: ");
   scanf("%f", &radius);

   volume = (4.0f/3.0f * 3.14159 * (radius * radius * radius));
   printf("Volume of sphere: %.3f\n", volume);

return 0;
}

Ahh, works perfectly. So I now have a fifteen line program that calculates the volume of a sphere... You know, just in case. ;D