40
Chapter 7 One Dimensional Array

Chapter 7 Array

Embed Size (px)

DESCRIPTION

PASCAL programming

Citation preview

Page 1: Chapter 7 Array

Chapter 7One Dimensional Array

Page 2: Chapter 7 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

Page 3: Chapter 7 Array

3

Problem with Variable• A variable is a memory location with a name

var num : integer;

Memory address

Page 4: Chapter 7 Array

4

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.

Page 5: Chapter 7 Array

5

Problem with Variable

• What if the number of numbers to be dealt with is 20 instead of 3?

• What is the difference?

Program Reverse;Var num1, num2 ... num20 : Real;Begin Readln(num1, num2 ... num20); Writeln(num20, num19 ... num1);End.

Page 6: Chapter 7 Array

6

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.

Page 7: Chapter 7 Array

7

Array Declaration

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

Array name Can store up to 20 values

Data type

Page 8: Chapter 7 Array

8

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]

Page 9: Chapter 7 Array

• 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)

9

Array Declaration

Page 10: Chapter 7 Array

10

Sample Program 1// Input & output array elements

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

// Sum all of the array elements

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; writeln(‘Sum : ’, sum);end.

Page 11: Chapter 7 Array

11

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 numbers2. Print out the numbers3. Compute average.4. Quit.

Page 12: Chapter 7 Array

12

Write a program… Firstly…

• Declare the array

• Note that index starts with 1

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

Page 13: Chapter 7 Array

13

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.

Page 14: Chapter 7 Array

14

Write a program… Thirdly…

• Get the user’s 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.

Page 15: Chapter 7 Array

15

Write a program… Fourthly…

• Take action based on the user’s choice Readln(choice); Case choice of 1 : Begin End; 2 : Begin End; 3 : Begin End; 4 : Begin End; End;

Page 16: Chapter 7 Array

16

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;

Page 17: Chapter 7 Array

17

Write a program… Sixthly…

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

Page 18: Chapter 7 Array

18

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;

Page 19: Chapter 7 Array

19

Write a program… Finally…

• Quit! 4 : Begin

halt; End;

Page 20: Chapter 7 Array

20

Write a program… In the end…

• Now, combine all to get a full program!

Page 21: Chapter 7 Array

1. Program AverageWithMenu;2. Uses crt;//winCrt3. Var4. numbers: Array[1..20] of Real;5. sum, average: Real ;6. choice, counter :integer ;7. Begin8. Writeln('Welcome to Cool Average

App!');9. Writeln('Please select your choices (1

-> 4):');10. Writeln('1. Read in 20 numbers');11. Writeln('2. Print out the numbers');12. Writeln('3. Compute average');13. Writeln('4. Quit');

14. Readln(choice);

15. Case choice of16. 1 : Begin17. For counter := 1 to 20 do18. Readln(numbers[counter]);19. End;20. 2 : Begin21. For counter := 1 to 20 do22. Writeln(numbers[counter]);23. End;

21

21. 3 : Begin22. sum := 0;23. For counter := 1 to 20 do24. Begin25. sum := sum + numbers[counter];26. Writeln(‘Current Sum is', sum);

27. End;28. average := sum / 20;29. Writeln('Average =', average);30. End;

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

Page 22: Chapter 7 Array

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

Page 23: Chapter 7 Array

Exercise.. Question 2

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

Page 24: Chapter 7 Array

Exercise.. Question 3

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

Page 25: Chapter 7 Array

Exercise.. Question 4

• Trace and write the output for the program below.

var i: integer; newarray: array [1..5] of integer;

beginfor i := 1 to 5 do newarray[i] := i * 3;

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

end.

Page 26: Chapter 7 Array

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.

Page 27: Chapter 7 Array

1. program posnegative;2. 3. var posneg: array [1..30] of integer;4. count_pos, count_neg, i: integer;5. 6. begin7. writeln ('Enter the elements of the array: ');8. for i:=0 to 30 do //loop to key in the number9. readln (posneg[i]);10. for i:=0 to 30 do11. begin12. if posneg[i] < 0 then //condition to check for negative number13. count_neg := count_neg + 114. else //if number is not negative, it must be positive15. count_pos := count_pos + 1;16. end;17. writeln ('There are ', count_neg, 'negative numbers in the array.');18. writeln ('There are ', count_pos, 'positive numbers in the array. ');19. readln();20. end.

27

Page 28: Chapter 7 Array

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.

Page 29: Chapter 7 Array

1. program compare_array;2. 3. var x : array [1..5] of integer;4. y : array [1..5] of integer;5. status, i: integer;6. 7. begin8. status := 1;9. write ('Enter values of first array: ');10. for i:= 1 to 5 do11. read (x[i]);12. 13. write ('Enter values of second array: ');14. for i:= 1 to 5 do15. read (y[i]);16. 17. for i := 1 to 5 do18. begin19. if x[i]<>y[i] then20. status := 021. end;22. 23. if status = 0 then24. writeln ('There are not equal')25. else26. writeln ('There are equal');27. readln();28. end.

29

Page 30: Chapter 7 Array

Exercise.. Question 7

• Could we swap elements within array?• Could we swap elements between arrays?

30

Page 31: Chapter 7 Array

26.procedure display();27.begin28. // display the array elements29. write('Now the array contains :');30. for i := 1 to MAX do31. write(num[i]:5);32. writeln();33.end;

34.procedure exitprogram();35.begin36. writeln('Press any key to exit, TQ.');37. readln();38.end;

39.begin40. fill_array();41. display();42. sort();43. display();44. exitprogram();45.end.

31

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

5. procedure fill_array();6. begin7. // fill up the array8. write('Enter five integers > ');9. for i := 1 to MAX do10. read(num[i]);11. readln();12.end;

13.procedure sort();14.begin15. // sort the array elements16. for i := 1 to MAX do17. for j := 1 to MAX - i do18. if num[j] > num[j + 1] then19. begin20. temp := num[j];21. num[j] := num[j + 1];22. num[j + 1] := temp;23. end;24.end;

Page 32: Chapter 7 Array

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

Page 33: Chapter 7 Array

33

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

Page 34: Chapter 7 Array

34

Sample Program

staffID[1] 11011

staffID[2] 11013

staffID[3] 11015

staffID[4] 11014

staffID[5] 11016

staffID[6] 11012

salary[1] 1200

salary[2] 7500

salary[3] 8550

salary[4] 6300

salary[5] 4550

salary[6] 2500

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

Page 35: Chapter 7 Array

35

program staff;const MAX = 6;//array upper boundvar i, // array index highest : integer; // index for 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!'); readln();end.

Who gets the highest salary?

Page 36: Chapter 7 Array

36

program staff;const MAX = 6;var i : integer; 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 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.

List staff IDs with salary over

RM5000

Page 37: Chapter 7 Array

37

Now, why don’t you try?

List staff IDs with salary over the average

salary

display staff ID with the lowest

salary

List staff IDs with salary below

RM5000

Page 38: Chapter 7 Array

38

Page 39: Chapter 7 Array

39

Page 40: Chapter 7 Array

40