>> SPEAKER 1: Let's write a program that prompts the user for a positive
integer, n, and then prints out the sum of all the numbers
between 1 and n.
Well, here we have main, which I've already written in advance.
And notice here at the top of main, I declare an int n.
>> I then, inside of a do while loop, first print out
positive integer, please.
Then I proceed to get an integer from the user with the CS50 library's get
int function.
And then in my while condition here, I make sure that n is greater than or
equal to 1 before I actually proceed to do something with that value.
>> What do I do next?
Well, I call a function that I'm going to call sigma, representative of the
capital sigma that you might have recalled from math classes that
indicates that you want to sum something from one value to another.
And whatever that function returns as its return value, I'm going to store
in a variable called answer.
>> Finally, in my last line in main, I'm going to print out what answer is.
Of course, we haven't yet implemented this function sigma.
So how do we go about doing that?
>> Well, at the bottom of my file, I'm going to proceed to declare a function
that returns an int.
And I'm going to call that function sigma.
And I'm going to specify that as input that function accepts also an int.
And I'll call it just, to be distinct, m instead of n.
But we could have called it the most anything we'd like.
>> Inside of the body of this function I'm going to proceed to use a familiar
construct, namely a loop.
But I'm also going to do a bit of sanity checking to make sure that the
user doesn't provide me with a number that I'm not expecting.
In particular, I'm going to do if m is less than 1 and, somewhat arbitrarily,
I'm simply going to return 0 if the number is not a positive
integer as I expect.
>> Then I'm going to declare a variable called sum and initialize it to 0.
This will ultimately store the sum of all of the numbers between 1 and m.
And then I'm going to use a familiar forward loop construct.
For int i gets 1, i is less than or equal to m, i plus plus.
And then, within the body of this loop, I'm simply going to do sum
equals sum plus i.
Or, more simply, sum plus equals i, which achieves the same result.
>> And then lastly, I need to return the sum that I've computed.
So I add in return sum.
>> Now I'm not done yet.
I need to teach C that this function actually exists.
And so atop my file I'm going to declare what we've called a function
prototype, identical to the signature that I used when defining the function
a moment ago.
>> Specifically, just above main, I'm going to type int
sigma, int m, semicolon.
Not implementing the function again, simply declaring it.
If I now save, compile, and run this program, let's see what I get.
Make sigma 0 dot slash sigma 0.
And now let's provide a positive integer like 2, which should give me
three, because the values between 1 and 2 are 1 plus 2 equals 3.
And indeed, that's what I get.
>> Let's run it again, this time with, say, 3.
So I should get 1 plus 2 plus 3 should give me 6.
And indeed, I do get 6.
>> And let's try one last value, say 50.
And 1,275 is our answer.