Download presentation
Presentation is loading. Please wait.
1
CS 1400 Jan 10, 2007
2
What is it? Computer Science is the art and science of writing programs –Users use programs –Programmers create these programs A program is a logical sequence of instructions that leads to a correct solution to a problem. Programmers are story-problem solvers!
3
Successful Problem Solvers have or acquire the necessary subject matter knowledge have a positive, objective attitude, viewing the activity as a learning opportunity rather than a “test” of their intelligence are active begin with what they understand draw on other information and sources of knowledge refer to the problem statement frequently employ sequential analysis when a question is initially unclear ask themselves questions and think out loud – (we call this "self talk") brainstorm use physical aids to thinking, such as drawing pictures relate problems to familiar / concrete experiences or to previous problems carefully proceed through a series of steps to reach a conclusion
4
Less Successful Problem Solvers lack necessary subject-matter knowledge and are willing to allow gaps of knowledge to exist have a poor attitude: an attitude of indifference towards achieving a complete, accurate comprehension of situations and relations demonstrate a lack of objectivity in dealing with problems are passive in their thinking have great difficulty in ascertaining what they are required to do grope blindly toward a solution fail to complete a chain of reasoning use one-shot thinking, rather than building an extended, sequential path to understanding rush through or skip instructions take little time to consider a question or break it down into its component parts are mentally careless and superficial select an answer on the basis of a feeling or guess
5
Example Program… washing hair Wet hair Pick up shampoo bottle Open the bottle Pour some shampoo on your hand Close the shampoo bottle Set bottle down Put shampoo on hair Rub shampoo into hair until lather covers hair Rinse hair until all lather is removed Repeat the previous eight steps one more time
6
Programs vs. Algorithms A program contains an algorithm A program must be in a certain format –-- think of a business letter!
7
Algorithms in Computer Science 1.The instructions must be well ordered. 2.The algorithm must be unambiguous -- clear and incapable of being misunderstood. 3.The process must eventually end. 4.The requested action must be "doable.“ 5.The algorithm must produce a result.
8
Program Development Step 1 - Analyze the problem Input:Input: Identify what information the program will have to retrieve or will be provided to the program by the user. Values given or known values: Identify any values that appear in the problem statement. These may or may not be used in your solution. Also watch for values or formulas that are not given but that will need to be used to complete the solution. Processing: What will need to be done to produce the desired output? It is important at this stage to focus on what needs to be done, not how it will be accomplished. Output : Identify what is to be produced by this program. This will be a list of the desired results.
9
Example… “Write a program to calculate the volume of a box, using dimensions provided by the user.” Input: height, width, depth of box Given values: none Processing: calculate volume Output: volume of the box
10
Step 2 – Outline the solution List steps to be performed in correct order – not concerned with details at this point. Get height, width and depth of the box from user Calculate the volume of the box Output the dimensions entered and the volume of the box
11
Step 3 – Develop an algorithm in pseudocode Print “Enter the height of the box: “ Get height from user Print “Enter the width of the box: “ Get width from user Print “Enter the depth of the box: “ Get depth from user volume = height * width * depth Print “The volume of the box is “ Print volume
12
Step 4 – Test the algorithm Pretend you are the computer – hand tracing Step 5 – Code the algorithm into C++ Step 6 – Run and test the program on the computer Step 7 – Document the program
13
Programming Language Elements Punctuation and spelling Syntax and grammar Semantics Pseudocode allows us to start writing algorithms without worrying about these issues.
14
English-like Pseudocode Key Words – these words have special meaning Get Print And Or If Else End If Repeat End Repeat While End While
15
English-like Pseudocode VariablesVariables – one-word name representing a value or a place where a value can be stored Think of a mailbox: –The name on the mailbox represents a variable name, –The contents of the mailbox represent the variable value. A variable only holds one value at a time!
16
English-like Pseudocode OutputOutput Print “message” Print variable InputInput Get variable from user
17
English-like Pseudocode OperatorsOperators Arithmetic: *, +, -, / Assignment: = CalculationsCalculations Variable = arithmetic expression (Arithmetic expressions may only use known values and variables)
18
Example: “Write a program to calculate the volume of a box, using dimensions provided by the user.” Print “Enter height, width, and length of box:” Get height from user Get width from user Get length from user volume = height * width * length Print “Volume is: “ Print volume
19
More Examples: “Write a program to calculate a paycheck, using hours worked and a payrate provided by the user” “Write a program to convert a temperature provided by the user in degrees Fahrenheit into degrees Centigrade” “Write a program to calculate the circumference of a circle provided by the user”
20
Control Structures Sequence Selection (or decision) Repetition
21
Selection If condition Statements… Else Statements… End If
22
Conditions A condition is a comparison question whose answer is either true or false (Boolean). The comparison operators are; greater than = greater than or equal == equal!= not equal
23
Example Selections If age < 18 Print “you cannot vote” Else Print “you can vote!” End If If quantity >= 26 Print “sufficient quantity is available: ” Print quantity End If
24
Example algorithm Write a program to calculate an employee paycheck using time-and-a-half for overtime Input: hours, payrate Given values: over 40 hours is overtime, overtime rate is 1.5 of payrate Processing: calculate regular pay, overtime pay, and total pay Output: total pay
25
Outline Get hours_worked and payrate from user If there was overtime, calculate pay using payrate for first 40 hours and 1½ payrate for additional hours Otherwise, calculate pay at payrate Print pay
26
Pseudocode Print “Enter hours worked: “ Get hours from user Print “Enter pay rate: “ Get payrate from user Overtime_rate = payrate * 1.5 If hours > 40 pay = 40 * payrate + (hours-40) * over_timerate Else pay = hours * payrate End If Print pay
27
Repetition The counted loop Repeat N times Statements… End Repeat The condition-controlled loop While condition Statements… End While
28
Examples “Write a program to output the squares of each number input by the user until the user inputs a negative number” “Write a program to accept 10 test scores from the user and output the counts of passing (>=60) and failing scores” “Write a program to output the squares of the integers 1 through 5”
29
Inputs:none Given values:integers 1 through 5 Processing:calculation squares Outputs:squares of integers 1 through 5
30
Outline… Start N as 1 As long as N is less than or equal to 5, repeat; Calculate and print the square of n Add 1 to n
31
N = 1 While N <= 5 square = N * N Print “The square of “, N, “ is “, square N = N+1 End While “Write a program to output the squares of the integers 1 through 5”
32
Print “The square of 1 is 1” square2 = 2 * 2 Print “The square of 2 is “, square2 square3 = 3 * 3 Print “The square of 3 is “, square3 square4 = 4 * 4 Print “The square of 4 is “, square4 square5 = 5 * 5 Print “The square of 5 is “, square5 Why not this?
33
“Write a program to accept 10 test scores from the user and output the counts of passing (>=60) and failing scores” Inputs:a list of 10 test scores Give values:a count of 10, a passing score of 60 Processing:find a count of passing and failing test scores Outputs:count of passing/failing scores
34
Outline… Start the counts of passing and failing scores as 0 Repeat the following 10 times; prompt the user and get a score if this score is passing add 1 to the passing count otherwise add 1 to the failing count Print the counts of passing and failing scores
35
passing = 0 failing = 0 Repeat 10 times Print “Enter a test score: “ Get score from user If score >= 60 passing = passing + 1 Else failing = failing + 1 End If End Repeat Print “Passing: “, passing, “ Failing: “, failing
36
“Write a program to output the squares of each number input by the user until the user inputs a negative number” Inputs:a list of positive numbers followed by a negative number Given values:none Processing:square of each positive number Outputs:calculated squares
37
Outline… Prompt the user and get the first number Repeat as long as the number is positive; calculate the square of this number Print the square Prompt the user and get the next number
38
Print “Enter the first number: “ Get number from user While number > 0 Square = number * number Print “Square is: “, square Print “Enter the next number: “ Get number from user End While “Write a program to output the squares of each number input by the user until the user inputs a negative number”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.