Accidentally Over Ambitious (Part 2)


The above link goes to my CS 50 Dropbox folder that has the three wee programs I had to write.

Pennies.c asks the user for how many days in the month and how many pennies they initially have. It outputs how much those pennies add up to when treated exponentially. I need to learn how to include commas in large numbers. The course doesnt say it explicitly, but I believe the point behind this program was to experience how inaccurate computers really are. Its all bits and bytes and this is something to sit and go over with Dr. Dave. It’s somewhat foggy to me. I was also surprising to learn that C does not have an operator for exponentiation. I later learned that if you use the math.h library you have the pow function.

Greedy.c asks the user for how much change is due. It then computes the fastest way to reach that number with the least amount of coins. Starting with quarters and moving to pennies. Its pretty easy conceptually especially if you use a modulus operator but for some reason I cant get it to compile. Help.

Lastly, chart.c asks for how many specific types of sightings occurred between the sexes. It then needs to take that input, calculate it to a percentage of 80 asterisks (*) printed on the screen to act as a graph. Its pretty silly. The formula I came up with/came across doesn’t really seem to work. It prints all the * on the same line.

Overall, it rocked. It was a much more reasonable problem set for an introductory class than MIT provided. Also, I’ve taken to C much easier than I thought. Although its freakishly annoying when I forget something like a simple “;”. The copious documentation (and the fact that C apparently hasnt changed at all for about 15 or so years) helps. I’m curious to learn the actual differences between C and C++, C#, and Objective-C. Are they like brothers from the same mother, or cousins far removed from a distant sire and constantly seeking to usurp the throne of the other… oh wait, I’m mixing my studies with my enthusiasm for the new season of Game Of Thrones.

Speaking of which, I’m tired. Off to relax world.


6 thoughts on “Accidentally Over Ambitious (Part 2)

    • I can’t pull up my computer right now but if I recall it was something like, “cannot convert “%” to binary (int to float)”. Sometimes when fiddling around I got “int to int” but I was never able to really figure out the how or why. I typecasted the variable in question to an int but it kept saying it was a float.

      • Ok. I see several things worth investigating:
        Line 18 – You can clean up that condition. Not (greater than or equal to) is the same as what?
        Same line – what happens if I’m a joker and enter a figure like 1.0005 ?

        Line 22 – not sure what you’re doing there, but it’s probably not what you think you’re doing. If it was me, I’d create another int called centsDue, and then assign (changeDue*100) to centsDue (casting to int as necessary). Then I’d do the computations below on centsDue. I’m guessing your original error is due to the computations’ modulus operators working on changeDue – I don’t think modulus is valid in C for floats. Using centsDue will fix that (assuming that’s the problem).

        Line 36, 43, 50, 56: You are taking an int, casting it to a float, then assigning it to itself (which is still an int, because that’s how it is declared). Seems like a waste of time to me.

        Your printf’s for quarters/dimes/nickels/pennies look funny (%.2d). If I recall, ‘.2’ indicates 2 places to the right of the decimal, but the d indicates a signed integer. Why not just %d ?

        I did find my copy of A Book on C by Kelley and Pohl – probably the most useful C reference I’ve used. I’ll pass it along next time I see you.

  1. Ok, so first off: thank you! The difference that feedback makes is amazing.

    Anyway, I’ll upload the updated version and you can review.

    Line 18. Yeah good catch. I dont know why I bothered doing it that way. I changed it to “while (changeDue < 0).

    Line 22. I was trying to round any possible float to a workable int. However, you pointed out that its really not necessary if you just multiply by 100 (which I did) and then assign the variable to a new one. Although I'm still unsure why the round function and the casting didnt seem to work.

    As to the modulus, you must be right. I used the new int variable and it cleared things up.

    I also ditched the second round of casting from an int to a float because I also realized that it wasnt necessary. I think my intent behind it was so that I could have the use of a decimal. But then I realized I'm just counting the number of coins… not the number of coins and some slice of a coin. Yeah, logic 1, Joseph 0.

    Then, in a fit of excited boredom, I went and polished things a bit further and added an "if" condition to change the output if the coins are singular or multiple. I guess my affinity for grammar got the best of me!

    I like progress.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s