Vous êtes sur la page 1sur 40

Chapter 7

One Dimensional Array

Learning Outcomes
Upon completion of this lecture, learners will be
able to:
grasp the basic concept of one dimensional array
and
apply array in Pascal program

Problem with Variable


A variable is a memory location with a name
var num : integer;

Memory
address

Problem with Variable


Variable that we have been looking at so far
can store only a single data
Suppose you are to write a program that
accepts 3 input numbers and print it back in
the reverse order:
Program Reverse;
Var
num1, num2, num3 : Real;
Begin
Readln(num1, num2, num3);
Writeln(num3, num2, num1);
End.
4

Problem with Variable


What if the number of numbers to be dealt
with is 20 instead of 3?
Program Reverse;
Var
num1, num2 ... num20 : Real;
Begin
Readln(num1, num2 ... num20);
Writeln(num20, num19 ... num1);
End.

What is the difference?


5

Problem with Variable


What if the number of numbers to be dealt
with is 10,000. Do we need 10,000 variables?
That is why we need array.
program array_eg;
var
num: array[1..20] of real;
begin
readln(num[1], num[2] ... num[20]);
writeln(num[20], num[19] ... num[1]);
end.

Array Declaration
Array declaration:
var num: Array[1..20] of Real;

Array name

Can store up
to 20 values

Data type

Array Declaration
var num: array[1..20] of real;
The line:
Readln(num[1], num[2] ... num[20]);

each square bracket is referred by index.

num[1]

num[2]

num[3]

num[4]

num[5] num[6] num[7] num[8]


8

Array Declaration
Each data stored at the given index location is
called an array element
Element must all be of the same data type
except for integers (could be stored in array of
real)

Sample Program 1
// Input & output array elements

// Sum all of the array elements

program array_eg;
var
num: array[1..20] of real;
i : integer;
begin
for i:= 1 to 20 do
readln(num[i]);

program array_sum;
var
num: array[1..20] of real;
i : integer;
sum : real;
begin
sum := 0;
for i:= 1 to 20 do
readln(num[i]);
for i:=1 to 20 do
sum := num[i] + sum;

for i:=1 to 20 do
writeln(num[i]);
end.

writeln(Sum : , sum);
end.

10

Write a program
That reads in a list of 20 numbers, print out the
numbers and prints out the average
The functionality above is to be accessed by the
user through a text menu:
Welcome to Cool Average App!
Please select your choice (1 -> 4):
1. Read in 20 numbers
2. Print out the numbers
3. Compute average.
4. Quit.
11

Write a program Firstly


Declare the array
Program Average_withMenu;
Var
numbers: Array[1..20] of Real;
Begin
..
End.

Note that index starts with 1


12

Write a program Secondly


Show the menu
Program Average_withMenu;
Var
numbers: Array[1..20] of Real;
Begin
Writeln('Welcome to Cool Average App!');
Writeln('Please select your choice (1 -> 4):');
Writeln('1. Read in 20 numbers');
Writeln('2. Print out the numbers');
Writeln('3. Compute average');
Writeln('4. Quit');
End.

13

Write a program Thirdly


Get the users choice
Begin
Writeln('Welcome to Cool Average App!');
Writeln('Please select your choice (1 -> 4):');
Writeln('1. Read in 20 numbers');
Writeln('2. Print out the numbers');
Writeln('3. Compute average');
Writeln('4. Quit');
Readln(choice);
End.

14

Write a program Fourthly


Take action based on the users choice
Readln(choice);
Case choice of
1 : Begin
End;
2 : Begin
End;
3 : Begin
End;
4 : Begin
End;
End;

15

Write a program Fifth


First menu item - Read in 20 numbers
Case choice of
1 : Begin
For counter := 1 to 20 do
Begin
Readln(numbers[counter]);
End;
End;

16

Write a program Sixthly


Second option - Print out the numbers.
2 : Begin
For counter := 1 to 20 do
Begin
Writeln(numbers[counter]);
End;
End;

17

Write a program Seventhly


Third option - Compute the average
3 : Begin
sum := 0;
For counter := 1 to 20 do
sum := sum + numbers[counter];
average := sum / 20;
Writeln('Average =', average);
End;

18

Write a program Finally


Quit!
4 : Begin
halt;
End;

19

Write a program In the end


Now, combine all to get a full program!

20

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.

Program AverageWithMenu;
Uses crt;//winCrt
Var
numbers: Array[1..20] of Real;
sum, average: Real ;
choice, counter :integer ;
Begin
Writeln('Welcome to Cool Average App!');
Writeln('Please select your choices (1 -> 4):');
Writeln('1. Read in 20 numbers');
Writeln('2. Print out the numbers');
Writeln('3. Compute average');
Writeln('4. Quit');

14.

Readln(choice);

15.
16.
17.
18.
19.
20.
21.
22.
23.

Case choice of
1 : Begin
For counter := 1 to 20 do
Readln(numbers[counter]);
End;
2 : Begin
For counter := 1 to 20 do
Writeln(numbers[counter]);
End;

21. 3 : Begin
22.
sum := 0;
23.
For counter := 1 to 20 do
24.
Begin
25.
sum := sum + numbers[counter];
26.
Writeln(Current Sum is', sum);

27.
28.
29.
30.

End;
average := sum / 20;
Writeln('Average =', average);
End;

31. 4 : Begin
32.
halt;
33.
End;
34. readln;
35. End;
36. End.

21

Exercise.. Question 1
Ask user to input 10 values in an array. Use
these value:5, 3, 15, 0, 25, 6, 7, 93, 41, 10

Exercise.. Question 2
Based on Question 1, use for loops to display
all elements of the array.

Exercise.. Question 3
Based on Question 1, use for loops to display
all elements of the array in reverse order.

Exercise.. Question 4
Trace and write the output for the program
below.
var i: integer;
newarray: array [1..5] of integer;
begin
for i := 1 to 5 do
newarray[i] := i * 3;

for i := 1 to 5 do
writeln (newarray[i]);
end.

Exercise.. Question 5
Using this line:
var posneg : array [1..15] of integer;

Write a program that calculates the number of


negative and positive numbers in the array.
Users will enter the elements in the array.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.

program posnegative;
var posneg: array [1..30] of integer;
count_pos, count_neg, i: integer;
begin
writeln ('Enter the elements of the array: ');
for i:=0 to 30 do //loop to key in the number
readln (posneg[i]);
for i:=0 to 30 do
begin
if posneg[i] < 0 then //condition to check for negative number
count_neg := count_neg + 1
else //if number is not negative, it must be positive
count_pos := count_pos + 1;
end;
writeln ('There are ', count_neg, 'negative numbers in the array.');
writeln ('There are ', count_pos, 'positive numbers in the array. ');
readln();
end.
27

Exercise.. Question 6
You are required to write a program that will
check consistency of both the array values to
see whether they match. Get the input values
for both arrays from the user.

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.

program compare_array;
var x : array [1..5] of integer;
y : array [1..5] of integer;
status, i: integer;
begin
status := 1;
write ('Enter values of first array: ');
for i:= 1 to 5 do
read (x[i]);
write ('Enter values of second array: ');
for i:= 1 to 5 do
read (y[i]);
for i := 1 to 5 do
begin
if x[i]<>y[i] then
status := 0
end;
if status = 0 then
writeln ('There are not equal')
else
writeln ('There are equal');
readln();
end.
29

Exercise.. Question 7
Could we swap elements within array?
Could we swap elements between arrays?

30

1.
2.
3.
4.

program bubble_sort;
const MAX = 5;
Var i, j, temp : integer;
num : array[1..5] of integer;

5. procedure fill_array();
6. begin
7. // fill up the array
8. write('Enter five integers > ');
9. for i := 1 to MAX do
10. read(num[i]);
11. readln();
12.end;
13.procedure sort();
14.begin
15. // sort the array elements
16. for i := 1 to MAX do
17. for j := 1 to MAX - i do
18. if num[j] > num[j + 1] then
19. begin
20.
temp := num[j];
21.
num[j] := num[j + 1];
22.
num[j + 1] := temp;
23. end;
24.end;

26.procedure display();
27.begin
28. // display the array elements
29. write('Now the array contains :');
30. for i := 1 to MAX do
31. write(num[i]:5);
32. writeln();
33.end;
34.procedure exitprogram();
35.begin
36. writeln('Press any key to exit, TQ.');
37. readln();
38.end;
39.begin
40. fill_array();
41. display();
42. sort();
43. display();
44. exitprogram();
45.end.
31

Exercise.. Question 7

FIGURE Q2a and FIGURE Q2b are sample output screen for a program that declares three single-dimensional arrays named
University, Year2012 and Year2013. This program gets the input from user for Year2013 only. Universities name and number
of student for Year2012 are assigned in the program. These arrays will store value for five elements. Total student for
Year2012 and Year2013 are calculated and displayed in a table. If number of student for Year2013 is greater than Year2012
then appropriate message is displayed.

32

Sample Program 2
There are several pieces of information that
go together, for example
Staff IDs, Department and Salary
Student IDs, Courses and Grades

index value as the primary key

33

Sample Program
var
staffID : array[1..6] of integer;
salary

: array[1..6] of real;

staffID[1] 11011

salary[1] 1200

staffID[2] 11013

salary[2] 7500

staffID[3] 11015

salary[3] 8550

staffID[4] 11014

salary[4] 6300

staffID[5] 11016

salary[5] 4550

staffID[6] 11012

salary[6] 2500

34

program staff;
const
MAX = 6;//array upper bound
var
Who gets the
i,
// array index
highest
: integer; // index for highest salary
highest salary?
highest_salary : real;
staffID
: array[1..MAX] of integer;
salary
: array[1..MAX] of real;
begin
// fill in the array
for i := 1 to MAX do
begin
write('Enter staff ID <space> salary --> ');
readln(staffID[i], salary[i]);
end;
// find the highest salary and displays
// the staff ID of the person who gets the highest pay
highest_salary := salary[1]; //assign the first stored salary as highest salary
highest := 1;
for i := 2 to MAX do
begin
if salary[i] > highest_salary then
begin
highest_salary := salary[i];
highest := i;
// capture array index for highest salary
end;
end;
writeln(staffID[highest], ' gets the highest pay.'); // display staffID
write('Press any key to exit, TQ!');
35
readln();
end.

program staff;
const
MAX = 6;
List staff IDs with
var
i
: integer;
salary over
staffID
: array[1..MAX] of integer;
RM5000
salary
: array[1..MAX] of real;
begin
// fill in the array
for i := 1 to MAX do
begin
write('Enter staff ID <space> salary --> ');
readln(staffID[i], salary[i]);
end;
// find and display staff ID with salary over RM5000
writeln();
writeln('List of staff with salary over RM5000');
writeln('StaffID
Salary');
writeln('------------------');
for i := 1 to MAX do
begin
if salary[i] > 5000 then
begin
writeln(staffID[i], '(':5,salary[i]:0:2,')'); // display staff ID
end;
end;
writeln('------------------');
write('Press any key to exit, TQ!');
readln();
end.
36

Now, why dont you try?

display staff ID with


the lowest
salary

List staff IDs with


salary over
the average
salary

List staff IDs with


salary below
RM5000

37

38

39

40

Vous aimerez peut-être aussi