Unformatted text preview:

Solving problems using the Computer as a Tool Programming Basics Topic 8 Part 1 Chapter 3 Part 3 Computer Science The computer is a tool by which Computer Scientists accomplish their goals to solve problems Control Logic Structures All modern programming languages are based on 3 basic control structures Sequence Is NOT just about coding or hardware or software Instructions are executed one after another in the order they appear in the program Until another control structure takes precedence Computer Science is about PROBLEM SOLVING Much like the microscope does not define biology or the test tube does not define chemistry the computer doesn t define Computer Science Selection Based on some condition either one part of the program is executed or another part is executed The program chooses which part to execute based on the condition Computer Science is about developing algorithms to solve complex problems Repetition Part of the code is executed over and over repeated This can be for a set number of times or until a condition is met Chapter 3 Programming Basics Control Logic Structures Algorithm Questions Tools we use to create Algorithms WHAT IS AN ALGORITHM An algorithm is a step by step definition of a process It should also be a well developed organized approach to solving a complex problem Computer Scientists ask themselves four critical questions when they evaluate algorithms Chapter 3 Programming Basics 4 1 Does the algorithm solve the stated problem 2 Is the algorithm well defined 3 Does the algorithm produce an output 4 Is it efficient 5 1 Developing an Algorithm 1 Identify the Input 2 Identify the Output 3 Identify the Processes 4 Develop a HIPO Chart 5 Develop Pseudo code or a Flowchart 6 Test our algorithm with a desk check 1 Identify the Inputs What data do I need to generate the output How will I get the data From the user 2 Identify the Output What is the format of the data 3 Identify the Processes What output do I need to return to the user How should it be displayed What do we need to do to produce the output with the given input How can I display the data to produce meaningful results Data vs Information 4 Develop a HIPO CHART A Simple Sequence Problem Creating our HIPO Chart Hierarchical and Input Processing Output Chart Shows the overall structure of the program Shows the relationship between different components modules in the system Top level is general 2nd level breaks it down into Input Processing Output Each successive level is more detailed Problem Statement Develop an algorithm that calculates the sum and average of two numbers entered by the user 1 2 3 4 Create a HIPO chart Write the Pseudo code Create a Flowchart Perform a desk check to test our algorithm First we need to ask a few questions What do we need as input What do we need to output What processing do we need to do to go from the input to the output All of these techniques help us develop our algorithm without dealing with the complexities of a programming language 11 How can we break out problem into more manageable 12 sub problems 2 HIPO CHART Top Level General Description INPUT first number second number Input Module Exercise 1 Design the algorithm for a program that calculates the total of a retail sale CALCULATE OUTPUT The sum avg of 2 numbers Processing Module INPUT INPUT first number second number CALCULATE sum avg CALC sum Output Module CALC avg OUTPUT sum avg OUTPUT sum The program should ask the user for the following the retail price of the item being purchased and the sales tax rate OUTPUT avg Lower Levels Detailed Description Top Level High level or general description 2nd Level Breaks it down into Input Processing and Output Modules Bottom Level Detailed Solution for now detail to 1 instruction 13 What is our input What are our output What do we need to calculate processing Writing Pseudo code Terse English description of a algorithm Chapter 3 Programming Basics 15 Chapter 3 Programming Basics Draw the HIPO chart for this problem Can be easily converted into any programming language Each sentence starts with a command Uses keywords such as PROMPT READ INPUT ASSIGN CALCULATE OUTPUT Example Calculate sum and average of two numbers Should Be Capitalized BEGIN PROGRAM these can be combined PROMPT the user for the first input into just INPUT READ the first input PROMPT the user for the second input It is implied that the user input will be read after a READ the second input prompt CALCULATE the sum CALCULATE the average OUTPUT the sum OUTPUT the average 17 END PROGRAM 14 HIPO CHART Some things to think about Once the information has been entered the program should calculate and display the following The sales tax for the purchase and the total sale 16 Exercise 1 Design the algorithm for a program that calculates the total of a retail sale The program should ask the user for the following the retail price of the item being purchased and the sales tax rate Once the information has been entered the program should calculate and display the following The sales tax for the purchase and the total sale Write the Pseudo code for this problem Chapter 3 Programming Basics 18 3 Pseudocode Before we move on Calculate the sales tax and total purchase price We need to think about the values that we want the computer to store as the program executes For this program We need to store into memory the two numbers we are using as input sales tax rate retail price We also will want to store the sum and the avg of these numbers those memory locations are called variables Chapter 3 Programming Basics 19 20 We can t just name them anything Required rules what the compiler cares about We don t know exactly where the data is stored But we don t need to Can have letters numbers and underscore Variables must begin with a letter They are case sensitive We reference the memory locations with names that we determine The computer keeps track of the actual address like file names A a Can t have spaces Can t have special characters Naming memory locations is called symbolic addressing Variables are essential to programming because we want to be able to use data that we store to do calculations Chapter 3 Programming Basics Chapter 3 Programming Basics Variable Naming Rules Variables Variable A place in memory we use to store data Variables store data that can change during program execution Can t be keywords Keywords Words that have special meaning in C 21 22 Chapter 3 Programming Basics Variables Naming Rules


View Full Document

Saddleback CS 1A - Programming Basics

Loading Unlocking...
Login

Join to view Programming Basics 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 Programming Basics 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?