structured programming introduction to c fundamentals

65
INF 130 STRUCTURED PROGRAMMING (3 UNITS) Instructor: Dr. H. Bii / Mr. J Sigei

Upload: omwoma-jackson

Post on 05-Dec-2014

4.901 views

Category:

Technology


6 download

DESCRIPTION

its a simplified introduction to C programming language basics

TRANSCRIPT

Page 1: structured programming Introduction to c fundamentals

INF 130

STRUCTURED PROGRAMMING

(3 UNITS)

Instructor:

Dr. H. Bii / Mr. J Sigei

Page 2: structured programming Introduction to c fundamentals

INTRODUCTION

Programming is a required skill today,

just like English and Maths.

It teaches a variety of skills that are

important in all kinds of professions: critical reading,

analytical thinking,

creative synthesis, and

attention to detail.

It provides immediate feedback, leading

to exploration, experimentation, and self-

evaluation.

Page 3: structured programming Introduction to c fundamentals

What is programming?

Process of coming up with computer

programs.

But what is a program?

A PROGRAM is a set of instructions that a

computer can follow to do something.

Programming = process of coming up

with instructions that a computer can

follow to do something.

Page 4: structured programming Introduction to c fundamentals

… What is programming?

As a process, it involves:

Understanding a problem for which a

solution is required;

Designing a solution;

Writing or coding the program in a

programming language;

Testing the code;

Correcting any errors (debugging); and

Maintaining the program.

Page 5: structured programming Introduction to c fundamentals

… and Structured programming?

Programming that:

Enforces a logical structure on the

program to make it efficient and easier to

maintain/modify.

Implements modules (procedures /

functions), each fulfilling some function.

Uses basic programming constructs -

sequence, selection and iteration (no goto

statements in „spaghetti code‟).

Page 6: structured programming Introduction to c fundamentals

Programming Language

Programming requires a programming

language:

a set of words, codes, and symbols that

allow a programmer to give instructions to

the computer.

Each language has rules (or syntax) for

writing the instructions.

Page 7: structured programming Introduction to c fundamentals

… Programming Language

There are many programming

languages – so many! (like human

languages)

They may be classified (in many

ways, including) into THREE broad

categories:

Machine Language;

Assembly Language; and

High-level Languages.

Page 8: structured programming Introduction to c fundamentals

Their Evolution

Page 9: structured programming Introduction to c fundamentals

Machine LanguageMade up of streams of 1s and 0s.

Programs written as series of 1s and

0s is said to be in machine language:

Page 10: structured programming Introduction to c fundamentals

… Machine Language

Machine language is the only language

understood by computer hardware.

Writing programs in machine language

is difficult and cumbersome –

instructions are difficult to remember.

The only language understood by computer

hardware is machine language.

Page 11: structured programming Introduction to c fundamentals

… Machine Language

Each computer has its own machine

language; rules of language differ from

one computer hardware to another.

Note: Machine language is also

referred to as First Generation

programming language.

First generation programming

Language; also low-level languages

Page 12: structured programming Introduction to c fundamentals

Symbolic / Assembly Language

Developed in 1950s to reduce

programmers‟ burden – by Grace Murray

Hopper, US naval officer.

She developed the concept of a special

computer program that would convert

programs into machine language.

Page 13: structured programming Introduction to c fundamentals

… Symbolic / Assembly Language

The program mirrored the machine

languages using symbols, or mnemonics,

to represent the various machine

instructions.

The languages developed in this manner

were known as symbolic languages.

An assembler translates symbolic code

into machine language – thus Assembly

Language.

Page 14: structured programming Introduction to c fundamentals

… Assembly language

Page 15: structured programming Introduction to c fundamentals

High-Level LanguagesSymbolic language improved programming

efficiency, but still tedious and hardware focused.

High-level languages were developed to further

improve efficiency and change focus from

computer to the problem being solved.

High-level languages:

produce portable programs;

easier to learn;

require less time to write;

easier to maintain; and

provide better documentation.

Page 16: structured programming Introduction to c fundamentals

… High-level language

Continued …

Page 17: structured programming Introduction to c fundamentals

… example

Programs written in high-level languages have

English-like statements (and familiar math symbols)

and must be converted to machine language (1s and

0s).

The process of converting them is called compilation.

Examples of high-level languages are FORTRAN,

COBOL, C, BASIC, Pascal, ALGOL, and many

others.

Page 18: structured programming Introduction to c fundamentals

Overview of some high-level languages

a) BASIC

Beginners‟ All-purpose Symbolic Instruction Code.

Developed by John Kemeny & Thomas Kurtz in

1964 – for beginners – easy to learn.

Example:5 REM Program to compute sum of 10 numbers

10 LET s = 0

20 FOR i=1 TO 10

30 READ n

40 LET s = s + n

50 NEXT i

60 PRINT "The sum of the given numbers ="; s

70 DATA 4, 20, 15, 32, 48

80 DATA 12, 3, 9, 14, 44

100 END

Page 19: structured programming Introduction to c fundamentals

b) Pascal

Introduced in 1971 by Niklaus Wirth in Zurich becoming

the first language to fully embody structured

programming concepts.

Example:PROGRAM SUMNUM(INPUT, OUTPUT);

(* Program to Compute the sum of 10

numbers *)

(* Declaration of variables *)

VAR Sum, N :REAL;

VAR i :INTEGER;

BEGIN

Sum := 0;

FOR i := 1 TO 10 DO

BEGIN

READ (N);

Sum := Sum + N;

END;

WRITELN ('THE SUM OF GIVEN

NUMBERS =', SUM);

END

Page 20: structured programming Introduction to c fundamentals

Assignment 1:

Read about and write short notes on the following

high-level languages:

FORTRAN;

COBOL;

ADA;

ALGOL; and

PL/1.

Page 21: structured programming Introduction to c fundamentals

System and Program Development

Page 22: structured programming Introduction to c fundamentals
Page 23: structured programming Introduction to c fundamentals

System Development

An old programming proverb:

Resist the temptation to code.

Note

Page 24: structured programming Introduction to c fundamentals

Program Development

Multistep process that requires that we:

Understand the problem – output

required; input available; steps to follow

in processing (algorithm).

Plan the logic of the solution (or

program). Three tools will help in this

task:

Flowcharts;

Pseudocode; and

Structure charts.

Page 25: structured programming Introduction to c fundamentals

… Program Development

Code or write program statements in a

programming language e.g. BASIC, C,

Pascal. This may be done on paper.

Key the program into the computer

(use text editor).

Compile, test and debug the program.

Complete the documentation: charts,

listings, manuals.

Page 26: structured programming Introduction to c fundamentals

... Programming Steps –

1) Understand the ProblemRead the problem or talk to & listen to the

person who posed the problem.

Identify the problem inputs, outputs, and

any additional requirements or constraints:

Inputs – data to work with;

Outputs – desired results;

Other Requirements/Constraints –

Relationships between variables, format of

results e.g. Table.

Page 27: structured programming Introduction to c fundamentals

... Understand the Problem

Example:

Compute and display the total cost of

oranges given the weight of oranges

purchased and the cost per 1000g of

oranges.

Page 28: structured programming Introduction to c fundamentals

... Understand the Problem

From the problem, we identify:

Compute and display the total cost of

oranges given the weight of oranges

purchased and the cost per 1000g of

oranges.

Inputs:

Quantity of oranges purchased (in Kgs);

Unit cost of oranges (in Kshs.).

Page 29: structured programming Introduction to c fundamentals

... Understand the Problem

From the problem, we identify:

Compute and display the total cost of

oranges given the weight of oranges

purchased and the cost per 1000g of

oranges.

Outputs:

Total cost of oranges (in Kshs).

Page 30: structured programming Introduction to c fundamentals

... Understand the Problem

Other requirements/constraints:

Total cost = Weight (Kgs) X Unit Cost

(Kshs).

The process we went thru (extracting

essential variables and their relationships)

is called ABSTRACTION.

Page 31: structured programming Introduction to c fundamentals

... Understand the Problem

Exercises:

Read two numbers from the keyboard,

sum them up, and display the result.

Eldy surveyors want a program that

converts distances in miles to kilometres

and showing the result on screen.

Page 32: structured programming Introduction to c fundamentals

2) Plan the logic of (Design) the solution

Involves developing a list of steps

(algorithm) to solve the problem.

Usually the most difficult part of problem-

solving process.

As noted above, 3 tools are important

here:

Structure charts;

Pseudocode; and

Flowcharts.

Page 33: structured programming Introduction to c fundamentals

Structure Chart

Also called hierarchy chart.

It shows the functional flow through a

program – parts (modules) of a program

and how they are related e.g.

Page 34: structured programming Introduction to c fundamentals

… structure chart

It is drawn before a program is written –

thus acting as an architectural blueprint –

more like a house plan.

In business world, once a structure chart

is completed, a structured walkthrough is

conducted – where a review panel is

taken through the structure to understand

how programmers plan to solve the

problem and attain the objectives they

set out to achieve.

Page 35: structured programming Introduction to c fundamentals

… structure chart

Question 1:

Develop a structure chart for a program

that is meant to obtain two numbers

from a user, multiply the numbers and

show the result on screen.

Page 36: structured programming Introduction to c fundamentals

Solution?

Multiply 2

numbers

Program

Print ResultCalculate

Product

Input first &

second

numbers

Page 37: structured programming Introduction to c fundamentals

Pseudocode Is an English-like statement; part of program logic.

It describes what the program will do in precise

algorithmic detail e.g.

Pseudocode

English-like statements that follow a loosely defined syntax

and are used to convey the design of an algorithm.

Page 38: structured programming Introduction to c fundamentals

Question 2:

Write a pseudocode for a program for calculating the

amount of tax (at 15% of value for property worth Kshs

100,000 and above, and 12% otherwise) of a property.

Assume that the user wants to determine taxes for more

than one property.

Question 3:

Write a pseudocode for a program for calculating

the cost of flooring the living areas (slide 33).

Reading:Do a literature search in the Library or on the Internet about pseudocodeas a design and documentation tool. Read and attempt the questions above.

Page 39: structured programming Introduction to c fundamentals

Flowchart

Program design tool that uses standard

graphical symbols to represent logical

flow of data thru a function/program.

Its primary purpose is to show the design

of an algorithm.

Page 40: structured programming Introduction to c fundamentals

… Flowchart

It frees a programmer from the syntax and

details of programming to concentrate on

details of the problem to be solved.

It gives a pictorial representation of an

algorithm – helps one to think of a

problem pictorially.

Page 41: structured programming Introduction to c fundamentals

example Startv

Stop

Read

Length

Read

Width

Print Area

Calculate

Area = Length X

Width

Page 42: structured programming Introduction to c fundamentals

example

Page 43: structured programming Introduction to c fundamentals

Flowcharting Symbols:

Two basic categories:

1) Auxiliary symbols

enhance readability or functionality of a

flowchart.

They do not directly show instructions or

commands.

a) Terminators:

START STOP

b) Flow lines:

Rule: One entry/exit!

Rule: Start/Stop has one

Exiting/entering line!

Page 44: structured programming Introduction to c fundamentals

… auxiliary symbols

c) Connectors:

n

Circle with number

in it.

Page 45: structured programming Introduction to c fundamentals
Page 46: structured programming Introduction to c fundamentals

2) Primary Symbols:

Show instructions or actions needed a problem presented in the algorithm.

They show all three structured programming constructs: sequence, decision, and repetition.

a) Sequence Symbols:

Sum ← x + y

i) Assignment

Statement:

ii) Input/Output

Statement:Read x

Page 47: structured programming Introduction to c fundamentals

Startv

Stop

Read

Length

Read

Width

Print Area

Calculate

Area = Length X

Width

Page 48: structured programming Introduction to c fundamentals

… sequence symbols:

iii) Module-call statement:

AVRG(ave, a, b, c)

Startv

Stop

Read (a)

Read (b)

Print

Average

Read (c)

AVRG(ave, a, b, c)

Flowchart for this

is elsewhere (below)

Page 49: structured programming Introduction to c fundamentals

AVRG(rslt, x, y, z)v

Return

sum ← x + y + z

rslt ← sum/3

Page 50: structured programming Introduction to c fundamentals

b) Selection statement symbols

Those for specifying conditional statements (that

allow execution of selected statements and

skipping of others).

There are 2 selection statements (decisions):

Two-way selection;

Multiway selection.

Condition Number > 10TF

T – to the right;

F – to the left;

NB: Not good practice to have any to bottom!

2-way:

Page 51: structured programming Introduction to c fundamentals

… selection Startv

Stop

Read (a)

a > 10

TF

Write

(newnum)

newnum ← a - 10

NOTE: Only one statement (or

null) in each branch is allowed & it

may be a compound statement

Page 52: structured programming Introduction to c fundamentals

… selection

Multiway selection:

Page 53: structured programming Introduction to c fundamentals

Example (multiway selection)

Page 54: structured programming Introduction to c fundamentals

Question 4:

Design an algorithm (flowchart) that reads an integer. If

the integer value is greater than 10, it subtracts 10 and

writes the original number and the result. If the number

is less than 10, it displays the message “That number is

less than 10”.

Question 5:

Design a flowchart for a program that reads a mark

between 0 and 100 and prints a corresponding letter

grade.

Page 55: structured programming Introduction to c fundamentals

c) Looping Statements symbols

Iteration/repetition (for, while, do..while)

a) for

- Counting loop.

- pre-test, so body may

never be executed.

- good when number of

times to loop is known.

Page 56: structured programming Introduction to c fundamentals

… for loop actions

Question 6:

Design a flowchart (or algorithm) for a program that

reads 20 numbers and print their sum.

Page 57: structured programming Introduction to c fundamentals

? answer…

Page 58: structured programming Introduction to c fundamentals

b) while

• Not a counting loop;

•Also pre-test loop – body

may not be executed.

•Good when number of

times to loop is not known.

Question 7:

Design a flowchart for a program that reads numbers from

the keyboard and prints their total. We don‟t know how

many numbers the user will input. However, all numbers

are positive numbers.

Page 59: structured programming Introduction to c fundamentals

NOTE: first number is

read before the loop (i.e.

priming the loop)

Page 60: structured programming Introduction to c fundamentals

c) do...while

• this is a post-test loop i.e. the

condition is tested at the end of

the loop.

•Body of the loop is executed at

least once

Question 8:

Design a flowchart for a program that reads and

processes numbers between 1 and 5. How it processes

the numbers is not important at this stage.

Page 61: structured programming Introduction to c fundamentals

(num > 1)

&& (num < 5)

Page 62: structured programming Introduction to c fundamentals

So far in Program Development …

Multistep process that requires that we:

Understand the problem – output required; input

available; steps to follow in processing (algorithm).

Plan the logic of the solution (or program). Three tools

will help in this task:

Flowcharts;

Pseudocode; and

Structure charts.

Code or write program statements in a programming

language e.g. BASIC, C, Pascal. This may be done on

paper.

Key the program into the computer (use text editor).

Test and debug the program.

Complete the documentation: charts, listings, manuals.

Page 63: structured programming Introduction to c fundamentals

Exercises

Write a pseudocode and develop a flowchart for each of

the following problems:

1) Calculate the circumference and area of a circle (PI =

3.142).

2) Compute and display the letter grade of a student‟s

mark in a certain course.

3) Compute and print the net pay of an employee given

the number of hours worked and the hourly rate of

pay.

4) Convert the temperature given in degrees fahrenheit

to degrees celcius given that:

Degrees Celcius = (Degrees Fahrenheit – 32) * 5/9

Page 64: structured programming Introduction to c fundamentals

Coding

Writing instructions in a programming language –

high-level language.

So we need to learn the language, its structure and

syntax.

Choice: C

Page 65: structured programming Introduction to c fundamentals