oct07 - Computer Science

Download Report

Transcript oct07 - Computer Science

CS 101 – Oct. 7
• Solving simple problems: create algorithm
• Structure of solution
– Sequence of steps (1,2,3….)
– Sometimes we need to make a choice
– Sometimes we need to repeat some steps
• Examples 

• Computer science is a problem-solving discipline.
• Every solution should have a well-defined structure,
such as listing the ingredients and steps for input,
calculations and output.
Example problems
• How would you solve these problems?
– Print the numbers from 1 to 10.
– Searching for something.
• In this list (3, 2, 7, 5, 4) where is the number 5?
• Which room contains my umbrella?
– Play Tic-Tac-Toe.
• Idea for solution (algorithm) is more
important than typing code at the keyboard.
Computer program
• Sequence of instructions that machine
carries out
• Nouns and verbs
• Structure:
– Input, calculations, output
– Auxiliary functions
Kinds of statements
•
•
•
•
•
Get input
Print output
Assign value to variable
If-else
Loop
Algorithm
• Clear sequence of steps to arrive at a solution to a
problem. Must specify:
– Input, output, variables and operations used
– The order in which the steps are taken
• Ideally, each step should perform one calculation:
–
–
–
–
Input or output of one value
One calculation, or decision to make
Calculations usually limited to basic math
Tedious details can be put off until later.
Examples
• Algorithm to add two numbers
–
–
–
–
Ask the user to enter 2 values
Obtain the input, and call the values a and b.
Set a new variable sum and set it to: sum = a + b.
Output sum.
• Calculate weekly wage. √
– Get hours and rate from the user.
– Set the wage as follows:
• If (hours > 40), use overtime formula
• Otherwise, use regular formula
– Output wage