You are on page 1of 69

Introduction to Computer Programming

Review on Computer Terminologies




Computer
   

something that computes takes input processes input (according to particular steps) produces output a sequence of instruction executed A set of detailed, step-by-step instruction that directs the computer what to do also called as software

 

Program


Computer Program


Terminologies Cont..


A computer program may also be called:  Project  Application  Solution

Review on Computer Terminologies




Programming Language


A set of rules that provides a way of telling the computer what operations to perform is a set of syntactic and semantic rules used to define computer programs. Creates computer program Requirement: analysis, logic, knowledge w/ programming language to use

Computer Programmer
 

Program Development Cycle




Software refers to a collection of instructions for the computer The computer only knows how to do what the programmer tells it to do Therefore, the programmer has to know how to solve problems

Performing a Task on the Computer


  

Determine Output Identify Input Determine process necessary to turn given Input into desired Output

Steps in Program Development


1. 2. 3. 4. 5. 6. 7.

Identification and analysis of the problem Planning the solution Setting up an algorithm program Coding the program Program Compilation Running , testing and debugging Documentation

Programming Process

Compile Errors?

Run-Time Errors?

Create/Edit Program

Source Program

Compile Program

Object Program

Execute Program

Identification and analysis of the problem




Determine the ff:  Input -> Data  Output -> Information Purpose:  to analyze thoroughly  to understand the problem  to identify the inputs and outputs

1. Defining the Problem


Review program specs Meet with analysts / users Identify program components (IPO Input, Processing, Output))

2. Planning the Solution (Design)

 

Design the algorithm An algorithm is a finite series of logical steps required to solve a particular problem. Items covered in a next few slides  Use flowcharts and pseudocode.  Use the appropriate programming flow control structures in the design  Desk check the solution

Example: Identification and analysis of the problem




Problem 1:  Write a program to will compute and display the sum of five (5) number Inputs:  7,8,3,2,1 Process  7+8+3+2+1 Output  SUM

Example: IPO Table


Input Processing Output SUM

7,8,3,2,1 SUM = 7 + 8 + 3 + 2 + 1

Setting up an algorithm program




Design an Algorithms
 

A detailed, step by step solution to a program the a straight forward sequence of logical / mathematical instructions that must terminate (return 0) and describes data to be processed to produce a desired options. Flowcharting Pseudocode


Tools
 

false code

involves actually writing a program, written in English rather than in a computer language.

See slides of analysis of algo

Algorithms in The Real World




We use algorithms almost daily in our lives without realizing it For example
an instruction manual a cooking recipe a knitting pattern

Flowcharting


A flowchart is a pictorial representation of an algorithm or logical steps. Each step is represented by a symbol and the arrows indicate the flow and order of the steps. The shape of the symbol indicates the type of operation that is to occur. Flowcharts may help the more visual students learn and understand logic.

Flowchart Symbols

Begin or End

Input or Output

Processing

Decision

Branch or Direction of Flow

Flow Control Structures




Flow control is the order in which statements are executed. There are four control structures. 1. Sequence Control 2. Selection Control


Also referred to as branching (if and if-else)

3. 4.

Case Control (similar to selection) Repetition Control (loops)

Flowchart Sequence Control


Begin Input price, qty

subtotal = price * qty;

Output subtotal End

Flowchart Selection Control


Calculate Handling based on total weight of the order

Input Weight

True
If weight < 10

Handling = 1.00

False Handling = 3.00

Output Handling

Flowchart Case Control

Flowchart Repetition Control (loops)

Example: Flowcharting
START

SUM = 7 + 8 + 3 +2 +1

END

Example: Psuedocode
Begin procedure Input 7,8,3,2,1 Sum = 7+8+3+2+1 Output SUM End procedure

Coding the program




Translating algorithms into a formal programming language IDE / Programming Environment


 

Integrated Development Environment A comprehensive package that includes text editors, translation and debugging software

Setting up an algorithm program




Design an Algorithms
 

A detailed, step by step solution to a program the a straight forward sequence of logical / mathematical instructions that must terminate (return 0) and describes data to be processed to produce a desired options. Flowcharting Psuedocode


Tools
 

false code

involves actually writing a program, written in English rather than in a computer language.

Example: IPO Table


Input Processing 7,8,3 SUM = 7 + 8 + ,2,1 3 + 2 + 1 Output SUM
/* * A C program example */ #include <stdio.h> main() { SUM = 7 + 8 + 3 + 2 + 1; printf(The sum is %d, SUM); }

Program Compilation


Checks that there is no violation of programming language  Compiler




reads entire source code the converts it to object code, which then it is now executed by the computer

Interpreter


reads source code one line at a time, and perform the specific instruction contained in one line

Compilers & Interpreters

A compiler translates all of the source code into object code, an executable program. During this translation process, a program listing of any syntax errors is produced.

An interpreter translates one line of the source code at a time. Interpreters run programs more slowly than compilers, but they are helpful programs for finding syntax errors, because they are interactive.

Running Testing and Debugging


 

Check and corrects errors Types of errors  Syntactical / Logical Errors  Compile-time / Run-time Errors

Documentation


Syntactical / Logical Errors




Syntactical errors
 

are due failure from ff the syntax Syntax




grammatical rules of the language defining its legal constructs

Logical Errors


formulation of algorithms, wrong analysis / approach

Compile / Run-time Errors




Compile-time
  

halt compilations Compilation




method in translating program codes into machine codes

Note: syntax error encountered during compilation is a compile-time error appears during execution or the program

Run-time error


Documentation
 

includes the pictorial and written descriptions of the software. It contains internal descriptions of programming commands and external descriptions and instructions.


Problem definition, algorithms, error-free source code, testing procedures

Documentation is necessary since, sooner or later, the program will need to be maintained (correct bugs, add new features, handle problems not thought of previously, etc. This is very difficult without documentation Examples  books, manuals, and pamphlets

ProblemProblem-solving approach like algebra class




 

How fast is a car traveling if it goes 50 miles in 2 hours? Output: a number giving the rate of speed in miles per hour Input: the distance and time the car has traveled Process: rate = distance/time

Pictorial representation of the problem solving process

Program Planning
    

A recipe is a good example of a plan Ingredients and amounts are determined by what you want to bake Ingredients are input The way you combine them is the processing What is baked is the output

Program Planning Tips




Always have a plan before trying to write a program The more complicated the problem, the more complex the plan must be Planning and testing before coding saves time coding

Program development cycle cycle


1. Analyze: Define the problem. 2. Design: Plan the solution to the problem. 3. Choose the interface: Select the objects (text boxes, buttons, etc.).

Program development cycle continued


4. Code: Translate the algorithm into a programming language. 5. Test and debug: Locate and remove any errors in the program. 6. Complete the documentation: Organize all the material that describes the program.

2.2 Programming Tools



  

Three tools used to convert algorithms into computer programs:


Flowcharts - Graphically depict the logical steps to carry out a task and show how the steps relate to each other. Pseudocode - Uses English-like phrases with some VB.NET terms to outline the program. Hierarchy charts - Show how the different parts of a program relate to each other.

Algorithms


A step by step series of instructions for solving a problem (a recipe is an example of an algorithm)

Problem solving example




How many stamps do you use when mailing a letter? One rule of thumb is to use one stamp for every five sheets of paper or fraction thereof.

Algorithm
1. Request the number of sheets of paper; call it Sheets. (input) 2. Divide Sheets by 5. (processing) 3. Round the quotient up to the next highest whole number; call it Stamps. (processing) 4. Reply with the number Stamps. (output)

Flowcharts


Graphically depict the logical steps to carry out a task and show how the steps relate to each other.

Flowchart symbols

Flowchart symbols continued

Flowchart example

Pseudocode


Uses English-like phrases with some VB terms to outline the task.

Pseudocode example
Determine the proper number of stamps for a letter Read Sheets (input) Set the number of stamps to Sheets / 5 (processing) Round the number of stamps up to the next whole number (processing) Display the number of stamps (output)

Hierarchy charts
Show how the different parts of a program relate to each other Hierarchy charts may also be called  structure charts  HIPO (Hierarchy plus Input-Process-Output) charts  top-down charts  VTOC (Visual Table of Contents) charts


Hierarchy charts example

Each entry represents a module

Divide-and-conquer method Divide-and

Used in problem solving take a large problem and break it into smaller problems solving the small ones first Breaks a problem down into modules

Statement structure


Sequence follow instructions from one line to the next without skipping over any lines Decision - if the answer to a question is Yes then one group of instructions is executed. If the answer is No, then another is executed Looping a series of instructions are executed over and over

Sequence flow chart

Decision flow chart

Looping flow chart

Direction of Numbered NYC Streets Algorithm




Problem: Given a street number of a one-way street in New York, decide the direction of the street, either eastbound or westbound Discussion: in New York even numbered streets are Eastbound, odd numbered streets are Westbound

Flowchart

Pseudocode
Program: Determine the direction of a numbered NYC street Get street If street is even Then Display Eastbound Else Display Westbound End If

Hierarchy Chart

Class Average Algorithm


Problem: Calculate and report the grade-point average for a class  Discussion: The average grade equals the sum of all grades divided by the number of students Output: Average grade Input: Student grades Processing: Find the sum of the grades; count the number of students; calculate average


Flowchart

Pseudocode
Program: Determine the average grade of a class Initialize Counter and Sum to 0 Do While there are more data Get the next Grade Add the Grade to the Sum Increment the Counter Loop Computer Average = Sum/Counter Display Average

Hierarchy Chart

Pseudocode & Algorithm




Example 1: Write an algorithm to determine a students final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

Pseudocode & Algorithm


Pseudocode:  Input a set of 4 marks  Calculate their average by summing and dividing by 4  if average is below 50 Print FAIL else Print PASS

The Flowchart
A Flowchart  shows logic of an algorithm  emphasizes individual steps and their interconnections  e.g. control flow from one action to the next

Flowchart Symbols
Basic

Example 2
Write an algorithm and draw a flowchart to convert the length in feet to centimeter. Pseudocode:  Input the length in feet (Lft)  Calculate the length in cm (Lcm) by multiplying LFT with 30  Print length in cm (LCM)


Example 2
Algorithm  Step 1: Input Lft  Step 2: Lcm n Lft x 30  Step 3: Print Lcm Flowchart
START

Input Lft

Lcm n Lft x 30

Print Lcm

STOP