DOC PREVIEW
CHESAPEAKE CSC 103 - CSC 103 Chapter 2: Input, Processing, & Output

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

10/26/20081CSC 103Chapter 2:Input, Processing, &OutputStarting Out with Programming Logic & Design First Editionby Tony GaddisModifications by M. L. Malone August 082-12.1 Designing a Program2.2 Output, Input, and Variables2.3 Variable Assignment and Calculations2.4 Variable Declarations and Data Types2.5 Named Constants2.6 Hand Tracing a Program2.7 Documenting a Program2-2` The foundation of good programming: DESIGN!` Examples of Design Tools◦ Flowcharts◦ Pseudocode◦ Structure Charts` Implementation◦ Design Æ Code2-3` Implementing in a programming language` No syntax errors` Source code Æ object code Æbl dexecutable code` Run & test◦ Logic errors?◦ Runtime errors?` Debugging◦What is this?2-4If you want to get technical, what step is missing here?2-51. ANALYSIS◦ Define WHAT tasks the program is to perform.• What does the customer want?2. DESIGN steps to perform the tasksClhbd•Create an algorithm, or step-by-step directions to solve the problem.• Use flowcharts, structure charts, and/or pseudocode2-610/26/20082` Fake code used as a model for programs` No syntax rules` Should be easily translated to actual code` Ex:Display “Enter the number of hours”Input hoursDisplay “Enter the hourly pay rate”Input payRateSet grossPay = hours * payRateDisplay “The gross pay is $”, grossPay2-7` A diagram that graphically depicts the steps that take place in a programFigure 2.2 Flowchart for the pay calculating program2-8Terminator used for start and stopParallelogram used for input and outputRectangle used for processes“Enter the number of hours the employee worked” is a STRING LITERAL•Appears in the code•Enclosed in quotation marksWhat happens here?2-9Here?Here?` Output ◦ Information or data is generated and displayed to user in meaningful form` Input Dt tht i◦Data that a program receives◦ Raw facts and figures…` Variables◦ Storage locations in memory for data2-10Many programs typically follow 3 steps:1It id1.Input received2. Process performed on input3. Output produced2-11`Display◦ Keyword to show output to the screen` Sequence◦ Lines of code execute in order in which they appear` String Literal◦ Sequence of characters (ex “Hello world!”)2-12Figure 2-5 Output of Program 2-1Figure 2-4 The statements execute in order10/26/20083`Input◦ Keyword to take values from the user of the program◦ (Generally…) Precede with a prompt unless eventdriven (ie input is mouseunless event-driven (ie input is mouse click)◦ Values stored in variables2-13 2-14` DEFINING VARIABLE NAMES◦ One word, no spaces◦ Avoid punctuation characters◦ Generally, the first character cannot be a number◦ Meaningful name best` Popular naming conventions◦ “camelCase” a popular naming conventionx Ex: hourlyPay, taxPaidLastYear◦ Using underscoresx Ex: hourly_pay, tax_paid_last_year2-15` Can come from user input` Can be set via an assignment statement` Ex: Set price = 20 or price Å 202-16` Performed using mathematical operators` Expression normally stored in variable a variable` Ex: Set sale = price – discount ORsale Å price - discountTable 21Common math operators2-17Table 2-1 Common math operatorsSet ans = 20-8*2Value of ans?Why?Why?2-1810/26/20084` Parenthetical expressions` Operations using exponents (^)` Multiplications (*), divisions, modulus (mod) as they appear left to right(mod) as they appear left to right` Additions & subtractions as they appear left to right2-19` Ex: translate this formula into a programming expression in pseudocode:P-QM = ------ + 8N(6+X)32-20` In many programming languages (C, C++, Java, Python, and others…)◦ Beware of integer division!E Set n m 15/4◦Ex: Set num = 15/4x What is the value of num?2-21`variable declaration ◦ includes a variable’s name and a variable’s data type` Data Type◦ defines the type of data you intend to store in a iblvariable◦ Exs of simple data types◦ Integer – stores only whole numbers◦ Real – stores whole or decimal numbers◦ String – any series of characters`Ex in pseudocode: Declare real grossPay`Ex in C++: float grossPay;2-22` To avoid logic errors at runtime` What does “initializing a variable” mean?This2-23This algorithm as a flow chart?Alternatively…Declare Real test1, test2,test3, average2-2410/26/20085` What’s wrong with this?Declare Real num1Declare Integer num2Set num2 = num1` What about…Declare Real num1Declare Integer num2Set num1 = num22-25` All “Declare” statements can be placed into one processing symbol in a flow chart, as in……Declare Real num1Declare Integer num2…2-26` A name that represents a value that cannot be changed as program executes` Makes programs more self explanatory` Easy to change◦ Appears only in one place!` Less human error` Exs:xConstant Real PI = 3.14159xConstant Real INTEREST_RATE = 0.0692-27` A simple debugging technique for locating hard to find errors in a program` Involves creating a chart with a column for each variable, and a row for each line of code2-28Figure 2-14 Program with the hand trace chart completed`External documentation ◦ describes aspects of the program for the user◦ sometimes written by a technical writer`Internal documentation ◦ explains how parts of the program works for the programmerprogrammer◦ also known as comments ◦often distinguished within the program with “//”◦Ex: From a C++ program Æ next slide2-29// TriArea calculates the area of a triangle given the vertices//-------------------------------------------------------------------------------float TriArea(int x1, int y1, int x2, int y2, int x3, int y3){float side1, side2, side3, // sides of trianglesarea, // area of the trianglehalfPerim; // 1/2 of perimeter// find side lengthsid1 L th(1212)2-30side1 = Length(x1,x2,y1,y2); side2 = Length(x2,x3,y2,y3); side3 = Length(x3,x1,y3,y1);// find ½ perimeterhalfPerim = (side1+side2+side3)/2;// calculate area by Heron's formulaarea =


View Full Document

CHESAPEAKE CSC 103 - CSC 103 Chapter 2: Input, Processing, & Output

Download CSC 103 Chapter 2: Input, Processing, & Output
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CSC 103 Chapter 2: Input, Processing, & Output and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CSC 103 Chapter 2: Input, Processing, & Output 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?