PT1420 Week 2: Software Program Design I (I m not printing out these notes! Take your own.) Today we'll be discussing designing programs: Algorithms and errors Flowcharts and pseudocode Sequence structures Variables, constants, and the order of operations We re in the first part of Chapter 2, from pages 29 55: 2.1: Designing a Program, pages 29 34; 2.2: Output, Input, and Variables, pages 34 43; and 2.3: Variable Assignment and Calculations, pages 43 55. If you want a paper copy of the textbook, or of the PowerPoint I m not using, it s up at the front by the sign in sheet. We ll be working, to start, with a program which does the following: If I know how many hours I worked, and I know how much I get paid per hour, how much money am I owed? We ll later be adjusting this to tell me not only how much am I owed, but how much will I get after taxes. (We are probably not going to deal yet with overtime, but if we don t we ll discuss why.) The Three Step Process of Programming Broadly speaking, there are three big steps which programs do when they run. 1. They take some sort of input. (This step is sometimes, rarely, skipped.) 2. They do something (to it) some sort of process. 3. They produce some sort of output.
This is illustrated on page 34, the Input > Process > Output steps. We re looking today at the Process step. We ll take input from the keyboard and output to the screen. We ll use Raptor to make a flowchart and run it. The Software Development Process The software development cycle is really high level illustrated on page 30: The design the program step is the hard one. (This is part contract/specification to protect you from disagreements/lawsuits.) This is also where you d perhaps want to start making flowcharts or using pseudocode. You d then write code. The first type of error you might find are syntax errors. This is an error in programming which prevents the program from even running (or compiling). 3+ is an example of a math expression with a syntax error; that plus sign needs to have something after it. Once your program runs (there are no more syntax errors) it may still have bugs. (It will have bugs.) These will be logic errors. The program is doing what you said as opposed to what you meant.
This is an important distinction, syntax errors versus logic errors. Logic errors are harder to find. The design step involves working out the algorithm you intend your program to use. These are the steps you re going to use to use to perform the tasks. There s an example on page 31: The problem we had above (with the paycheck) could have an algorithm like this: 1. Ask the user how many hours he worked. 2. (Store that number of hours in a variable.) 3. Ask the user how much he makes per hour. 4. (Store that number, of dollars and cents, in a different variable.) 5. Calculate the pay, by multiplying those two variables together and storing that product in a third variable. 6. Display the pay the user should expect (that third variable). This algorithm is a sequential structure because the steps are performed in order straight down. The other two control structures are decision structures (if then branching) and repetition structures (loops). This algorithm has three variables: The number of hours worked The amount of pay per hour
Gross pay There are rules and good practice about naming variables. You can t start a variable name with a number. You can t have a space in a variable name. You can t have weird characters in variable names. For example, a%b is not a valid variable name, but it may not produce a syntax error because in some programming languages a%b means the remainder when a is divided by b (it s called modulo). You do want to make your variable names useful. HoursWorked is a good variable name in this program. Don t make them really long. You may see variables always starting with lower case, for example hoursworked. (This is called camel case.) You may be able to use an underscore, Hours_Worked, if that s useful. You don t want to use ALL CAPS: HOURSWORKED. In most environments, all caps means that a variable is a constant. For example, PI=3.1415 could be a definition of the PI variable as a constant 3.1415. We ll talk more about constants. You only want to use short variables like i, a, b, x, when you re doing something really short and obvious. For example, for (i=1, i<10, i++) { print i; } is a loop which just prints the numbers 12345678910. Using i is ok here because its use is brief and obvious. Back to this algorithm with variables: The number of hours worked: HoursWorked [number with decimals, a floating point number] The amount of pay per hour: PayPerHour [number with decimals, a floating point number]
Gross pay: GrossPay [number with decimals, a floating point number] We re going to look at making pseudocode for this task, and also at using Raptor to make a flowchart and running it. Pseudocode could look like: 1. Print How many hours did you work? 2. Input HoursWorked 3. Print How much is your hourly pay? 4. Input PayPerHour 5. Set GrossPay = HoursWorked * PayPerHour 6. Print Your paycheck should be for $, GrossPay One last topic: Variables, constants, and the order of operations We ve spoken briefly about variables. You want to name your variables sensibly. You want to figure out in advance which variables you need, and what sort of data is stored in them. Some common sorts of data are integers, floating point numbers, character, string (of characters), or boolean (true/false bits). Sometimes you define a variable as a constant, which means (1) the value won t change, (2) the value can t change, and (3) the compiler may do things to make your use of that variable more efficient. const PI=3.1415 const COMPANY_NAME= ITT Technical Institute, Inc. const MINIMUM_WAGE=7.25 Programs also use constants. Constants can be numbers. 1, 2, 3.14. Think carefully about using constants in your code should they be constants?
String constants are common. When we said How many hours did you work? we created a string constant. When we want to set or change variables, we use the Order of Operations (PEMDAS). First comes parentheses, from the inside out. Use parentheses every time you think you re doing something confusing. GrossPay = (HourlyWage * HoursWorked) Next comes exponents. Usually this uses the character ^ or **. 2^3 would be 8 (2x2x2=8). Some languages would say 2**3. Multiplication * and Division / left to right. Multiplication uses the * character. Division uses the / character. Think carefully about using parentheses here if your math is complicated. Addition and Subtraction, left to right. Usual symbols, + and. Again, use parentheses if your math is complicated. The syntax for setting a variable s value is Variable Name = some expression and the variable s name must be alone on the left hand side. If I have a statement (p. 49) Set outcome = 12 + 6 / 3 I might want to write it Set outcome = 12 + (6 / 3) because even though the parentheses are unnecessary, putting them there makes it clear to me when I look at it that that part happens first. and maybe what I wanted was Set outcome = (12 + 6) / 6