CSC102: Programming Techniques
Arrays & Functions
Isaac Armah-Mensah
Department of Computer Science & Information Technology
University of Cape Coast
Introduction
What Are Arrays and Functions?
Arrays
A data structure that stores a fixed-size, ordered collection of elements, all of the
same type.
Functions
A reusable block of code that performs a specific task.
Why both? Arrays store data; functions manipulate data. Together they form the
backbone of most programs.
1
Array
Definition 1
An array is a collection of elements of the same data type stored under a single
name and accessed using an index.
Index 0 1 2 3 4
Value 75 82 90 67 88
• Stores multiple values using one variable name.
• Elements are accessed using indices.
• Elements are stored in contiguous memory locations.
2
3
4
Declaring and Initializing Arrays
Declaration and Initialization
// Create and initialize
scores = [78, 92, 65, 88, 95]
// Create an array of 5 integers
numbers = [0, 0, 0, 0, 0]
// Create an empty array of size 10
data = new array of size 10
• Arrays can be initialized when created.
• All elements must have the same data type.
• The size determines the number of elements stored.
5
6
Array Traversal
Processing Every Element
Traversal means visiting every element in the array.
Display Elements Input Elements
FOR i = 0 TO 4 FOR i = 0 TO 4
PRINT scores[i] READ scores[i]
END FOR END FOR
• The loop variable represents the index.
• Each iteration processes one element.
• Traversal is the most common array operation.
7
8
9
10
11
12
Example
13
14
15
16
Functions
Functions
Definition 2
A function is a named block of statements that performs a specific task and may
return a value.
Why Functions? Main Parts of a Function
• Reduce repeated code. 1. Function Name
• Improve program organization. 2. Parameters
• Make programs easier to test. 3. Function Body
• Allow tasks to be reused. 4. Return Value
• Simplify large programs.
Write a function once and reuse it whenever needed.
17
18
19
20
21
22
23
Question 1: Hospital Temperature Monitoring
Example 1
A hospital monitors the daily body temperatures (in °C) of ten patients admitted
to an isolation ward.
The temperatures are stored in an array.
The medical team wants to determine:
• The average temperature.
• The highest temperature recorded.
• The number of patients whose temperature exceeds 38°C.
Write an algorithm in pseudocode to solve the problem.
24
Question 1: Hospital Temperature Monitoring
Solution 1
START
temp =[37.2,39.1,38.5,36.9,37.8,39.4,38.7,37.1,40.0,38.2]
sum = 0
highest = temp[0]
count = 0
FOR i = 0 TO 9
sum = sum + temp[i]
IF temp[i] > highest THEN
highest = temp[i]
END IF
IF temp[i] > 38 THEN
count = count + 1
END IF
END FOR
average = sum / 10
PRINT average, highest, count
END 25
Question 2: Mobile Money Transactions
Example 2
A mobile money agent records the values of eight customer transactions during
a working day.
The transaction amounts are stored in an array.
At the end of the day, the agent wants to know:
• The total value of all transactions.
• The largest transaction.
• The smallest transaction.
Write an algorithm in pseudocode to solve the problem.
26
Question 2: Mobile Money Transactions
Solution 2
START
trans =[120,85,300,250,90,450,175,210]
total = 0
largest = trans[0]
smallest = trans[0]
FOR i = 0 TO 7
total = total + trans[i]
IF trans[i] > largest THEN
largest = trans[i]
END IF
IF trans[i] < smallest THEN
smallest = trans[i]
END IF
END FOR
PRINT total, largest , smallest
END 27
Question 3: WAEC Examination Analysis
Example 3
A secondary school stores the Mathematics examination scores of fifteen students
in an array.
The headmaster wants to determine:
• The number of students who passed.
• The number of students who failed.
• The highest score.
• The average score.
A score of 50 or above is considered a pass.
Write an algorithm in pseudocode to solve the problem.
28
Question 3: WAEC Examination Analysis
Solution 3
START FOR i = 0 TO 14
scores = sum = sum + scores[i]
[65,72,48,90,81, IF scores[i] > highest THEN
39,55,67,44,73, highest = scores[i]
85,92,50,61,47] END IF
pass = 0 IF scores[i] >= 50 THEN
fail = 0 pass = pass + 1
sum = 0 ELSE
highest = scores[0] fail = fail + 1
END IF
END FOR
average = sum / 15
PRINT pass
PRINT fail
PRINT highest
PRINT average
29
END
Question 4: Student Assessment Processing
Example 4
A lecturer stores the assessment records of five students in a two-dimensional
array.
Each row represents a student and each column represents:
• Quiz 1
• Quiz 2
• Examination
The lecturer wants to calculate the total score and grade for each student.
Use the grading system:
A (80–100), B (70–79), C (60–69), D (50–59), F (Below 50)
Write an algorithm in pseudocode to solve the problem.
30
Solution 4
Solution 4
START
scores =
[
[18,20,45],
[15,16,40],
[20,19,50],
[17,18,42],
[19,20,48]
]
FOR row = 0 TO 4
total = 0
FOR col = 0 TO 2 31
Question 5: Electricity Billing Function
Example 5
An electricity company charges customers based on monthly consumption.
To avoid repeating calculations throughout the billing system, management wants
a reusable function.
Write a function named CalculateBill that accepts consumption and rate as pa-
rameters and returns the total bill after adding a fixed service charge of GH¢
12.50.
Solution 5
FUNCTION CalculateBill(consumption, rate)
bill = (consumption * rate) + 12.50
RETURN bill
END FUNCTION
32
Question 6: Bank Loan Eligibility Function
Example 6
A bank approves loans only if a customer’s monthly income is at least GH¢ 3,000.
Write a function named CheckEligibility that accepts monthly income and returns:
• ”Eligible”
• ”Not Eligible”
depending on the customer’s income.
33
Solution 6
Solution 6
FUNCTION CheckEligibility(income)
IF income >= 3000 THEN
RETURN "Eligible"
ELSE
RETURN "Not Eligible"
END IF
END FUNCTION
34
Question 7: Shipping Cost Function
Example 7
An online shopping company calculates shipping charges based on package
weight.
Write a function named ShippingCost that accepts package weight and cost per
kilogram and returns the total shipping charge.
35
Solution 7
Solution 7
FUNCTION ShippingCost(weight, rate)
cost = weight * rate
RETURN cost
END FUNCTION
36
Question 8: Best Salesperson Award
Example 8
A company stores the monthly sales figures of its sales representatives in an array.
Management wishes to identify the salesperson with the highest sales figure.
Write a function named HighestSales that accepts the sales array and its size and
returns the highest sales value recorded.
37
Solution 8
Solution 8
FUNCTION HighestSales(arr, size)
highest = arr[0]
FOR i = 1 TO size - 1
IF arr[i] > highest THEN
highest = arr[i]
END IF
END FOR
RETURN highest
END FUNCTION
38
Question 9: Scholarship Selection
Example 9
A university stores students’ academic scores in an array.
Students who score 75 or above qualify for a scholarship.
Write a function named CountScholarshipStudents that accepts the score array
and its size and returns the number of students who qualify.
39
Solution 9
Solution 9
FUNCTION CountScholarshipStudents(arr, size)
count = 0
FOR i = 0 TO size - 1
IF arr[i] >= 75 THEN
count = count + 1
END IF
END FOR
RETURN count
END FUNCTION
40
Question 10: Rainfall Analysis
Example 10
A meteorological station records daily rainfall measurements for a month and
stores the values in an array.
Researchers want to determine the average rainfall recorded during the month.
Write a function named AverageRainfall that accepts the rainfall array and its
size and returns the average rainfall.
41
Solution 10
FUNCTION AverageRainfall(arr, size)
sum = 0
FOR i = 0 TO size - 1
sum = sum + arr[i]
END FOR
average = sum / size
RETURN average
END FUNCTION
42
43