8 Arrays and Important Algorithms 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 an array?

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

Write an algorithm that searches through an array of integers named nums to find the highest value and the average of all of the numbers stored. The array stores 100 numbers.

Your program should then output the highest value and average. You can assume all variables have been declared already

A
total ← 0
highest ← -999999
average ← 0

FOR i ← 1 TO 100 STEP 1
    total ← total + nums[i]
 
    IF nums[i] > highest THEN
        highest ← nums[i]
    ENDIF
NEXT i
 
average ← total / 100

OUTPUT "Average", average
OUTPUT "Highest", highest
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write an algorithm that asks the user to enter 10 names and stores them into an array called names.

It should then output the names in reverse order

A
DECLARE names : ARRAY[1:10] OF STRING
 
OUTPUT “Enter 10 names:”

// Store each name input into the array
FOR i ← 1 TO 10 STEP 1
    INPUT names[i]
NEXT i

// Loop through the array and output the names in reverse 
FOR i ← 10 TO 1 STEP -1
    OUTPUT reverse[i], " "
NEXT i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Declare an array to store 30 names

A

DECLARE names : ARRAY[1:30] OF STRING

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

Declare an array to store 100 ages

A

DECLARE ages : ARRAY[1:100] OF INTEGER

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

Declare a 2D array (called results) that has 10 rows and 8 columns of whole numbers

A

DECLARE results : ARRAY[1:10, 1:8] OF INTEGER

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

A 2D array (results) has been used to store the exam results for 10 students. Each row is a set of student results. 8 results are stored for each student (each column stores the subject result). There is also an that array stores the student names for each row of results in order.

Write an algorithm in pseudocode to output the results for each student.

DECLARE results : ARRAY[1:10, 1:8] OF INTEGER
DECLARE names : ARRAY[1:10] OF STRING

A
FOR studentCount <- 1 TO 10 STEP 1
    OUTPUT names[studentCount]
		
	FOR subjectCount <- 1 TO 8 STEP 1
	    OUTPUT results[studentCount, subjectCount]
	NEXT i
NEXT i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write an algorithm that allows the user to input an integer. It should then search through an array called AList and output the position of the entered number in the array. If the value entered is not in the array it should output “Item not found”

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

Write a bubble sort algorithm that sorts an array of integers called AList into ascending order.

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