0% found this document useful (0 votes)
10 views

8 - Programming NEW 2210 (VB, Python) (MT - L)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

8 - Programming NEW 2210 (VB, Python) (MT - L)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Computer Science 2210/0478 with

Section 8: Programming Majid Tahir www.majidtahir.com


1

Syllabus Content:
8.1 Programming concepts
Candidates should be able to:
1- Declare and use variables and constants
2- Understand and use basic data types including
o Integer
o Real
o Char
o Boolean
o String
o Date
3- Understand and use input and output
4- Types of programs including:
o (a) Understand and use the concept of sequence
o (b) Understand and use the concept of selection
 IF Statements
 CASE Statements
o (c) Understand and use the concept of iteration
 Count controlled loop
 Pre Condition loop
 Post condition loop
o (d) Understand and use the concepts of totalling and counting
o (e) Understand and use the concept of string handling
o (f) Understand and use arithmetic, logical and Boolean operators
5 Understand and use nested statements
o Including nested selection and iteration
o Candidates will not be required to write more than three levels of nested
statements

8.2 ARRAYS
Candidates should be able to:
1 Declare and use one-dimensional (1D) and two-dimensional (2D) arrays
2 Understand the use of arrays
3 Write values into, and read values from, an array using iteration

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 1


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

8.1 Algorithms:
An algorithm is a sequence of steps done to perform some task.
The essential aim of an algorithm is to get a specific output,
An algorithm involves with several continuous steps,
The output comes after the algorithm finished the whole process.
So basically, all algorithms perform logically while following the steps to get an output for a
given input.

Types of Algorithms:
Structured English
Flowcharts
Pseudo codes
Program Code
STRUCTURED ENGLISH:
Structured English provides a more formal way of documenting the stages of the algorithm.
Structured English is a subset of English language that consists of command statements used
to describe an algorithm.
FLOWCHARTS:
Flow chart is a graphical representation of a program.
Flowcharts use different symbols containing information about steps or a sequence of events.

Symbol Name Usage

Terminator To start and stop the program

INPUT or OUTPUT To INPUT or OUTPUT data

Process To show a process

To Represent a Pre Defined


PROCEDURE or FUNCTION
Function/Procedure/Subroutine

A Condition statement with


Decision Symbol
Yes/No/True/False decision

Represent the flow of data


Data flow lines
from one component to next.

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 2


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

PSEUDOCODE: Pseudo code is an outline or a rough draft of a program, written


as a series of instruction.
Pseudo code uses keywords commonly found in high-level programming languages,
without being bound to the syntax of any particular language.
It describes an algorithm’s steps like program statements.

Variable:
Variable is a named memory location with DataType where value can be stored.
The content of a variable can change at runtime

Constants:
Just like variables, constants are "dataholders". They can be used to store data that is
needed at runtime.
In contrast to variable, the content of a constant can't change at runtime, it has a
constant value.
Before the program can be executed (or compiled) the value for a constant must be
known.

Arithmetic
Use the arithmetic operators.

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’.

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.

Input
We indicate input by words such as INPUT, READ or ENTER, followed by the name of
a variable to which we wish to assign the input value.

Output:
We indicate output by words such as OUTPUT, WRITE or PRINT, followed by a
comma-separated list of expressions.

Totaling
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 (Adds Number to Total)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 3


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

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
Structured statements
In the sequence structure the processing steps are carried out one after the other. The
instructions are carried out in sequence, unless a selection or loop is encountered.

Mathematical Operators in Pseudocodes and Programming languages


Pseudocode Operator Operator Mathematical operator
(VB) (Python)
+ + + Addition

- - - Subtraction

* * * Multiplication

/ / / Division

= = == Equal

<> <> != Not equal

MOD Mod % Modulus

^ ^ ** Exponent 23 = 2^3 or 2**3


Logical Operators in Pseudocodes and Programming languages
Pseudocode Operator Operator Comparison
(VB) (Python)
> > > Greater than
< < < Less than
>= >= >= Greater than equal to
<= <= <= Less than equal to
= = == Equals to
<> <> != Not equal
() () () Group in Brackets
^ ^ Exponent
**
OR OR OR Or
NOT NOT NOT Not
AND AND AND And

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 4


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

8.1 Data types

The following table shows the Visual Basic data types, their supporting common
language runtime types, their nominal storage allocation, and their value ranges.

Basic Data Types:


A variable can store one type of data. The most used data types are:

Pseudo Operator Operator DATA TYPE Formats


code (VB) (Python)
INTEGER Integer int Integer (Whole numbers)
REAL Decimal float Decimal numbers
CHAR Char Single character e.g “F” for female or
Not used in Python
“M” for male
BOOLEAN Boolean bool Boolean e.g True or False
STRING String str Text
DATE Date class datetime Date

8.1 Declaration of Variables and Constant:

The process of creating a variable is called declaring a variable. Variables must be


created or declared where users enter their data.
Pseudo code

BEGIN
DECLARE variable : Datatype
Variable 0 //initialization
OUTPUT (“What is your Email address”)
INPUT variable value\
IF valid email address?
Then ...
END IF

Each declaration needs 4 things:

Pseudo code VB code example:


• DECLARE keyword
• Variable name
• : keyword
• Variable data type

DECLARE variable : Datatype

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 5


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Declaring Multiple Variables:


Pseudocodes VB Code Console Mode
DECLARE index : Integer Dim index As Integer
DECLARE grade : Integer Dim grade As Integer
DECLARE counter : Integer Dim counter As Integer
The three declarations above can be rewritten as one declaration if same data type is used:
DECLARE index, grade, counter : Integer
VB Code Console Mode
Dim index, grade, counter As Integer
In Python you have to initialize variable with a value
PYTHON:
Index, grade, counter = 0
Constants
Creating Constants in Pseudocode is just writing costant name and value with it. In contrast to variable,
the content of a constant can't change at runtime, it has a constant value.
Pseudocode: VB Console mode:
CONSTANT <identifier> = <Value> Const pi As Decimal = 3.1415
CONSTANT Pi 3.1415 Dim Pi As Decimal = 3.1415
or
PYTHON:
CONSTANT Pi = 3 .14 ConstPi =3.1415
Type of Programs:
 Sequence
 Selection
 Repetitions/Loops
Sequence
Statements are followed in sequence so the order of the statements in a program is
important. Assignment statements rely on the variables used in the expression on the
right-hand side of the statement all having been given values. Input statements often
provide values for assignment statements. Output statements often use the results from
assignment statements.
start
PSEUDOCODE Flow Chart
BEGIN
DECLARE num1, num2 : Integer INPUT
DECLARE sum, profduct :Integer
num1, num2
PRINT (“Enter number 1”)
INPUT number1
PRINT (“Enter number 2”) Sum = num1 + num2
INPUT number2 prod = num1 * num2
Sum number1 + number2
product number1 * number2
PRINT (“the sum is”, sum) OUTPUT
PRINT (“the product is”, product)
sum, prod
END

End
www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 6
Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

VB code PYTHON
Sub main() num1=int(input("enter number1"))
Dim Num1 As Integer num2=int(input("enter number2"))
Dim Num2 As Integer total = num1+num2
Dim Sum As Integer prod = num1*num2
Dim Procduct As Integer print("Total is", total)
Console.Writeline(“Enter number1”) print("Product is", prod)
Num1 = Console.Readline()
Console.Writeline(“Enter number2”)
Num2 = Console.Readline()
Sum = Num1+Num2
Product = Num1*Num2
Console.Writeline(“Sum is” & sum)
Console.Writeline(“Product is” & Product)
End Sub

STRUCTURED ENGLISH FLOWCHART

Pseudocode VB Code

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 7


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Pseudocode: PYTHON:
BEGIN miles = float(input("enter miles"))
DECLARE miles,km : REAL km = miles*1.61
OUTPUT (“Enter miles”) print("Kilometers are:", km)
INPUT miles
km miles * 1.61
OUTPUT(“Km are : ” & km)
END

11.2 Structured statements for selection (conditional statements)


These statements are used to select alternative routes through an algorithm; selection’s
logical expressions often involve comparisons, which can operate on text strings as well
as numbers
IF…THEN…ELSE…ENDIF
CASE…OF…OTHERWISE…ENDCASE
IF…THEN…ELSE…ENDIF
For an IF condition the THEN path is followed if the condition is true and the ELSE path
is followed if the condition is false.
There may or may not be an ELSE path. The end of the statement is shown by ENDIF.
A condition can be set up in different ways:
IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND (Age < 70)
THEN
PRINT ("You can ride")
ELSE
PRINT ("Too small, too young or too old")
ENDIF
CASE … OF … OTHERWISE … ENDCASE
For a CASE condition the value of the variable decides the path to be taken. Several
values are usually specified. OTHERWISE is the path taken for all other values. The
end of the statement is shown by ENDCASE.
The algorithm below specifies what happens if the value of Choice is 1, 2, 3 or 4.
CASE Choice OF
1: Answer ← Num1 + Num2
2: Answer ← Num1 - Num2
3: Answer ← Num1 * Num2
4: Answer ← Num1 / Num2
OTHERWISE PRINT ("Please enter a valid choice")
ENDCASE

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 8


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

The IF THEN ELSE statement

PSEUDOCODE FLOWCHART:
BEGIN
START
DECLARE marks : Integer
PRINT ("Enter your grade")
INPUT marks
IF marks > 50 INPUT
THEN marks
PRINT ("You have passed")
ELSE
PRINT (“You’ve failed”) Yes OUTPUT
END IF IF marks>50 (“Pass”)
END No

OUTPUT
(“Fail”)

STOP

VB Code
PYTHON Code:
Sub main()
marks = int(input(" Enter your marks ")) Dim marks As Integer
if marks>=50:
Console.Writeline(“Enter marks”)
print("Pass")
else: print("Fail") Marks = Console.Readline()
If marks >= 50 Then
Console.Writeline(“pass”)
Else
Console.Writeline(“fail”)
End If
End Sub

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 9


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

FLOWCHART:

START

INPUT
marks

Yes
OUTPUT
IF marks >=
(“Grade A”)

No
Yes OUTPUT
IF marks >= 60
(“Grade B”)
No

IF marks >=50 OUTPUT (“Grade C”)

No Yes

OUTPUT (“Grade U”)

STOP

IF THEN, ELSE-IF statements VB code example

BEGIN Sub main()


DECLARE marks : INTEGER Dim marks As Integer
PRINT ("Enter marks") Console.Writeline(“Enter marks”)
INPUT marks Marks = Console.Readline()
IF marks >= 80 If marks >= 80 Then
THEN PRINT ("Grade A")
Console.Writeline(“ A ”)
ELSE IF marks >= 60
THEN PRINT ("Grade B") Elseif marks >= 60 Then
ELSE IF marks >= 50 Console.Writeline(“ B ”)
THEN PRINT ("Grade C") Elseif marks >= 50 Then
ELSE PRINT ("Grade U")
Console.Writeline(“ C ”)
END IF
END IF Else
END IF Console.Writeline(“ U ”)
End If
END
End Sub

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 10


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Python code OUTPUT

marks = int(input(" Enter your marks "))


if marks>=80:
print("Grade A")
elif marks>=60:
print("Grade B")
elif marks>=50:
print("Grade C")
else:
print("Grade U")
int("Grade U")

The IF statement is useful, but can get clumsy if you want to consider “multi-way
selections

CASE Statement START FLOWCHART:

INPUT num1,
num2, Opvalue

Yes
Ans = num1+num2
Opvalue = “+”

No Yes
Opvalue = “-” Ans = num1-num2

No
Yes Ans = num1 * num2
Opvalue = “ * ”

No
Opvalue = “/” Ans = num1 / num2

No Yes

OUTPUT (“ Invalid Choice”)

STOP

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 11


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

CASE OF OTHERWISE… Pseudocode


BEGIN
DECLARE num1, num2, Ans : INTEGER
DECLARE Opvalue : CHAR
INPUT num1, num2
OUTPUT (“Enter Operator value + add, - sub, * multiply, / divison”)
INPUT Opvalue
CASE OF OpValue
"+" : Answer ¨ Number1 + Number2
"-" : Answer ¨ Number1 - Number2
"*" : Answer ¨ Number1 * Number2
"/" : Answer ¨ Number1 / Number2
OTHERWISE OUTPUT ("Please enter a valid choice")
ENDCASE
OUTPUT (“Answer is :”,Ans)
END
PYTHON
num1=int(input("Enter number 1 "))
num2=int(input("Enter number 2 "))
OpValue = ("Enter Opvalue, + is add), - is subract), * is Multiply, / is divide")
if OpValue == "+":
Answer = num1 + num2
elif OpValue == "-":
Answer = num1 + num2
elif OpValue == "*":
Answer = num1 + num2
elif OpValue == "/":
Answer = num1 + num2
else: print("invalid operator")
print("Answer is : ", Answer)
Visual Basic (Console mode) .
Dim Num1, Num2, Answer As Integer
Dim Opvalue As Char
Console.Writeline(“INPUT num1 and num2”)
Num1 = Console.Readline()
Num2 = Console.Readline()
Console.Writeline(“Enter Opvalue”)
Console.Writeline (+ add, - sub, * multiply, / divison”)
Opvalue = Console.Readline()
Select CASE OpValue
CASE "+"
Answer = Number1 + Number2
CASE "-"
Answer = Number1 - Number2
CASE "*"
Answer ¨ Number1 * Number2
CASE "/"
Answer ¨ Number1 / Number2
CASE Else
Console.Writeline ("input valid choice")
End Select
Console.Writeline (“Answer is :” & Answer)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 12


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

CASE OF OTHERWISE… FLOWCHART

START

Pseudo code
INPUT marks
BEGIN
DECLARE marks : Integer
OUTPUT
PRINT ("Enter your marks") marks>=80? Yes (“Grade A”)
INPUT marks
CASE OF marks no
80 >= :PRINT("Grade A") marks>=70? Yes OUTPUT
70 >= :PRINT("Grade B") (“Grade B”)
60 >= :PRINT("Grade C") no
60 >= :PRINT("Grade D") OUTPUT
40 >= :PRINT ("Grade E") marks>=60? Yes (“Grade C”)
OTHERWISE
PRINT("Grade U, Repeat Exam") no
marks>=50? OUTPUT
END CASE Yes (“Grade D”)
END no

yes
OUTPUT
marks>=40? (“Grade D”)

no

OUTPUT
(“Grade U)

STOP

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 13


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Program Code in Visual Basic Console Mode:


Sub Main()
Dim marks As Integer
Console.WriteLine("Input your marks")
marks = Console.ReadLine()
Select Case marks
Case >= 80
Console.WriteLine("Grade A")
Case >= 60
Console.WriteLine("Grade B")
Case >= 50
Console.WriteLine("Grade C")
Case Else
Console.WriteLine("Grade U")
End Select
End Sub

Python does’t use CASE Statements so Elif is used:


PYTHON Code
marks = int(input(" Enter your marks "))
if marks>=80:
print("Grade A")
elif marks>=60:
print("Grade B")
elif marks>=50:
print("Grade C")
else: print("Grade U")

LOOPS (Structured statements for iteration (repetition)


Many problems involve repeating one or more statements, so it is useful to have
structured statements for controlling these iterations or repetitions.

Exit conditions consist of logical expressions whose truth can be tested, such as
Count = 10 or Score < 0.

At a particular time, a logical expression is either True or False.


There are three type of Loops
FOR…TO…NEXT (Count controlled Loop)
WHILE…DO…ENDWHILE (Pre- Condition Loop)
REPEAT…UNTIL (Post Condition Loop)

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 14


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

FOR … NEXT LOOP


This is to be used when loop is to be repeated a known fixed number of times.
The counter is automatically increased each time the loop is performed.

FOR count = 1 to 10
INPUT number
total = total + number
NEXT count

WHILE … Do LOOP
This loop is used when we don’t know how many times the loop is to be performed. The Loop is
ended when a certain condition is true.
This condition is checked before starting the loop.

While COUNT < 10 DO


Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Endwhile
Output TOTAL

REPEAT … UNTIL LOOP


REPEAT UNTIL Loop is used when we do not know how many times loop will be performed.
The Loop is ended when a certain conation is true.
The Condition is checked at the end of the Loop and so a REPEAT Loop always has to be
performed at least once.

REPEAT
Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Until COUNT = 10
Output Total

FOR Loop PSEUDOCODE

The fore loop repeats statements a set number of time.

It uses a variable to count how many time it goes round the loop and stops when it reaches its limit.

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 15


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

BEGIN
DECLARE count, number : Integer
OUTPUT (“Input a number for its times table")
INPUT number
FOR count = 1 To 20
PRINT (number , “times" , count , “ = ” number * Count”)

NEXT

VB code example FOR LOOP:

Sub Main(args As String())


Console.WriteLine("Times Table Program")
Dim count, num As Integer
Console.WriteLine("please Input a number for its TimesTable")
num = Console.ReadLine()
For count = 1 To 20
Console.WriteLine(num & " Times " & count & " = " & num * count)
Next
End Sub
Output VB

PYTHON Code FOR LOOP

print(" Times Table Program ")


num = int(input("Enter a number for its TimesTable"))
for count in range(1,10):
print(num, " X ", count, " = ", num*count)

Output PYTHON

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 16


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

FLOWCHART FOR LOOP

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 17


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

WHILE DO ENDWHILE loop

The wile loop is known as a test before loop. The condition is tested before entering the loop, but tested
each time it goes round the loop. The number of times the statements within the loop are executed
varies. The test before loop goes round 0 or more times.
This method is useful when processing files and using “read ahead” data

VB Code example PSEUDOCODE


BEGIN
Sub main() DECLARE marks : REAL
Dim marks As Integer INPUT marks
Console.Writeline(“Enter marks”) WHILE marks > 100 OR < 0
marks = Console.Readline() PRINT (“ERROR, RE-Input “)
While marks > 100 OR marks < 0 INPUT marks
Console.Writeline(“REINPUT 0 to 100”) END WHILE
marks = Console.Readline() IF marks>=50
End While THEN
if marks >= 50 Then OUTPUT (“Pass”)
ELSE
Console.Writeline(“ Pass ”)
OUTPUT (“Fail”)
Else
END IF
Console.Writeline(“ Fail ”) END
End If
End Sub PYTHON Code .

REPEAT UNTIL loop

The repeat loop is similar to the while loop, but it tests the condition after the statements have been
executed once. This means that this test after loop goes round 1 or more times.

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 18


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

VB Code PSEUDOCODE
Sub main()
BEGIN
DECLARE name : String
Dim marks As Integer
REPEAT
Do
PRINT (“Enter marks 0 to 100”)
Console.Writeline(“Enter marks 0 to100”)
INPUT marks
Marks = Console.Readline() UNTIL marks>=0 AND marks <=100
Loop Until marks>=100 AND <=100
if marks >= 50 Then IF marks>=50
Console.Writeline(“ Pass ”) THEN
Else OUTPUT(“Pass”)
Console.Writeline(“ Fail ”) ELSE
End If OUTPUT(“Fail”)
End Sub END IF
END
PYTHON Does not have REPEAT LOOP so WHILE Loop is used.

FLOWCHART…WHILE-ENDWHILE

START
marks

(“Grade A”)

INPUT marks (“ ”)

(“ ”)

(“ ”)
LOOP
(“ ”)

(“

WHILE
marks >100 OR < 0 OUTPUT
(“ ERROR, Re Input”)
Yes

No
STOP
marks

(“Grade A”)

www.majidtahir.com (“ ”) WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 19


(“ ”)

(“ ”)
Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

FLOWCHART…REPEAT-UNTIL
START
marks

(“Grade A”)

(“ ”)

LOOP (“ ”)

(“ ”)

(“ ”)

(“

OUTPUT (“Enter marks 0 to 100”)


OUTPUT (name) No

UNTIL
INPUT marks marks>=0 AND <=100

Yes

STOP
marks

10.2 Array Data Type (“Grade A”)

(“ ”)

(“ ”)

An array is a special variable that has one name, but can store multiple values. Each value is stored in an
(“ ”)

element pointed to by an index.


The first element in the array has index value 0, the second has index 1, etc (“ ”)

(“

One Dimensional Arrays


A one dimensional array can be thought as a list. An array with 10 elements, called names, can store 10
names and could be visualized as this: Lower bound of ARRAY can start from 1 or 0

Index Name
1 Fred
2 James
3 Tom
4 Robert
5 Jonah
6 Chris
7 John
8 Matthew
9 Mikey
10 Jack

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 20


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Arrays (One-dimensional arrays)


In order to use a one-dimensional array in a computer program, you need to consider:
• What the array is going to be used for, so it can be given a meaningful name
• How many items are going to be stored, so the size of the array can be determined.
• What sort of data is to be stored, so that the array can be the appropriate data type.
DECLARATION on Blank Array with 10 slots can be done like this:
Pseudocode: VB code example:
DECLARE names[10]: STRING Dim names(9) As String
PYTHON Code
name[]

Entering Values in One-Dimension Array


BEGIN
DECLARE count : Integer
DECLARE name [5] : String // for declaring 5 elements in ARRAY
DECLARE marks [5] : Integer
FOR count = 1 to 5 // for inputting 5 names and grades
PRINT (“Enter Name “& count)
INPUT name (count)
PRINT (“Enter grade for “& name(count))
INPUT marks (count)
NEXT count
FOR count 1 to 5 // for displaying 5 names and grades
PRINT (name (count) & “has marks " & marks(count))
NEXT count
END

PYTHON Code:
name = []
marks = []
for count in range(5):
name.append (str(input("Enter name: ")))
marks.append (int(input("Enter marks ")))
print("Name ", name, " scored ", marks)

OUTPUT screen

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 21


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

VB Code in Console Mode


Dim name(5) As String
Dim marks(5) As Integer
For count = 1 To 5
Console.WriteLine("input name " & count)
name(count) = Console.ReadLine()
Console.WriteLine("input marks " & count)
marks(count) = Console.ReadLine()
While marks(count) > 100 Or marks(count) < 0
Console.WriteLine("Re-Enter marks" & count)
marks(count) = Console.ReadLine()
End While
Next
For count = 1 To 5
Console.WriteLine(name(count) & " scored: " & marks(count))
Next
Output of VB code (Console mode)

Python One-dimensional array with values in it.


num = [1, 2, 3]
num.append(4)
num.extend([5, 6])
print(num) # will print this in output [1, 2, 3, 4, 5, 6]

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 22


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

Another example of One-Dimensional Array


Module Module1
Sub Main()
Dim count As Integer
Dim name(4) As String
Dim marks(4) As Integer
Dim gender(4) As String
For count = 0 To 4
Console.WriteLine("please enter your name" & count)
name(count) = Console.ReadLine()
Console.WriteLine("please enter your gender" & count)
gender(count) = Console.ReadLine()
Console.WriteLine("please enter your marks" & count)
marks(count) = Console.ReadLine()
Next count
For count = 0 To 4
Console.WriteLine("your name is : " & name(count))
Console.WriteLine("your gender is : " & gender(count))
Console.WriteLine("your marks are : " & marks(count))
Next count
Console.ReadKey()
End Sub
End Module

Multi-Dimensional Arrays or Two dimensional Arrays (2D Array):


A multi-dimensional array can be thought of as a table, each element has a row and column index.
Following example declares a two-dimensional array called table with 3 rows and 4 colums
and would be declared in PseudoCode as follows:
DECLARE table(3, 4) : INTEGER

Visual Basic(Console mode)


Dim table(3, 4) : As Integer

Python Code
row, col = 3, 4
table = [[0 for x in range(row)] for y in range(col)]
# Creates a list containing 5 lists, each of 8 items, all set to 0

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 23


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

PSEUDOCODE Example of Two-Dimension Array


BEGIN
DECLARE table(3, 4) : Integer
FOR row = 1 To 3
FOR column = 1 To 4
PRINT("Please Input Value in Row: ",row, "column : ", column)
INPUT table(row, column)
NEXT
NEXT
FOR row = 1 To 3
FOR column = 1 To 4
PRINT ("Row = " & row & "column = " & column & “has Value”)
PRINT (table(row, column))
NEXT
NEXT
END

VB Code Example of Two-Dimension Array


Sub Main()
Dim table(2, 3) As Integer
For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Please Input Value in Row: " & row & "column : " & column)
table(row, column) = Console.ReadLine()
Next
Next
Console.Clear()

For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Row = " & row & "column = " & column & “has Value”)
Console.WriteLine(matrix(row, column))
Next
Next
Console.ReadKey()
End Sub

Multi-Dimensional Arrays:
A multi-dimensional array can be thought of as a table, each element has a row and
column index.
Following example declares a two-dimensional array called matrix and would be
declared by

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 24


Computer Science 2210/0478 with
Section 8: Programming Majid Tahir www.majidtahir.com
1

VB Code for 2-D Array is:

Refrences:
Computer Science by David Watson & Helen Williams
Visual Basic Console Cook Book
Computer Science AS and A level by Sylvia Langfield and Dave Duddell
https://www.sitesbay.com/javascript/javascript-looping-statement
http://wiki.jikexueyuan.com/project/lua/if-else-if-statement.html

www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 25

You might also like