Download presentation
Presentation is loading. Please wait.
Published byMarcus Johnston Modified over 8 years ago
1
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement; /*body of code */ } Building programs by divide and conquer Length of a function One task per function End statement with ; Function has a return type Must have a main routine Use void to indicate no parameters passed Comments marked with /* */
2
NA2204.1jcmt CSE 1320 Intermediate Programming C Ins and Outs (Puts) Input commands scanf - formatting for any type of data getchar - read a single char gets - get a line of text ‘Readin’ Output commands printf - formatting for any type of data putchar - print a single char puts - put a line of text w‘Ritin’
3
NA2204.1jcmt CSE 1320 Intermediate Programming C Arithmetic (the third ‘R’) All the usual suspects ( ) : parentheses *, /, % : multiply, divide, remainder +, - : add, subtract Operators have precedence - all symbols have it No built-in power operator - pow(x) function
4
NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Read three integers from the user at the keyboard, sum them, take their average, and then print out all of the original numbers, the sum and the average with labels to the screen. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff
5
NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Read numbers What command to use? Syntax? scanf(“%d %d %d”, num1, num2, num3); But does the computer know what num1 is? Is it declared? Does the user know what to enter? Give them a clue.
6
NA2204.1jcmt CSE 1320 Intermediate Programming Our story so far … int main (void) {/* declarations */ int num1, num2, num3; /*body of code */ printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); }
7
NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) Now what? –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Sum the numbers What command? Syntax? sum = num1 + num2 + num3; But what is sum? Drat, another declaration. Notice that = means ‘assignment’ not equality. = is right side assigned to left
8
NA2204.1jcmt CSE 1320 Intermediate Programming Our story continues … int main (void) {int num1, num2, num3, sum; /*body of code */ printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; }
9
NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Take the average Avg is sum over number of - ergo, must find the sum first. avg = sum / 3; Declare avg. What are these things we’ve been declaring? What is a variable and why do we need it?
10
NA2204.1jcmt CSE 1320 Intermediate Programming Almost there… int main (void) {int num1, num2, num3, sum, avg; printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; avg = sum / 3; /* take input average */ }
11
NA2204.1jcmt CSE 1320 Intermediate Programming How would I write a program to.. Actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Print stuff Need to print out what the user gave, then what we calculated. printf(“The inputs were %d, %d, %d”, num1, num2, num3); printf(“The sum is %d and the average is %d”, sum, avg); What are we going to realize about the average value?
12
NA2204.1jcmt CSE 1320 Intermediate Programming And now… int main (void) {int num1, num2, num3, sum, avg; printf(“Please enter three integers on a single line then press RETURN”); scanf(“%d %d %d”, num1, num2, num3); sum = num1 + num2 + num3; avg = sum / 3; /* take input average */ printf(“The inputs were %d, %d, %d”, num1, num2, num3); printf(“The sum is %d and the average is %d”, sum, avg); }
13
NA2204.1jcmt CSE 1320 Intermediate Programming Have I written a program to.. Do the actions? –Read numbers (3 times) –Sum the numbers (When should this happen?) –Take the average (what do we need to know first?) –Print stuff Structure the progam? Correct name and parameters? All variables declared? All actions performed? Yes. IS IT DONE?
14
NA2204.1jcmt CSE 1320 Intermediate Programming No. Is it all correct? What about the average value? What could be changed? Anything else amiss? After the average value is fixed - is it done then? Maybe. Isn’t this fun!?! :)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.