8.2.1 Data Structures (1D Arrays) Flashcards

1
Q

What is an array?

A
  • Collection of variables of the same data type
  • Referred to by a single name (identifier)
  • Each element is accessed by an index (position)
    Often called a data structure
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the advantages of arrays?

A

+ can store multiple values under a single identifier
+ reduces the number of variables
+ can use iteration to loop through an array
+ allows for more efficient programming

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Basic Array in Pseudocode

A

INTEGER nums[4]

nums[0] ← 10
nums[1] ← 20
nums[2] ← 30
nums[3] ← 40

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Reading and Writing to Arrays

For loops are perfect for iterating through arrays using the counter variable as the index.

Show an Example in Pseudocode

A

STRING names[10]

OUTPUT “Enter 10 names: “

//store input in each element of the array
FOR i ← 0 TO 9 STEP 1
INPUT names[i]
NEXT i

or

//loop though each element of array and output
FOR i ← 0 TO 9 STEP 1
OUTPUT names[i]
NEXT i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Searching Arrays

Exam questions often ask you to search arrays to find the largest value or to perform some calculation. The example below calculates the average of an array of 100 numbers and finds the highest value

Show an Example in Pseudocode

A

//store input in each element of the array
FOR i ← 0 TO 100 STEP 1
total ← total + nums[i]

IF nums[i] > highest THEN
    highest ← nums[i]
ENDIF NEXT i

//calculate average
average ← total / 100

How well did you know this?
1
Not at all
2
3
4
5
Perfectly