>> [MUSIC PLAYING]
>> ZAMYLA CHAN: Would you rather receive $10 million right now or receive
pennies every day of the month, where each subsequent day, you receive
double the amount of pennies you received the day before?
In pennies, we're going to prove why you should
definitely take the pennies.
Your job is to write pennies.c--
a program that first asks the user how many days there are in the month and
then asks how many pennies the user will receive on the first day.
The program should then calculate the amount that the user will have
accumulated at the end of the month and display it.
>> We can break this program up into a couple of tasks.
We'll need to prompt for multiple user inputs--
days and months, and pennies on first day.
We need to keep track of how much money the user has and update it every
day, doubling the amount of pennies that they receive.
And finally, we print their final total, in dollars.
>> Let's tackle user input first.
I'm going to open up apples.c, short program that deals
with inputs and outputs.
At the top here, I've included some of the libraries that I need--
CS50's library and a standard I/O library.
I print a prompt using the printf function from the
standard I/O library.
But the actual prompting of the user happens in the next line with this
GetInt function in the CS50 library.
>> GetInt obtains input from the user and ensures that the input is an integer.
So I store the user's input into integer i, subtract 1 from i, and then
print out the new value if i.
After compiling apples.c using the command "make apples," I have an
executable file apples.
Running that, I'll input 1.
The program tells me that I have 0 apples left.
And so that worked as expected.
>> So let's run this again and input negative 1.
Now, having a negative amount of apples doesn't really make sense, but
the program accepts it, because negative 1, well, it is an integer.
And now the program or the monster inside the program eats a negative
apple, and now I have negative 2 apples.
Hm, so that's one lesson--
even though the GetInt function and related functions, like GetFlow or
GetString get the right data type, you have to make sure that the input makes
sense for your program.
>> In pennies, it won't do just a prompt for an integer, you'll need to make
sure that the user inputs an integer that make sense given the context.
Month can only have 28, 29, 30, or 31 days.
We want to continuously demand a valid integer.
If they input an integer outside of our accepted bounds, we'll prompt them
again and again, until you give us a valid integer.
>> Recall the "repeat until" block in Scratch?
You can make a similar construct in C using a "while" loop
or "do-while" loop.
The while loop structure has a condition that must be met for the
body of the loop to execute.
>> This should look a little similar, because the condition is just like the
Scratch blocks with the angles and goes into the brackets
of the "while" loop.
So that condition will check whether the integer is within
the appropriate bounds.
You can prompt the integer using GetInt and then execute your "while"
loop, repeating the prompt and printing instructions, while the input
is not valid.
>> Another way to ensure correct user input is by using a "do-while" loop,
which is very similar to a While loop.
The "do-while" loop executes the code inside the body ones and then checks
whether the condition is met or not.
This is useful for obtaining user input, because you know that you need
to prompt them at least once.
If the condition is not met, the program will execute the line after
your "do-while" loop.
If the condition is met though, the loop will repeat
>> A "do-while" loop for validating user input will look something like this.
I declare a variable n, GetInt, and then repeat until n is valid.
And this process, I explained, was using the integer-data type, which
you'll use for the days of the month.
But we know that the number of pennies will pile up quickly, so in order to
store larger numbers, use the data type LongLong, but remember the same
principle of validation applies.
>> Awesome, so once we have two valid inputs from the user--
days in the month and the pennies on the first day--
we can move onto the next part of the program.
The first day, the user starts with however many pennies they specified
and the amount of pennies that they receive the next day is doubled.
So it makes sense to keep track of these two amounts--
how much money the user has and how many pennies they'll be given.
>> Now, doing something for 28 to 31 days is repetitive, so let's use a "for"
loop to iterate over the days in the month, updating the total and penny
amount every time.
"For" loop syntax contains the following--
an initialization, a condition, and an update.
Your "for" loop will initialize a variable, when it
first enters the loop.
If the condition is met, the body of the loop will execute.
Afterwards, the update will execute.
If the condition is still met, the loop will execute and update, and
repeat, as long as your condition evaluates to true.
>> Here's a "for" loop that divides an integer p by 2 a total of 10 times.
Note how I declared the integer p outside of my "for" loop, so that the
integer can be accessible outside the "for" loop.
After the month has passed, it's time to tell the user how much money
they've gotten.
Per the spec, you need to tell the user how many dollars, not how many
pennies, they have.
But so far, you've kept track of the total and the pennies using LongLongs,
which are whole numbers.
>> There are a 100 pennies to $1, so unless the number of pennies is a
multiple of 100, you'll need to represent decimal places.
The doubled autotype allows you to do this.
So how do we convert from representing pennies to dollars?
>> Let's take a look pi.c.
This program takes a LongLong big_pi and divides it by 100,000, and prints
the result to five decimal places.
If we wanted to display four decimal places, then we can just replace this
number here.
So let's save, compile pi, and then see the result, which we
expect to be 3.1415--
the first five digits of pi.
>> It isn't though.
And why?
Well, because big_pi is a LongLong, which is a whole number, which won't
keep track of decimals.
The modification I need to make is in the division.
I need to cast to a double here before dividing, because doubles can't keep
track of decimal places.
And I'm going to add a new line here for better formatting.
>> And here we have it--
3.1416.
You may notice that it prints 3.1416 instead of 3.1415 as expected.
That's because it rounds during the division process, as opposed to just
truncating the value.
Now you should be able to print the final total correctly, which finishes
the program.
>> Congratulations.
My name is Zamyla.
And this was pennies.
>> [MUSIC PLAYING]