1 / 52

CS 108 Computing Fundamentals Notes for Thursday September 4, 2014

CS 108 Computing Fundamentals Notes for Thursday September 4, 2014. printf( ). A standard output function found in stdio.h Displays info to the monitor… here’s an example: printf ( &quot; <br> I hope you C things my way. <br> &quot; ) ;

Download Presentation

CS 108 Computing Fundamentals Notes for Thursday September 4, 2014

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 108 Computing FundamentalsNotes for Thursday September 4, 2014

  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 calledprintf( ) something to use when it performs its magic… that something that is being sent to the function in any function call is officially know as an actual argument or actual parameter which is everything between the ( )of the function call… the actual parameters/ arguments list may be empty (this means that nothing is sent to the function when it is called)

  3. More printf( ) printf(" The batting average is %f", batting_average); • 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 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

  5. 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: develop an algorithm… you have 5 minutes…

  6. Practice Algorithm • Greet user (tell user the purpose of the program) • Prompt the user to enter the length of a rectangle • Read user's input for length • Prompt the user to enter the width of the rectangle • Read the user's input for width • Calculate the area (formula: multiply length times width) • Display the area to the screen • Sign-off with the user • Terminate the program

  7. 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 • Place the algorithm as a series of comments inside the main( ) block of statements (use my "C Program Template") as a guide • Focus on the first step of the algorithm • Begin coding by translating this step of the algorithm to C source-code (save changes!!) • Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) • Compile (troubleshoot/fix, save changes, and recompile, if necessary) • Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) • Comment thoroughly (save changes, compile, and test one last time) • Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8until all steps of the algorithm have been satisfied • Terminate

  8. 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 (use my "C Program Template" as a • guide (it's just above GHP #3 assignment and just under today's • slides)…you can cut-and-paste into a nano or pico file • Note: make sure that your SSH 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_04a.html

  9. 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

  10. 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_04b.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).

  11. 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_04c.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

  12. 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 <stdio.h> • 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_04d.html

  13. 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 sep4.c 1. GREET USER (TELL USER THE PURPOSE OF THE PROGRAM) # include <stdio.h> int main ( void ) { printf( " \n This program calculates the area of a rectangle. \n " ) ; return ( 0 ) ; }

  14. 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 <stdio.h> int main ( void ) { printf( " \n This program calculates the area of a rectangle. \n " ) ; return ( 0 ) ; }

  15. 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 <stdio.h> // PREPROCESSOR DIRECTIVE THAT ALLOWS THE USE OF printf( ) • Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_04e.html

  16. 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 8until all steps of the algorithm have been satisfied. • Next algorithm step is: 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE

  17. 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_04f.html

  18. 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_04g.html

  19. 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 sep4.c 2. PROMPT USER TO ENTER THE LENGTH OF A RECTANGLE

  20. 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

  21. 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

  22. 9. Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8until all steps of the algorithm have been satisfied. • Next algorithm step is: 3. READ THE USER'S INPUT FOR LENGTH

  23. 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_04h.html

  24. 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_04i.html

  25. 6. Compile (troubleshoot/fix, save changes, and recompile, if necessary) gcc sep4.c 3. READ THE USER'S INPUT FOR LENGTH

  26. 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_04.html Mistake!

  27. 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 <stdio.h> // 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 testprintf( ) call • Go to http://www.cs.sunyit.edu/~urbanc/cs_108_sep_04k.html

  28. 9. Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8until all steps of the algorithm have been satisfied. 4. PROMPT THE USER TO ENTER THE WIDTH OF THE RECTANGLE

  29. 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_04L.html

  30. More About Compiling • ./a.outis 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 )

  31. nano &pico Shortcuts • while holdingctrl 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

  32. C Functions QuickStart • What is a function? • Why are functions useful? • What are the characteristics of a function? • Examples!!

  33. 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 ( ), andscanf( )

  34. 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!!)

  35. 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

  36. 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?

  37. Let's Work on an Example continued • What should we do first? • Develop an algorithm!!

  38. 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

  39. Let's Work on an Example continued • Transfer your algorithm to the C_Program_Template • 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

  40. Let’s Write the Code Using the "Urban Inside-Out 1-Step-at-a-Time Method" • Determine desired output(s) and input(s), then develop your algorithm • Place the algorithm as a series of comments inside the main( ) block of statements (use my "C Program Template") as a guide • Focus on the first step of the algorithm • Begin coding by translating this step of the algorithm to C source-code (save changes!!) • Add any additional C code necessary to support the translation of the single algorithm step (save changes!!) • Compile (troubleshoot/fix, save changes, and recompile, if necessary) • Run and Test (troubleshoot/fix, save changes, recompile, rerun, retest and repeat, as necessary) • Comment thoroughly (save changes, compile, and test one last time) • Move on to the next step of the algorithm and proceed with "Inside-Out Method" step 4 through 8until all steps of the algorithm have been satisfied • Terminate

  41. 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!

  42. 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

  43. 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.

  44. What is a Block • A related collection of statements • In C a block is enclosed within { }

  45. 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

  46. 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; returnexpression;

  47. 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

  48. Let's Work Another Example

More Related