Arrays

Think Pair Share

We have the following task: 

  • Create a program to collect names and scores of a test in a class of 30 students
  • Find the highest score student and the average score
  • Displays the scores of all students and indicate whether he/she is above average or not

Discuss the solution

Arrays

  • For storing series of value of the same type
  • Each value is called an array element
  • Each element has an "address" to access it which is called array index
  • Usually array size is fixed (i.e. Cannot resize after it is declared)
#Pseudocode
#Declare array of size 10
Array_Name[1:10]

#Put value 10 in the first
#position
Array_Name[1] <- 10

#Access value in first pos
PRINT Array_Name[1]
#Python
#Initialize array of size 10 and zero all elements
ArrName = [0 for i in range(10)]

#Access item in the first position
#Note Python array index starts with 0
ArrName[0] = 10
print(ArrName[0])    #Access the value in zero

Class practice (ArrayExercise1)

  • Create an array called Names of size 5
  • Ask the user to input names and store them in the array (hint: use for loop)
  • Print all the names afterwards
Enter student name 1: Jimmy
Enter student name 2: Amy
...

Students:
Jimmy
Amy
...

Class practice 

  • Continue from the previous exercise
  • Add one more array (Scores) which is also size of 5
  • After each input name, input score as well
  • Print all corresponding names and the scores at last
Enter student name: Jimmy
Enter Score of Jimmy: 45
...

Results:
Jimmy  45
Amy    86
...

Class practice 

  • Continue from the previous exercise
  • At the end of the program, display:
    • Student who got the highest score
    • Average score of all students
...
Results:
Jimmy: 45
Amy: 86
...

Amy, who got 86 points, is the highest
Class average score is 72

Final step

Display whether the student is above average or not

...
Results:
Jimmy  45   below average
Amy    86   above average
...

Amy, who got 86 points, is the highest
Class average score is 72

Array Exam style questions

The class teacher need a program to collect  the height of all students. Name is not required. The data will store in an array and later use it for statistical calculations. 

1. A constant is needed for total number in school. Suggest the name of the constant. 

 

2. Suggest the name of the array for storing the data. 

 

3. Write the algorithm to collect the data for the teacher. (either pseudocode, flowchart, or python)

 

4. The teacher need to know the average height of students. Write the algorithm that display the average height (assuming that part 3 is already done)

Answer to the exam style questions

Answer is just for reference - any reasonable answer is acceptable

1. A constant is needed for total number in school. Suggest the name of the constant.

StudentCount

2. Suggest the name of the array for storing the data. 

StudentHeight

3. Collect data

Note: Constant StudentCount is supposed to be pre-defined (i.e. should not have input)

#Declare array, index from 1
StudentHeight[1:StudentCount]
FOR i <- 1 to StudentCount
  INPUT StudentHeight[i]
NEXT i
#This example is zero indexed, similar to python
StudentHeight[StudentCount]
FOR i <- 0 to StudentCount-1
  INPUT StudentHeight[i]
NEXT i
Total <- 0
FOR i <- 1 to StudentCount
  Total <- Total + StudentHeight[i]
NEXT i
Average <- Total / StudentCount
PRINT Average

Again, you can use zero based array...

Made with Slides.com