Programming Notes111updated

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 56

Welcome to

Programming

DEFINITIONS
Program Language

set of words, symbols, and codes that enable


a programmer to communicate a solution
algorithm to the computer.

DEFINITIONS
Program

set of instructions which are used for


solving a problem

DEFINITIONS
Program Language
a standardized communication technique for
expressing instructions to a computer. Like
human languages, each language has its own
syntax and grammar.
enable a programmer specify precisely what
data a computer will act upon, how these data
will be stored/transmitted, and precisely what
actions to take under various circumstances.

CATEGORIES OF PROGRAMMING
LANGUAGES

High Level
Low Level

READY!!

Why low level languages are difficult to write?


Writing a program in low level language would
be difficult for us because it would be
cumbersome and time consuming. Imagine just
using 00000000 and 111111111 only to write an
essay or a project in class how would you cope?
Did you know that early programmers had to
write programs in low level language?
Different combination of zeros and ones mean
different things. Example 11000 might
represent the character A

EXAMPLES OF LOW LEVEL


LANGUAGE

Machine language or machine code


Assembly language

EXAMPLES OF HIGH LEVEL


LANGUAGE

COBOL, Pascal,
Fortran, C++, BASIC,
There are many more
Which one will we be
using in class?
Who is Pascal? Do a
research on him now?
Blaise Pascal, French
Mathematician, He
invented the first
adding machine called
Pascaline.

WHAT IS MACHINE LANGUAGE?

Machine language is a low level language or machine


code; which is Binary language written in 0s and 1s.
This is the language of the computer. It can only
understand binary. Programs written in machine code
will be executed quickly .

WHAT IS ASSEMBLY LANGUAGE?


This is another Low level language but it uses short codes
to represent instructions such as ADD, SUB, MULT. Although
this is a low level language it is not machine code and it
has to be converted into machine code by an assembler so
that the computer can carry out instructions.

CATEGORIES OF PROGRAMMING
LANGUAGES?

Low Level
High Level

Low Level
Machine dependent
Difficult for programmers to write
Execute machine instructions quickly

HIGH LEVEL

Machine Independent (not machine specific)


Developed with the programmer in mind
Easier for the programmer to use
Program instructions take a longer time to be
executed.
Programs written in high level language code
must be converted to machine code using
translator programs such as a compiler or an
interpreter.

GENERATION OF PROGRAMMING
LANGUAGES
First
Second
Third
Fourth
Fifth

http://passcisa.blogspot.com/2008/09/fivegenerations-of-programming.html
http://www.computerhope.com/jargon/num/1gl.
htm
http://learningarea5.blogspot.com

http://passcisa.blogspot.com/2008/
09/five-generations-of-programming
.html
,
http://www.computerhope.com/jar
gon/num/1gl.htm
,
http://learningarea5.blogspot.com

FIRST GENERATION
Low level
Machine language/machine code/
Binary

SECOND GENERATION
Low level
Assembly language

THIRD GENERATION
High level
Use of English like statements
Examples:
Pascal
Cobol (Common Business Oriented Language)
BASIC (Beginners All Purpose Symbolic
Instruction Code)
C
Fortran(Formula Translation)

FOURTH GENERATION

High level
Similar to third generation and even easier to
use
user friendly offer extra help through
prompts, wizards and instructions
examples
Structured Query Language(SQL)
Visual Basic
C++
Oracle report

FIFTH GENERATION

Built on third and fourth generation


Non procedural languages
Programmer only writes the goals to be
achieved, constraints/parameters. The
computer then solves the problem
Example : Prolog

GRADED WORK- TO BE DONE ON FOLDER LEAVES

Identify two advantages and two disadvantages


of each generation of programming languages.
Do this in a table format.
Identify which generation of language for each
of the following statement:
A. A low level language that is also called
assembly language
B. High level language that uses prompts and
assistants to make programming easy
C. High level language used in artificial
intelligence that works out problem solving
steps for you

D. A low level language also called


machine language that uses binary
E. High level language that uses
English-like statements

TRANSLATION PROGRAMS

Compiler
Interpreter
Assembler

TRANSLATION PROGRAMS

Compiler is a program that


translates all lines of high
level language code into
machine code in one go to
create a stand-alone
program

TRANSLATION PROGRAMS

Interpreter is a program
that translated each line
of high level language
code into machine code
one line at a time

TRANSLATION PROGRAMS

Assembler is a program
that translates assembly
language code into
machine code

SOURCE CODE VS OBJECT CODE


High Level
Language

Source
Code

Compiler
Interpreter

Translator

Object
Code
Machine
Language

PROGRAM DESIGN TECHNIQUES

Flow chart
Used to illustrate data, information
and workflow by interconnection of
specialized symbol with flow lines.
The combination of symbols and
flow lines portray the logic of the
program. Each symbol indicates the
type of operation to be performed and
the flowchart graphically illustrates the
sequence in which the operations are to
be performed

PROGRAM DESIGN TECHNIQUES

Pseudocode
It represents the logic in program-like
statement written in a language you can
express the logical flow of the program.

Is a logic development tool that uses


English-like statements or clauses to
present the logical steps necessary to
solve a problem. It helps the programmer
to focus on the logic of the program. It is
not a language but a thought process.

PSEUDOCODE
Advantages:
It is easy to use and create
Very little specific syntax is required
Helps to focus on the logic of the program
which reduces the time spent in coding,
testing and debugging systems

ALGORITHM

Step by step procedure to solve a problem

Pseudocode contains one or more of the


following statements:
Input
Output
Assignment
Control (sequence, selection,
Iteration/repetition/loop)

INPUT STATEMENT

Used to get data from outside the computer


via some input device into a variable for
manipulation by the pseudocode.
Variable is a named quantity in a pseudocode
whose value can be changed. The value of a
variable can be changed by an input or
assignment statement

TYPES OF VARIABLES
Integer
Real
String
Constant is a value in a pseudocode that does
not change
Identifier is the name invented by a
programmer for a data item. An identifier
can be a variable or a constant.
Write a pseudocode to read input into two
variables A and B.

ASSIGNMENT STATEMENT

Used to assign a value or change the content


of the variable.
Two parts
Lvalue- refers to the variable as the
storage location where the Rvalue will be
stored
Rvalue- refers to a value which may be
the result of an expression or content of
another variable

ASSIGNMENT STATEMENT

Write an assignment statement to store


the value 100.

Write a pseudocode to read 2 numbers


and find their sum, average and product.

Calculate the salary of a days worker,


input to the pseudocode are hours worked for
the day and the hourly rate.

THE OUTPUT STATEMENT

Used to get information to the programmer or computer


user.
Forms: sound, text, voice, graphics depending on the
creativity of the programmer. For our purpose output will
be in the form of text or value. For output we use Print,
Display, Write

Rules
information between double quotation will be
printed as is
when a variable is used in a print statement the
content of the variable is printed
Variables are not included between quotes but
separated from the quoted information by comma.

THE OUTPUT STATEMENT


I.
II.

III.

Write an instruction to print Today


is Monday
Write a pseudocode to enter two
values and find their sum. Print the
result.
Write a structured algorithm which
requests the user to input the price
of an item. The algorithm should
also allow the user to enter the price
and output it with a suitable label.

PRACTICE QUESTIONS
1. Prompt

the user to input the name and price of an item and


the quantity purchased. The algorithm should calculate the
amount due. The output should be the name, price,
quantity and amount due with appropriate labels.

2.

Ask the user to input the radius of a circle. Calculate


diameter and print it with a suitable label.

3. prompt

the

the user to enter the prices for two different pay per
view movies. Calculate the amount due and output it with
the following message: Amount due BCC Television Ltd
4. Prompt the user to input the number of days in a month.
Calculate and print the number of hours in the month
appropriately labelled.

Write a structured algorithm to input the


name of a video tape, the date it was
borrowed, the rental fee charged and the
amount of money paid in. Compute the
amount of money due back to the borrower
and output it with a suitable label.

Write a pseudocode algorithm to ask the user


to input the name of a student, the marks
he/she received in a test and the worth of
the test. Calculate the percentage mark the
student received. Print the information with
suitable labels.

ASSIGNMENT

Write a structured algorithm that displays a


message asking the user to enter the name of a
book and its author. It should also allow the user
to input the required data and output the
information appropriately labelled.
Write a structured algorithm to prompt the user to
enter the length of a square. Calculate the area of
the square and output it with a suitable label.
Write a structured algorithm to request the user to
input two numbers which are stored in variables A
and B. The algorithm should subtract B from A and
output the answer with the message Answer is:

CONTROL STATEMENTS
Used to conceptualize the program logic
Types
Sequence the processing steps are
performed in order one after another
Selection depicts the logic for selecting the
appropriate sequence of statements
Iteration/repetition/loop used to represent
the program logic when a portion of the
program is executed repeatedly until a
particular condition is met

CONTROL STATEMENTS

Sequence: 1st Instruction


2nd instruction
3rd instruction

Selection:
If condition Then If condition Then
statement (action)
statement (action)
End If
Else
statement (action)
End If

QUESTIONS

Write a pseudocode to read two values and


subtract the smaller from the larger number
Ask the user to enter two test grades and print
the larger of the two test grades.
Print the value of N1 if it is greater than N2
Write a pseudocode to prompt the user to enter
the time. If the time is 11, Output Ring the bell
Read a number N, if N is greater than 100, add
10 to the number. Print the number.
Input the age of a person. If the age is greater
than 35, output old person otherwise output
young person

PRACTICE QUESTIONS
1.

2.

3.

A student is given a discount of 10% off school


fees. If the fees are paid before 30 days, a
discount is given. Read fee and number of
days. Output the fee, discount amount and fee
less the discount amount.
Prompt the user to input the mark a student
received in a test. If the mark is less than 60
output the word fail otherwise output Pass.
Prompt the user to enter a number. If the
number is greater than 45 the pseudocode
should prompt the user for 3 additional
numbers and find their sum. Otherwise the
pseudocode should terminate.

4.

5.

6.

Instruct the user to input the price and


quantity of an item. Calculate the discount
amount at 10% of the total if the total is
greater than or equal to $1000. otherwise
calculate the discount amount at 5% of the
total output
Ask the user to enter the time. If the time
is 6:30 pm then the output should be
please go home
Read N. If N is greater than 50 add 5 to the
number. Print the number

7.

8.

9.

Ask the user to enter 3 integers. Calculate


their sum and average. Print the results
with suitable labels.
Write a structured algorithm that requests
the user to input a positive number which is
stored in a variable N. If N is less than
2,000, it should subtract 20 from N. Print
the result.
Write a structured algorithm that prompts
the user to enter the pass mark and the
mark a student received in a test. Output
the word fail or pass accordingly.

10. Prompt the user to input two unequal


values that are stored in variables x and y.
Your pseudocode should print the lower
value.
11. Write a structured algorithm that instructs
the user to input the price and quantity of
an item. Calculate the discount amount
at 10% of the total if the total is greater
than or equal to $1000.00. Otherwise
calculate the discount amount at 5% of
the total output.

Write a structured algorithm to read the


amount of rainfall for each day of the month
of November. Print the total rainfall for the
month.
Write a structured algorithm to read the
amount of rainfall for each month of a year.
Print the total rainfall for the year and the
monthly average.

WORKED EXAMPLE
Design a structured algorithm that
ask the user to input two numbers.
The algorithm should accept the
numbers and calculate their sum and
average. Output the results with
suitable labels.

PSEUDOCODE
Start
PRINT Enter your first number
READ num_1
PRINT Enter your second number
READ num_2

PSEUDOCODE
Sum
Avg

num_1

CONTD

+ num_2

Sum / 2

PRINT The Sum = , Sum


PRINT The Average = , Avg
Stop

PASCAL CODE
Program Calculate;

Uses
wincrt;

Var

Num1, Num2, Sum: Integer;


Avg: Real;

PASCAL CODE

CONTD

Begin

Writeln ('Enter a number');

Read (Num1);

Write ('Enter another number');


Readln (Num2);

PASCAL CODE

CONTD

Sum := Num1 + Num2;


Avg:= Sum div 2;

Writeln ('The sum= ', Sum);

Writeln ('The average= ', Avg:3:2);

End.

Write a structured algorithm that


instructs the user to input the price and
quantity of an item. Calculate the
discount amount at 10% of the total if
the total is greater than or equal to
$1000.00. Otherwise calculate the
discount amount at 5% of the total
output. Output the price, quantity,
total, discount amount and total less
discount.

PSEUDOCODE
Start
Print Enter school fee
Read Fee
Print Enter Numbers of days
Read days
If days < 30 Then
Discount Fee * 10%
End if
NewFeeFee Discount

Print The school fee paid =, Fee


Print The number of days in which fees are
paid = , days
Print Fee Less discount =, NewFee
Stop

Program Schoolfee;
Uses
wincrt;
Var
days: Integer;
fee,discount,newfee:Real;
Begin
Writeln ('Enter school fee paid ');
Read (fee);
Writeln ('Enter number of days ');
Readln (days);

If days < 30 Then


Discount := fee * 0.1;
newfee:= fee-discount;
Writeln ('Fee = ', Fee:1:2);
Writeln (' ');
Writeln ('Number of days =', days);
Writeln ('

');

Writeln (' School fee less discount = ', newfee:3:2);


End.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy