40 likes | 91 Views
Indexed Variables. Indexed variables ( arrays ) allow us to use many instances of a particular variable. This means we can specify algorithms which use many occurrences of identical variables easily.
E N D
Indexed Variables Indexed variables( arrays ) allow us to use many instances of a particular variable. This means we can specify algorithms which use many occurrences of identical variables easily. Consider an algorithm to record the number of occupants in a car which pass the university gates each day. Question: Can you specify beforehand the number of cars that will pass by ?
Indexed Variables To solve this problem we introduce the concept of an index. The index points to a particular instance of a variable which can have many occurrences and is enclosed in square brackets after the variable name e.g. car[1], car[2], car[3] Whenever we require a new instance of a variable we augment the variable index. With this concept algorithms can store unknown amounts of related data.
Indexed Variables Example {Algorithm to record number of car occupants. 0 to stop} index 1 { Always ensure index is initialised to 1} READ car[index] { Get value for car 1 } WHILE ( car[index] != 0 ) index index + 1 { Now get ready for next car } PRINT “Enter number of occupants” READ car[index] ENDWHILE