You are on page 1of 17

COMPUTER SCIENCE

HPC INTRODUCTION TO COMPUTER PROGRAMMING

1. Definition of Key Terms


1.1 Program
A program is a set of instructions which tell the computer to carry out a particular
task and how to carry it out. Examples: Firefox, Microsoft word, Zoom, games
1.2 Software
Software is a collective term referring to one or more programs
1.3 Programming Language
A programming language is software that is used by programmers to write computer
programs. Examples are: Visual Basic, Python, Java, C, C++
1.4 Computer Programmer
A computer programmer is a person who writes computer programs. In order for a
programmer to write a program they must first learn a particular programming
language and its syntax.
1.5 Syntax
Syntax refers to the rules of a particular programming language. Each programming
language has its own rules that are specific to it. For example to output in Visual
Basic we use the key words: Console.writeline whereas to output in C++ we use
the keyword Cout<<
1.6 Algorithm
An algorithm is a set of instructions written in their order for carrying out a particular
task. An algorithm is usually shown using pseudocode or a flowchart

1.7 Pseudocode
Pseudocode refers to English like statements which are used to outline an algorithm.
Pseudocode does not have syntax but it must be written clearly and concisely so
that it is easy to read and understand
2. Programming Concepts
2.1 Storage of values
When a programmer wants to write a program, he must first decide how he is
going to store values. The 3 ways of storage of values for a program are:
VARIABLE, CONSTANT and ARRAY
2.1.1 Variable
A variable is a storage space in computer memory whose value can change as the
program is running. For example Count=Count+1
A variable has a variable name and a datatype. A variable name is also called an
IDENTIFIER.
2.1.2 Constant
A constant is a storage space in computer memory for values whose value does not
change as the program is running. Example Constant PI=3.142
2.1.3 Array
An array is a data structure for storage of values which has many spaces under one
name or identifier and datatype. Values stored in an array must be of the same
datatype
2.1.4 Datatype
A datatype specifies the type of data that can be stored in a variable, constant or an
array. Examples are:
Integer – for whole numbers such as 20, 15 or 50
Double or Real or Decimal – for storing numbers with decimal points such as 3.5,
0.23, 25.89
Boolean – true or false
Char – A single character such as a “b” or “z”
String - a string of many characters such as “uMalcom Sambo” or “ibizo lami ngu
Ryan Winterboer”

2.2 INPUT, PROCESSING AND OUTPUT


Every program or algorithm has Input, Processing and Output
2.2.1 INPUT
Every program accepts input from the user e.g for a program which adds 2 numbers
the inputs are: Num1 and Num2. In Pseudocode the key word INPUT or READ are
used to input data into the program.
Example: For a program which prompts the user to input their first name and
displays it, the input is the firstname
2.2.2 PROCESSING
Every program processes the input in order to produce an output. Processing is the
calculation that happens, For example a program which adds 2 numbers, the
processing is:
Sum=Num1 + Num2

2.2.3 OUTPUT
After processing, the program gives an output. For example for a program that adds
2 numbers the Output is Sum. In Pseudocode the key word OUTPUT or PRINT are
used to input data into the program.

Example 1: (Pseudocode)

Write a program that adds 2 numbers and outputs the sum

START

DECLARE Num1, Num2, Sum : INTEGER

INPUT “Please enter the first number “, Num1

INPUT “Please enter the second number “, Num2

Sum Num1+Num2

OUTPUT “The sum is “,Sum

END

Example 1: (Visual Basic)

Write a program that adds 2 numbers and outputs the sum


Module Module1

Sub Main()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Sum As Integer
Console.WriteLine("Please enter the first number")
Num1 = Console.ReadLine
Console.WriteLine("Please enter the second number")
Num2 = Console.ReadLine
Sum = Num1 + Num2
Console.WriteLine("The sum of the 2 numbers is" & Sum)
Console.ReadKey()

End Sub

End Module
Example 2

Example 2
Question: Write a program which subtracts 2 numbers
Module Module1

Sub Main()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Difference As Integer
Console.WriteLine("Enter the first number")
Num1 = Console.ReadLine
Console.WriteLine("Please enter the second number")
Num2 = Console.ReadLine
Difference = Num1 - Num2
Console.WriteLine("The difference of the 2 numbers is " & Difference)
Console.ReadKey()

End Sub

Example 3: Write a program which multiplies 2 numbers


Module Module1
Sub Main()
Dim Num1 As Integer
Dim Num2 As Integer
Dim Mult As Integer
Console.writeline(“Enter the first number”)
Num1=Console.Readline
Console.Writeline(“Please enter the second number”)
Num2=Console.readline
Mult=Num1*Num2
Console.Writeline(“The difference of the 2 numbers is “&Mult)
Console.Readkey()
End Sub
End Module

Example 4: Write a program that asks the user to input their name and age and
output it
Module Module1
Sub Main()
Dim FirstName As String
Dim Surname As String
Dim Age As Integer
Console.writeline(“Enter the first Name”)
FirstName=Console.Readline
Console.Writeline(“Please enter the Surname”)
Surname=Console.readline
Console.Writeline(“Please enter the Age”)
Surname=Console.readline
Console.Writeline(“You said your first name is: “&FirstName)
Console.Writeline(“You said your Surname is: “&Surname)
Console.Writeline(“You said your Age is: “&Age)
Console.Readkey()
End Sub
End Module

3.0 Assignment and Initialisation


3.1 Assignment
Assignment:
Assignment is the process of writing a value into a variable (a named memory location). For
example, Count ← 1 can be read as ‘Count is assigned the value 1’, ‘Count is made equal to
1’ or ‘Count becomes 1’.
3.2 Initialization:
If an algorithm needs to read the value of a variable before it assigns input data or a
calculated value to the variable, the algorithm should assign an appropriate initial value to
the variable, known as Initialization.
NB: Any variable that will be used to store a value of a calculation must be initialised.
Examples of variables which need to be initialised are: Total, Count, Highest, Lowest
4.0 Totalling and Counting
4.1 Totalling:
To keep a running total, we can use a variable such as Total or Sum to hold the running total
and assignment statements such as:
Total ← Total + Number
ADD Number to Total
4.2 Counting:
It is sometimes necessary to count how many times something happens.
To count up or increment by 1, we can use statements such as:
Count ← Count + 1
INCREMENT Count by 1

5.0 Efficiency:
An algorithm‟s efficiency can be judged in terms of:
• Speed: How quick the algorithm produces the required output.
• Memory requirements: How many lines of code the algorithm contains.
Correctness:
Although an algorithm is expected to produce the correct outputs, correctness can still be
measured in terms of:
Accuracy:
How many decimal places produce output with greater accuracy (e.g. more decimal places)
Range:
Will the algorithm work with the complete range of inputs? Or can it only deal with positive
numbers, whole numbers, numbers below 1 million, etc.
Reliability:
Will the algorithm always produce correct output within the range that it is designed to work?
Or are there values which it will not accept (e.g. zero).
Appropriateness: Appropriateness can be measured in terms of:
Length: If the problem is simple then a short algorithm would normally be expected.
Speed: if the output needs to be generated quickly, then the algorithm must be able to
generate
output quick enough.
Memory requirements: An algorithm controlling a washing machine must not require a lot
of memory!
6.0 Programming Constructs (Control Structures)
Control structures are used to control the program for it to perform what the
programmer wants.
Think of a Car: A car has many controls available to make it do anything that the
driver wants it to do. For example, when the driver wants the car to increase speed,
he steps on the accelerator pedal, when he wants the car to stop he steps on the
brake pedal. When he wants the car to turn left he turns the steering wheel leftwards.
In a similar manner, programmers need to be able to control their programs for
example: To execute instructions in a certain order or sequence, to check a condition
and select the path that corresponds to whether the condition is TRUE or FALSE
When writing a program, we use 3 control structures:
 Sequence
 Selection
 Iteration

1. SEQUENCE
Programming statements are executed one after another in the order in which they
are written. The order of the statements is important.

2. SELECTION
This is when a program needs to make a decision and select a path depending on a
given condition. Conditional statements are used for selection and these are:
1. IF…THEN…ELSE…ENDIF
2. CASE…OF…OTHERWISE…ENDCASE

Example 1: Question: Write a program that prompts the user to input the name
and computer science mark for a student and output the grade:
90-100 A*
80-89 A
70-79 B
60-69 C
50-59 D
30-49 E
0-29 U
//USING IF…THEN…ELSE…ENDIF
START
DECLARE StudentName : STRING
DECLARE CompSciMark : INTEGER
INPUT “Please enter your computer science mark”, CompSciMark
INPUT “Please enter your name”, StudentName
IF CompSciMark <=100 AND CompSciMark >=90 THEN
OUTPUT “A*”
ELSEIF CompSciMark <=89 AND CompSciMark >=80 THEN
OUTPUT “A”
ELSEIF CompSciMark <=79 AND CompSciMark >=70 THEN
OUTPUT “B”
ELSEIF CompSciMark <=69 AND CompsciMark >=60 THEN
OUTPUT “C”
ELSEIF CompSciMark <=59 AND CompSciMark >=50 THEN
OUTPUT “D”
ELSEIF CompSciMark <=49 AND CompSciMark >=40 THEN
OUTPUT “E”
ELSEIF CompSciMark <=29 AND CompSciMark >=0 THEN
OUTPUT “U”
ELSE
OUTPUT “You have entered an invalid mark”
ENDIF
END

//USING CASE…OF…OTHERWISE…ENDCASE
START
DECLARE StudentName : STRING
DECLARE CompSciMark : INTEGER
INPUT “Please enter your name”, StudentName
INPUT “Please enter your computer science mark”, CompSciMark
CASE CompSciMark OF:
90 TO 100 :
OUTPUT “A*”
80 TO 89 :
OUTPUT “A”
70 TO 79 :
OUTPUT “B”
60 TO 69 :
OUTPUT “C”
50 TO 59 :
OUTPUT “D”
40 TO 49 :
OUTPUT “E”
0 TO 29:
OUTPUT “U”
OTHERWISE
OUTPUT “You have entered an invalid mark”
ENDCASE
END

Write a program that prompts the user to input their name and age. The program
must output the following messages depending on the age of the person:
Greater than 140 – “You no longer exist”
80 to 140 – “you are too old to drive”
16 to 80 – “You are allowed to drive “
Great than 0 to 15 – “You are too young to drive. You are a baby”
Less than 1 – “Error. You have entered an invalid age”

3. ITERATION/LOOPING/REPETITION
It is a construct which is used when an action has to be repeated more than
once. When an action needs to be repeated more than once we use looping.
There are 3 types of loops:
1. FOR…TO…NEXT
2. REPEAT…UNTIL
3. WHILE…DO…ENDWHILE
FOR…TO…NEXT Loop
It is used when the number of iterations/repetitions is known. It is a count controlled
loop – meaning it can move the counting variable on its own, from the lower limit to
the upper limit. “It can count on its own”.

E.g: write a program that displays the name “Sam Rundle” 10 times.
START
DECLARE Count : INTEGER
Count0

FOR Count1 TO 10
OUTPUT “Sam Rundle”
NEXT Count
END

Write a program that prompts the user to input their name and age and outputs them
10 times

START
DECLARE StudentName : STRING
DECLARE StudentAge : INTEGER
DECLARE Count : INTEGER
Count0

INPUT “Enter a name for the student”,StudentName


INPUT “Enter the age of the student”,StudentAge

FOR Count1 TO 10
OUTPUT “The student name is “,StudentName
OUTPUT “The student age is”, StudentAge
NEXT Count
END
Example 2: Write a program that prompts the user to input 10 numbers and
calculates and outputs the total.
START
DELCARE Number : INTEGER
DECLARE Total : INTEGER
DECLARE Count : INTEGER
Count0
Total0
FOR Count1 TO 10
INPUT “Enter a number: “, Number
TotalTotal+Number
NEXT Count
OUTPUT “The total is : “,Total
END

Example 3:
Write a program that prompts the user to input 10 numbers and stores them in
an ARRAY, and add the average them.

START
DECLARE Numbers[1:10] : INTEGER
DECLARE Sum, Count : INTEGER
DECLARE Average : REAL
Sum0
Average 0
Count0

FOR Count1 TO 10
INPUT “Enter a number”, Numbers (Count)
SumSum+Numbers(Count)
NEXT Count

FOR Count1 TO 10
OUTPUT “The numbers are:”, Numbers(Count)
NEXT Count

AverageSum/10

OUTPUT “The total is “, Sum


OUTPUT “The average is “, Average
END

REPEAT…UNTIL
Repeats an action while the condition is FALSE until it becomes TRUE. It is a
conditional loop, meaning it *CHECKS A CONDITION*.
It tests the condition at the bottom (Bottom Tested loop/Post Condition loop) –
meaning, the program will enter the loop at least once before a condition is tested.

EXAMPLE 1:
Write a program that prompts the user to input a number and add it to the total until
the total is greater than 200. No need to store the numbers, just store the total.

START
DECLARE Number, Total : INTEGER
Total 0
REPEAT
INPUT “Please enter a number”, Number
TotalTotal+Number
UNTIL Total>200
OUTPUT “The total of the numbers is: “,Total
END

Example 2:
Write a program that prompts the user to input a PIN until they input a correct
PIN = 1234

START
CONSTANT Pin : STRING=”1234”
DECLARE PinGuess : STRING
REPEAT
INPUT “enter the pin: “, PinGuess
UNTIL PinGuess=Pin
OUTPUT “You have finally entered the correct pin”
END
Example 3:
Write a program that prompts the user to input a PIN until they input a correct
PIN = 1234. The program must allow a person 5 chances only

START
DECLARE Pin : STRING
DECLARE PinGuess : STRING
DECLARE Count : INTEGER
Count0
Pin”1234”
REPEAT
INPUT “ enter the pin: “, PinGuess
CountCount+1
UNTIL PinGuess=Pin OR Count=5
IF PinGuess=Pin THEN
OUTPUT “You have finally entered the correct pin”
ELSE
OUTPUT “You exhausted your 5 chances”
END IF
END

WHILE…DO…ENDWHILE

It is a conditional loop. It is a (PRE CONDITION/TOP TESTED LOOP). It tests the


condition at the top, meaning the program may never enter the loop at all if the
condition is not met.
It repeats while the condition is true until it becomes false

Example 1: Write a program that prompts the user to input a number and adds it to
the total until the total is greater than 200. No need to store the numbers, just store
the total.

START
DECLARE Number, Total : INTEGER
Total 0

WHILE Total<=200
INPUT “Please enter a number”, Number
TotalTotal+Number
ENDWHILE
OUTPUT “The total of the numbers is: “,Total
END

Array
An array is a data structure for storage of values which has many spaces and can
store multiple values under the same Identifier and datatype.

Example 1: Write a program that prompts the user to input 10 student names and
stores them in an array and then displays them.

START
DECLARE StudentNames[1:10] : STRING
DECLARE Count : INTEGER
Count0

FOR Count1 TO 10
INPUT “Enter a name of the student”,StudentName(Count)
NEXT Count

FOR Count1 TO 10
OUTPUT “The name of the student is : “ , StudentName(Count)
NEXT Count
END

Mr A Ncube , the geography teacher wants to grade and split his class into two sets
depending on the percentage marks. If a student scores 70 and above they are in
set 1 if they score less than 70 than they are put in set 2. The number of students
is 100. Show how many are in set 1 and how many are in set 2
START
DECLARE Set1Count, Set2Count, Index : INTEGER
DECLARE StnGeoMark : REAL
Index0
Set1Count0
Set2Count0

FOR Index1 TO 100


REPEAT
INPUT “Please enter a mark”,StnGeoMark
IF StnGeoMark>=70 AND StnGeoMark<=100
THEN
Set1Count=Set1Count+1
ELSE
IF StnGeoMark<70 AND StnGeoMark >=0
THEN
Set2Count=Set2Count+1
END IF
ELSE
OUTPUT “Invalid Mark entered”
OUTPUT “reenter mark”
ENDIF
UNTIL StnGeoMark>=0 AND StnGeoMark<=100
NEXT Index
OUTPUT “The number of students in set 1 is”, Set1Count
OUTPUT “The number of students in set 2 is “,Set2Count
END

Write a program that prompts the user to enter 20 numbers that are between -90 and
90.The program must calculate and output the sum and average of the positive and
negative numbers

START
DECLARE Number, NegativeNumCount, PositiveNumCount, NegativeSum,
PositiveSum, Index : INTEGER
DECLARE NegativeAverage, PositiveAverage : REAL
NegativeNumCount0
PositiveNumCount0
Index0
NegativeSum0
PositiveSum0

FOR Index1 TO 20
REPEAT
INPUT “Please Enter a Number”,Number
IF Number<0 AND Number>=-90 THEN
NegativeNumCount+=1
NegativeSum=NegativeSum+Number
ELSEIF
Number>=0 AND Number =<90 THEN
PositiveNumCount+=1
PositiveSum=PositiveSum+Number
ELSE
OUTPUT “Invalid Number re-enter”
ENDIF
UNTIL Number>=-90 AND Number=<90
NEXT Index
NegativeAverage=NegativeSum/NegativeNumCount
PositiveAverage=PositiveSum/PositiveNumCount
OUTPUT “The Negative Sum is”,NegativeSum
OUTPUT “The Positive Sum is”,PositiveSum
OUTPUT “The Negative Average is”,NegativeAverage
OUTPUT”The Positive Average is “,PositiveAverage
END

You might also like