Download presentation
Presentation is loading. Please wait.
Published byGavin Daniel Modified over 9 years ago
1
CS 108 Computing Fundamentals Notes for Thursday September 10, 2015
2
printf( ) A standard output function found in stdio.h Displays info to the monitor… here’s an example: printf ( " \n I hope you C things my way. \n " ) ; We are calling the printf( ) … we are sending the called printf( ) something to use when it performs its magic… that something that is being sent to the function in any function call is officially known as an argument or an actual argument or an actual parameter which is everything between the ( ) of the function call… the arguments list or actual parameters/actual arguments list may be empty (this means that nothing is sent to the function when it is called)… it is incorrect to place the keyword void here (in the function call's arguments list)
3
More printf( ) printf(" The batting average is %f ", batting_average); Arguments / Actual arguments / Actual parameters (between the ( ) of the call) Format string (between the " " ) Placeholders or format specifiers or conversion specifiers (look for %)… in this case, just one: %f Data list ( constant(s), identifiers for variable(s), function call(s), and/or expression(s) )… in this case: batting_average … which is a variable identifier/name Data list identifiers (constant(s) or function call(s) or variable name(s) or expression(s)) must correspond one-to-one, by position, with the format specifier(s) in the format string
4
scanf( ) scanf( ) is standard input function found in stdio.h Copies data from an input device (usually the keyboard) into a variable(s) address (es) specified as an argument/ actual argument/actual parameter scanf( " %f ", &batting_average) ; Actual arguments/Actual parameters (between the ( ) of the call) Format string Format specifiers or placeholders or conversion specifiers Address list ( variable name(s) ) preceded by & ) & is the “address of” operator and translates to “the address of variable____ ” Do not ever place text inside the scanf( ) format string; this is a place for format specifiers or spaces only
7
The "Urban Inside-Out 1-Step-at-a-Time Method" of Developing Programs 1. Determine desired output(s) and input(s), then develop your algorithm 2. Place the algorithm as a series of comments inside the main( ) block of statements (use my "C Program Template") as a guide 3. Focus on the first step of the algorithm 4. Begin coding by translating this step of the algorithm to C source-code (save changes!!) 5. Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) 7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) 8. Comment thoroughly (save changes, compile, and test one last time) 9. Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8 until all steps of the algorithm have been satisfied 10. Terminate
8
Practice Obtain the length and the width of a rectangle from a user, calculate the area of the rectangle and then display it. For this exercise we’re interested in whole numbers only. First step: determine your input(s) and output(s), then develop an algorithm… you have 5 minutes
9
Input(s) and Output(s) ? Obtain the length and the width of a rectangle from a user, calculate the area of the rectangle and then display it. For this exercise we’re interested in whole numbers only. Input(s): 1) length and 2) width Output(s): 1) area of the rectangle
10
Practice Algorithm 1. Greet user (tell user the purpose of the program) 2. Prompt the user to enter the length of a rectangle 3. Read user's input for length 4. Prompt the user to enter the width of the rectangle 5. Read the user's input for width 6. Calculate the area (formula: multiply length times width) 7. Display the area to the screen 8. Sign-off with the user 9. Terminate the program
11
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (1) 1. Determine desired output(s) and input(s), then develop your algorithm 2. Place the algorithm as a series of comments inside the main( ) block of statements (use my "C Program Template") as a guide 3. Focus on the first step of the algorithm 4. Begin coding by translating this step of the algorithm to C source-code (save changes!!) 5. Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) 7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) 8. Comment thoroughly (save changes, compile, and test one last time) 9. Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8 until all steps of the algorithm have been satisfied 10. Terminate
12
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (2) 2. Place the algorithm as a series of comments at the top of the main( ) block of statements Note: make sure that your Putty window is 90 - 92 characters wide… a narrower setting will cause unwanted "wrapping" of text Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10a.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10a.html
13
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (3) 3. Focus on the first step of the algorithm 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) 2. PROMPT THE USER TO ENTER THE LENGTH OF A RECTANGLE 3. READ USER'S INPUT FOR LENGTH 4. PROMPT THE USER TO ENTER THE WIDTH OF THE RECTANGLE 5. READ THE USER'S INPUT FOR WIDTH 6. CALCULATE THE AREA BY MULTIPLYING LENGTH TIMES WIDTH 7. DISPLAY THE AREA TO THE SCREEN 8. SIGN-OFF WITH THE USER 9. TERMINATE THE PROGRAM
14
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (4) 4. Begin coding by translating this step of the algorithm into C source-code (save changes!!) 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) This program calculates the area of a rectangle. Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10b.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10b.html This is not yet complete… this “string of text” that we want displayed to the screen needs some C-specific additions (we need to transform this text string into C code that contains this text string that will be displayed to the screen).
15
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (5) 4. Begin coding by translating this step of the algorithm into C source- code (save changes!!) 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) printf( " \n This program calculates the area of a rectangle. \n " ) ; Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10c.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10c.html This C statement completes the task of algorithm step #1… but we need to add more supporting C code to make it work… on to Inside- Out Method Step 4
16
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (6) 5. Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) # include int main ( void ) { printf( " \n This program calculates the area of a rectangle. \n " ) ; return ( 0 ) ; } Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10d.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10d.html
17
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (7) 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) gcc sep10.c 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) # include int main ( void ) { printf( " \n This program calculates the area of a rectangle. \n " ) ; return ( 0 ) ; }
18
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (8) 7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary)./a.out 1. GREET USER AND TELL USER THE PURPOSE OF THE PROGRAM # include int main ( void ) { printf( " \n This program calculates the area of a rectangle. \n " ) ; return ( 0 ) ; }
19
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (9) 8. Comment thoroughly (save changes, compile, and test one last time) 1. GREET USER AND TELL USER THE PURPOSE OF THE PROGRAM /*****************************************************************************/ /* #include PREPROCESSOR DIRECTIVE(S) (IF ANY) ARE DIRECTLY BELOW */ /*****************************************************************************/ #include // PREPROCESSOR DIRECTIVE THAT ALLOWS THE USE OF printf( ) Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10e.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10e.html
20
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (10) 9. Move on to the next step of the algorithm and proceed with "Inside- Out Method" step 4 through 8 until all steps of the algorithm have been satisfied. Next algorithm step is: 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE
21
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (11) 4. Begin coding by translating this step of the algorithm into C source-code (save changes!!) 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE Please enter the length of the rectangle: Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10f.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10f.html
22
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (12) 5. Add any additional C code necessary to support the translation of the single algorithm step… save changes!! 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE printf( " \n Please enter the length of the rectangle: " ) ; Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10g.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10g.html
23
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (13) 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) gcc sep10.c 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE
24
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (4) 7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary)./a.out 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE
25
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" (15) 8. Comment thoroughly (save changes, compile, and test one last time) 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE No additional commentary necessary for this step
26
9. Move on to the next step of the algorithm and proceed with "Inside- Out Method" step 4 through 8 until all steps of the algorithm have been satisfied. Next algorithm step is: 3. READ THE USER'S INPUT FOR LENGTH
27
4. Begin coding by translating this step of the algorithm into C source- code (save changes!!) 3. READ THE USER'S INPUT FOR LENGTH scanf( "%f", &length ) ; Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10h.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10h.html
28
5. Add any additional C code necessary to support the translation of the single algorithm step… save changes!! 3. READ THE USER'S INPUT FOR LENGTH int main ( void ) { // Marks the beginning of main function's statements /**********************************************************************************************************/ /* LOCAL VARIABLE(S) FOR main( ) (IF ANY) ARE DECLARED DIRECTLY BELOW */ /**********************************************************************************************************/ float length = 0.0 ; Cannot store a value to a variable (which is accomplished on the previous slide's purple code) unless a variable is declared/created (accomplished by the purple code on this slide) Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10i.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10i.html
29
6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) gcc sep10.c 3. READ THE USER'S INPUT FOR LENGTH
30
7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary)./a.out 3. READ THE USER'S INPUT FOR LENGTH Adding a "test print statement" (see the black printf( ) call below) is a test that helps confirm that your code is correct scanf( "%f", &length ) ; printf( " \n\n TEST TEST the value of length is %d \n\n ", length ) ; Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10j.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10j.html Mistake!
31
8. Comment thoroughly (save changes, compile, and test one last time) 3. READ THE USER'S INPUT FOR LENGTH /***********************************************************************************************************/ /* #include PREPROCESSOR DIRECTIVE(S) (IF ANY) ARE DIRECTLY BELOW */ /***********************************************************************************************************/ #include // PREPROCESSOR DIRECTIVE THAT ALLOWS THE USE OF printf( ) // AND scanf( ) /**********************************************************************************************************/ /* ALGORITHM STEP 3: READ TO USER'S INPUT FOR LENGTH */ /**********************************************************************************************************/ scanf( "%d", &length ) ; Added the purple comment above and removed the test printf( ) call Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_10k.htmlhttp://www.cs.sunyit.edu/~urbanc/cs_108_sep_10k.html
32
9. Move on to the next step of the algorithm and proceed with "Inside- Out Method" step 4 through 8 until all steps of the algorithm have been satisfied. 4. PROMPT THE USER TO ENTER THE WIDTH OF THE RECTANGLE
33
Let's do the next several steps together using nano The completed program is found at the following link: http://web.cs.sunyit.edu/~urbanc/cs_108_sep_10L.html
34
More About Compiling./a.out is overwritten each time a new program is compiled this way… not great if you want to save your compiled programs Instead, use the following gcc compiler command general form: gcc file_name.c -o file_name.exe example: gcc 3.c -o 3.exe (the -o switch allows your to name your output file )
35
nano &pico Shortcuts while holding ctrl press w followed by t which produces a prompt that asks you on which line you wouyd like the cursor to be positioned pressing ctrl and c produces the number of the line on which the cursor is currently positioned pressing ctrl and y moves the cursor up one page pressing ctrl and v moves the cursor down one page pressing ctrl and k deletes an entire line
36
C Functions QuickStart What is a function? Why are functions useful? What are the characteristics of a function? Examples!!
37
What is a function? Function can be thought of as a module or a subprogram A named block of source-code that is designed to perform a specific task within a program Examples of functions with which we’ve worked: main( ), printf ( ), and scanf( )
38
Functions Are Very Flexible Tools Many useful functions are found in libraries See Table 3.1 and Appendix B in the textbook Some functions are general while others are very specific in nature and use Some functions have parameters/arguments and some do not Some functions allow a varying parameters… such as printf( ) and scanf( ) Some functions return a single value and some do not return a single value (function cannot return multiple values) Those functions that don't return a single value are "void functions" Function libraries are not panaceas: we need ability to create our own functions (programmer-created functions… next week!!)
39
Let's Start With main ( ) Most basic general syntax guide for a program where main( ) is the only function the programmer must create (similar to your homework assignment) preprocessor directives //comments main-function-return-value-data-type main ( void ) { //comments variable declaration statements ; //comments instruction statements ; //comments return statement ; //comments } //comments
40
Let's Work on an Example Write a C program that satisfies the following requirements: main( ) is the only programmer-created function the program obtains an integer value from the user the program displays the user's input along with the square and the cube of the user's input What should we do first?
41
Let's Work on an Example continued What should we do first? Determine input(s) and output(s) Develop an algorithm
42
Let's Work on an Example continued What should we do first? Determine input(s) and output(s) Inputs: an integer from the user Outputs: the user's input, its square, and its cube Develop an algorithm
43
Let's Work on an Example continued Our algorithm!! Remember: algorithms should be about "what to do" not about "how to do it" 1. DISPLAY INTRO MESSAGE 2. PROMPT USER TO ENTER AN INTEGER 3. READ THE USER'S INPUT 4. CALCULATE THE SQUARE VALUE (INPUT * INPUT) 5. CALCULATE THE CUBE VALUE (INPUT * INPUT * INPUT) 6. DISPLAY THE USER'S INPUT, ITS SQUARED VALUE, AND ITS CUBED VALUE 7. DISPLAY AN ENDING MESSAGE 8. STOP
44
Let's Work on an Example continued Transfer your algorithm to a source-code file and make it a comment I recommend that you begin writing your C code by starting with the first step of you algorithm… this means that you start writing code "in the middle" of your program That why I call this the "Urban Inside-Out 1-Step-at-a-Time Method" of source code development We will develop this is class together using nano or pico and gcc
45
Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" 1. Determine desired output(s) and input(s), then develop your algorithm 2. Place the algorithm as a series of comments inside the main( ) block of statements (use my "C Program Template") as a guide 3. Focus on the first step of the algorithm 4. Begin coding by translating this step of the algorithm to C source-code (save changes!!) 5. Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) 7. Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) 8. Comment thoroughly (save changes, compile, and test one last time) 9. Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8 until all steps of the algorithm have been satisfied 10. Terminate
46
Let's play with our program and inject some errors to our source-code By injecting errors we can learn how those errors manifest themselves (at compile time or at run time, for example). First, let's remove the & from a scanf( ) (a very common mistake) Let's remove a variety of other piece of our program (always removing one piece at a time to see how that impacts compilation and then execution) Let's use Google to our advantage to help us troubleshoot Play with your food!! Seriously, play with our programs!! Develop some intellectual curiosity!
47
What is an Expression An expression is a sequence of operators and operands Constructed according to the language's syntax Evaluates to a specific data type's single value. Does not necessarily end with ; Operand: the name given to the entity or entities that are operated on by an operator ( entities: integer constant(s), real constant(s), named constant(s), function(s) and variable(s) ) Operator: a character or combination of characters that identifies a data operation, such as addition
48
What is a Statement A statement is a source code construct that is roughly equivalent to a sentence in natural language (English). A statement forms a complete unit of execution Often ends in a ; May declare one or more variables, May assign an expression's result to a variable, May repeatedly execute other statements in a loop, May control a loop, May makes a decision, and so on.
49
What is a Block A related collection of statements In C a block is enclosed within { }
50
Assignment Statements Assignment statements store constants or the results of some computation to a variable Assignment statements use the assignment operator = (remember, this doesn’t mean “equal” … think “gets” instead). The following are examples of assignment operator use: selection = 1; sum = 0; sum = sum + 1; batting_avg = hits / at_bats; selection = ‘N’; Assignment statements replace old values of a variable with new values
51
The Return Statement When a return statement is executed, program control is immediately passed back to the calling environment (such as the operating system or another function). If an expression follows the keyword return, the value of the expression is returned to the calling environment as well. A return statement has one of the following two forms: return; return expression;
52
The Return Statement return; return answer ; // Assuming answer is a variable return first + second ; // Assuming first & second are variables return ( a + b + c ); // Assuming a, b, and c are variables return ( 6 + 4 / 2 ); // All the operand are constants
53
Let's Work Another Example
54
Nano/pico Shortcuts (one more time) while holding ctrl press w followed by t which produces a prompt that asks you on which line you wouyld like the cursor to be positioned pressing ctrl and c produces the number of the line on which the cursor is currently positioned pressing ctrl and y moves the cursor up one page pressing ctrl and v moves the cursor down one page pressing ctrl and k deletes an entire line
55
GHP #4 See the class Web page Let’s discuss the template Consider using 2 Putty sessions simultaneously One session is open with editor (nano or pico) and your source- code file The second session is where you run gcc The beauty of 2 simultaneous sessions is that your compiler error messages are available for you to use AS you edit your source-code file
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.