You are on page 1of 24

Lecture 1

Programming
Fundamental

Software

The intermediary between you (the user)


and the hardware

Operating system (OS) see the next page

Windows, OS X, Linux

Application programs

End-user applications
Word processor, etc.
Matlab, etc.

Application development software


(programming languages)
C, Matlab (sort of), Python, Java, FORTRAN, etc.

Operating System (OS)

A program that:

Acts as an intermediary between


hardware and application
software
Provides a consistent, stable way
for applications to interact with
hardware

APIs, so you dont have to do it all


yourself

Examples:

Windows XP/Vista
Linux

http://en.wikipedia.org/wiki/File:Operating_system_placement.svg

Create/Edit
source files

Program Development

(your program!)

Compile
source files

Link
compiled files
Load
executable file

Run
your program!
Test
Repeat process

Correct
Not correct

Method for Developing a Program


Note: these steps are to be done BEFORE you write any program code!

1. Define the problem:

State the problem you are trying to solve in clear and


concise terms.

2. List the inputs and the outputs

Inputs: information needed to solve the problem


Outputs: what the algorithm will produce as a result

3. Describe the steps needed to convert or manipulate the


inputs to produce the outputs (develop the algorithm)

Begin at a high-level first


Refine (subdivide the high-level) steps until they are
effectively computable operations.

4. Test the algorithm:

choose data sets, and verify that your algorithm works!

Structured Programming
Sequence
Selection

IF
IF ELSE
SWITCH

Repetition

WHILE
DO WHILE
FOR
Flowchart constructs

Algorithm

What is an algorithm?

A recipe
A procedure
Definition:

well-ordered collection of unambiguous and


effectively computable operations, that when
executed, produces a result and halts in a finite
amount of time.

Characteristics of an Algorithm

Well-ordered:

Unambiguous:

the steps are in a clear order


the operations described are understood by a
computing agent without further simplification

Effectively computable:

the computing agent can actually carry out


the operation

Pseudocode

natural language-like statements that precisely


describe the steps of an algorithm
Statements that describe actions
Focuses on the logic of the algorithm
Avoids language-specific elements
Written at a level so that code can be generated
almost automatically from each statement
Steps are numbered

Subordinate numbers and/or indentation are used for


dependent statements in selection and repetition
structures

Flowcharts - 1

Flowcharts

A graphical tool that diagrammatically depicts


the steps and structure of an algorithm or
program
Symbol

Name/Meaning

Symbol

Meaning

Process Any type of internal


operation: data transformation,
data movement, logic operation,
etc.

Connector connects sections


of the flowchart, so that the
diagram can maintain a smooth,
linear flow

Input/Output input or output


of data

Terminal indicates start or


end of the program or algorithm

Decision evaluates a condition


or statement and branches
depending on whether the
evaluation is true or false

Flow lines arrows that


indicate the direction of the
progression of the program

Flowchart Constructs - Sequence and Selection

Control Structures

from Deitel & Deitel, 6th ed., p. 122

Flowchart Constructs - Repetition

from Deitel & Deitel, 6th ed., p. 122

Control Structures

Example:

Obtain a series of positive numbers from the keyboard, and


determine and display their sum. Assume that the user types the
sentinel value -1 to indicate "end of data entry"

Define the problem

Statement pretty well defines the problem

List inputs and outputs

inputs: number entered from keyboard


outputs: sum of number

Example:

Obtain a series of positive numbers from the keyboard, and


determine and display their sum. Assume that the user types the
sentinel value -1 to indicate "end of data entry"

Develop the algorithm

High-level first, then refine:


1. Start
2. Declare variables: ________
3. Repeat while number not equal to -1
3.1. get number
3.2. add to sum
4. Display sum
Does this work?

Example:

Develop the algorithm, cont.


Refine

1. Start
2. Declare variables: ________
3. Repeat while number not equal to -1
3.1. get number
3.2. add to sum
4. Display sum

1. Start
2. Declare variables: num, sum
3. while num not equal to -1, continue doing:
3.1. Display prompt Enter positive number
3.2. Read number from the keyboard
3.3. Display number entered
3.4. add to sum
4. Display sum
Are we there yet?

Example:

Develop the algorithm, cont.

Add a test to exclude negative numbers

1. Start
2. Declare variables: num, sum
3. while num not equal to -1, continue doing:
3.1. Display prompt Enter positive number
3.2. Read number from the keyboard
3.3. Display number entered
3.4. if num less than zero, then
3.4.1 continue
3.5. add to sum
4. Display sum
Are we there now?

Flowchart

Start

Declare variables: num, sum


Intialize variables: num = 0, sum = 0

1. Start
2. Declare variables: num, sum
3. while num not equal to -1, continue doing:
3.1. Display prompt Enter positive number
3.2. Read number from the keyboard
3.3. Display number entered
3.4. if num less than zero, then
3.4.1 continue
3.5. add to sum
4. Display sum

if num ! = -1

No
Display sum

Yes

Display "Enter
positive integer"

Read num from


keyboard

Display num

Stop

Test the algorithm!

if num < 0

No
sum = sum + num

Yes

Structure of a C Program

A formal letter has a


structure

So does a program in C

Burford Furman
Title
Professor
Dept. of Mech. and Aero. Eng
San Jos State University
San Jose, CA 95192-0087

block

July 20, 2009

Date

Dear Prof. Furman,

Salutation

Im writing you to see if I can get into ME 30

Body

Sincerely,

Closing

Jane Student

Signature

C Code
Programmers block
Pre-processor directive
Main function (statements go between { } )

Declare and initialize


variables

While loop
(repetition structure)

return statement

Programmers Block

Include important information (comments)


to document the program:

Title
Date
Author
Description
Inputs/Outputs
Algorithm
Revision history

Add comments using one of two methods:


1. /* put comment between */ (note: traditional C)
2. // comment (note: single line only)

# include (pre-processor directive)

Includes a library file for standard io

functions for things like printing, etc.

main() function

Your program needs a


main() function

Statements go between
the braces { }
main() ends with the
return keyword and
usually the value zero

If main() runs
successfully, it returns
a value of zero

Declare and initialize variables

Variables must be declared before you can use them

while() Repetition Structure

while (condition)
repeat statements
between { }

Full program

You might also like