CS 108 Computing Fundamentals Notes for Thursday September 10, 2015.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Chapter 6: User-Defined Functions I
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Introduction to a Programming Environment
Chapter 6: User-Defined Functions I
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
A First Program Using C#
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CS 108 Computing Fundamentals Notes for Tuesday, January 27, 2015.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Programming With C.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
CS 108 Computing Fundamentals Notes for Thursday, September 17, 2015.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS 108 Computing Fundamentals Notes for Tuesday, September 15, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
User-Written Functions
Chapter 6: User-Defined Functions I
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS 108 Computing Fundamentals Notes for Thursday, September 14, 2017
Chapter 2 - Introduction to C Programming
User-Defined Functions
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CS 108 Computing Fundamentals Notes for Thursday September 10, 2015

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)

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

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

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

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

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

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

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

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 characters wide… a narrower setting will cause unwanted "wrapping" of text Go to

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

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

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

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

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 ) ; }

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 ) ; }

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

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

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

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

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

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

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

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

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

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

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

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 Mistake!

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

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

Let's do the next several steps together using nano The completed program is found at the following link:

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 )

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

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

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( )

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

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

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?

Let's Work on an Example continued  What should we do first?  Determine input(s) and output(s)  Develop an algorithm

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

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

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

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

 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!

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

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.

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

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

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;

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 ( / 2 ); // All the operand are constants

Let's Work Another Example

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

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