Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 1: Introduction to Programming

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-2 1.1 What is Programming? A program is a list of instructions that is executed by a computer to accomplish a particular task. Creating those instructions is programming Program development cycle: –Analyze the problem –Design a program to solve the problem –Code the program –Test the program

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-3 1.2 Basic Programming Concepts A simple programming problem: convert a price from British pounds into U.S. dollars. Pseudocode Input the price of the item, PoundPrice, in pounds Compute the price of the item in dollars: Set DollarPrice = 1.62 * PoundPrice Write DollarPrice –Variables used: PoundPrice and DollarPrice –Constants: 1.62

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-4 BASIC and C++ Code BASIC Code: DIM PoundPrice As Double DIM DollarPrice As Double INPUT PoundPrice LET DollarPrice = 1.62 * PoundPrice PRINT DollarPrice END C++ Code: void main(void) { float PoundPrice, Dollar Price; cout << “Enter price in Pounds.”; cin >> PoundPrice; DollarPrice = 1.62 * PoundPrice; cout << DollarPrice; }

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-5 Data Input Input operations get data into the programs A user is prompted for the data to be entered –This text uses the word Write to indicate a prompt for input –The word Input indicates that a user has entered a value –Example: Write “Enter the price in pounds” Input PoundPrice

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-6 Variables and Constants Data is input into a program variable. A variable is a named piece of memory whose value can change during the running of the program. Example: Write “Enter the price in pounds” Input PoundPrice The variable is PoundPrice. A value which cannot change is a constant. In this example, the constant is 1.62

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-7 Naming Variables Rules are language dependent Common rules –All variable names must be one word –Spaces are never allowed –Variables cannot begin with a number Good practices –Names should be meaningful –Long names are allowed but names should be as short as possible, yet still be meaningful –Hungarian notation

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-8 Variable Name Examples Some examples: Miles_traveled is fine Miles Traveled is not (space) TaxRate_1 is fine 1_TaxRate is not (begins with a number) Variable1 is fine but not meaningful What’s wrong with these? My Number 2_4_6_8_go CowWhoJumpedOverTheMoon

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-9 1.3 Data Processing and Output  Set DollarPrice = 1.62 * PoundPrice The above statement is a processing statement. Take the value in the variable PoundPrice, multiply it by 1.62, and set the value of the variable DollarPrice to the result of the multiplication.  Write DollarPrice Output the value in DollarPrice to the monitor. Assignment statements change the value in a variable  Set counter = counter + 1 Take the value of counter, add 1, and store the result back in the same variable.

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-10 Arithmetic Operations +Addition2+3=5 -Subtraction7-3=4 *Multiplication5*4=20 /Integer Division12/5=2 /Real Division12.0/5.0=2.4 ^Exponentiation2^3=8 %Modulus14%3=2

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-11 Hierarchy of Operations 1st: perform operations inside parentheses (from inside out if more than one) 2nd: perform exponentiation 3rd: do multiplications, divisions, and modulus from left to right (if there are more than one) 4th: do additions and subtractions from left to right (if there are more than one)

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-12 Example of Hierarchy of Operations 3 * (6 + 2) / 12 – (7 – 5) ^ 2 * 3 = ? ( ) first: = 3 * 8 / 12 – 2 ^ 2 * 3 ^ next: = 3 * 8 / 12 – 4 * 3 Leftmost * next: = 24 / 12 – 4 * 3 Division next:= 2 – 4 * 3 Multiply next: = 2 – 12 Subtract last: = -10

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-13 Data Output Data that is sent from the program to the screen, printer, or to a file is output. In pseudocode, the following statement will display the value of the variable to the screen and send the cursor to the next line: Write DollarPrice Given the following statement: Write “The price of the item is”, DollarPrice, “ dollars” The output will look like this: The price of the item is 162 dollars Note that the text inside the “ ” is output to the user as is, and it is the value in the variable that is output.

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-14 Annotate the Output If the output consists of numbers or any data that has no explanatory text with it, you should annotate your output – this means to add some text so the user knows what the output means. Example: if Test1 and Test2 are 2 exam scores for a student: Average = (Test1 + Test2) / 2 Write “The student’s average is: “  annotated Write Average

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-15 1.4 Types of Data Numeric Data –Integer data (whole numbers) 10 25 -45 0 –Real (Floating point) data (numbers that have a decimal point) 23.5 -5.0 –Note: 5 and 5.0 are stored differently in a computer even though they have the same value. The first, 5 is an Integer but the second, 5.0 is a Real number. Character data (alphanumerics) –All the characters you can type at the keyboard –The type is String or Character

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-16 The Declare Statement Variables should be declared to identify what the type is. We use the following: –Declare MyWholeNumber As Integer –Declare MyDecimalNumber As Real –Declare MyText As String

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-17 Exponential Notation Scientific notation: 680,000 = 6.8 x 10 5 1,502,000,000 = 1.502 x 10 9 0.068 = 6.8 x 10 -2 0.00001502 = 1.502 x 10 -5 Exponential notation: 680,000 = 6.8E+5 1,502,000,000 = 1.502E+9 0.068 = 6.8E-2 0.00001502 = 1.502E-5

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-18 Character String Data Character: any symbol that can be typed at the keyboard Character string: a sequence of characters Character and String Data Types: –Declare MyInitial As Character declares a variable named MyInitial as a Character type –Declare MyName As String declares a variable named MyName as a String type

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-19 Concatenation Concatenation takes two strings and joins them to create a string result The concatenation operator is symbolized, in pseudocode, with a + sign Example: if: String1 = “yellow” and String2 = “duckie” then the statement: Set MyFriend = String1 + String2 results in: MyFriend = “yellowduckie”

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-20 Pseudocode Language (Ch 1) Our pseudocode language will be expanded throughout the text. In this chapter we learned how to input, output, assign values to variables, and perform arithmetic calculations. InputAssignment Input VariableSet Variable = 10 Set Variable = AnotherVariable

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-21 Pseudocode Language (Ch 1) OutputArithmetic Operations Write “literal text” Write Variable Write “literal text”, Variable ( ) ^ * / % + -

22 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1-22 Questions & Discussion


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and."

Similar presentations


Ads by Google