1 00:00:00,000 --> 00:00:02,830 >> SPEAKER 1: Let's write a program that prompts the user for a positive 2 00:00:02,830 --> 00:00:05,950 integer, n, and then prints out the sum of all the numbers 3 00:00:05,950 --> 00:00:07,980 between 1 and n. 4 00:00:07,980 --> 00:00:10,580 Well, here we have main, which I've already written in advance. 5 00:00:10,580 --> 00:00:13,520 And notice here at the top of main, I declare an int n. 6 00:00:13,520 --> 00:00:16,079 >> I then, inside of a do while loop, first print out 7 00:00:16,079 --> 00:00:17,530 positive integer, please. 8 00:00:17,530 --> 00:00:21,070 Then I proceed to get an integer from the user with the CS50 library's get 9 00:00:21,070 --> 00:00:22,070 int function. 10 00:00:22,070 --> 00:00:26,410 And then in my while condition here, I make sure that n is greater than or 11 00:00:26,410 --> 00:00:30,480 equal to 1 before I actually proceed to do something with that value. 12 00:00:30,480 --> 00:00:31,520 >> What do I do next? 13 00:00:31,520 --> 00:00:34,690 Well, I call a function that I'm going to call sigma, representative of the 14 00:00:34,690 --> 00:00:37,700 capital sigma that you might have recalled from math classes that 15 00:00:37,700 --> 00:00:40,860 indicates that you want to sum something from one value to another. 16 00:00:40,860 --> 00:00:44,540 And whatever that function returns as its return value, I'm going to store 17 00:00:44,540 --> 00:00:46,500 in a variable called answer. 18 00:00:46,500 --> 00:00:50,280 >> Finally, in my last line in main, I'm going to print out what answer is. 19 00:00:50,280 --> 00:00:52,840 Of course, we haven't yet implemented this function sigma. 20 00:00:52,840 --> 00:00:54,590 So how do we go about doing that? 21 00:00:54,590 --> 00:00:58,040 >> Well, at the bottom of my file, I'm going to proceed to declare a function 22 00:00:58,040 --> 00:00:59,450 that returns an int. 23 00:00:59,450 --> 00:01:01,630 And I'm going to call that function sigma. 24 00:01:01,630 --> 00:01:06,340 And I'm going to specify that as input that function accepts also an int. 25 00:01:06,340 --> 00:01:09,800 And I'll call it just, to be distinct, m instead of n. 26 00:01:09,800 --> 00:01:12,120 But we could have called it the most anything we'd like. 27 00:01:12,120 --> 00:01:14,930 >> Inside of the body of this function I'm going to proceed to use a familiar 28 00:01:14,930 --> 00:01:16,420 construct, namely a loop. 29 00:01:16,420 --> 00:01:19,010 But I'm also going to do a bit of sanity checking to make sure that the 30 00:01:19,010 --> 00:01:22,340 user doesn't provide me with a number that I'm not expecting. 31 00:01:22,340 --> 00:01:28,010 In particular, I'm going to do if m is less than 1 and, somewhat arbitrarily, 32 00:01:28,010 --> 00:01:31,280 I'm simply going to return 0 if the number is not a positive 33 00:01:31,280 --> 00:01:32,800 integer as I expect. 34 00:01:32,800 --> 00:01:36,920 >> Then I'm going to declare a variable called sum and initialize it to 0. 35 00:01:36,920 --> 00:01:40,810 This will ultimately store the sum of all of the numbers between 1 and m. 36 00:01:40,810 --> 00:01:43,550 And then I'm going to use a familiar forward loop construct. 37 00:01:43,550 --> 00:01:50,272 For int i gets 1, i is less than or equal to m, i plus plus. 38 00:01:50,272 --> 00:01:54,010 And then, within the body of this loop, I'm simply going to do sum 39 00:01:54,010 --> 00:01:56,350 equals sum plus i. 40 00:01:56,350 --> 00:02:01,900 Or, more simply, sum plus equals i, which achieves the same result. 41 00:02:01,900 --> 00:02:04,810 >> And then lastly, I need to return the sum that I've computed. 42 00:02:04,810 --> 00:02:07,640 So I add in return sum. 43 00:02:07,640 --> 00:02:08,560 >> Now I'm not done yet. 44 00:02:08,560 --> 00:02:11,360 I need to teach C that this function actually exists. 45 00:02:11,360 --> 00:02:14,400 And so atop my file I'm going to declare what we've called a function 46 00:02:14,400 --> 00:02:18,270 prototype, identical to the signature that I used when defining the function 47 00:02:18,270 --> 00:02:19,250 a moment ago. 48 00:02:19,250 --> 00:02:22,450 >> Specifically, just above main, I'm going to type int 49 00:02:22,450 --> 00:02:26,080 sigma, int m, semicolon. 50 00:02:26,080 --> 00:02:29,240 Not implementing the function again, simply declaring it. 51 00:02:29,240 --> 00:02:32,800 If I now save, compile, and run this program, let's see what I get. 52 00:02:32,800 --> 00:02:37,460 Make sigma 0 dot slash sigma 0. 53 00:02:37,460 --> 00:02:41,050 And now let's provide a positive integer like 2, which should give me 54 00:02:41,050 --> 00:02:45,920 three, because the values between 1 and 2 are 1 plus 2 equals 3. 55 00:02:45,920 --> 00:02:47,300 And indeed, that's what I get. 56 00:02:47,300 --> 00:02:49,940 >> Let's run it again, this time with, say, 3. 57 00:02:49,940 --> 00:02:53,470 So I should get 1 plus 2 plus 3 should give me 6. 58 00:02:53,470 --> 00:02:54,740 And indeed, I do get 6. 59 00:02:54,740 --> 00:02:57,380 >> And let's try one last value, say 50. 60 00:02:57,380 --> 00:03:01,160 And 1,275 is our answer. 61 00:03:01,160 --> 00:03:02,253