8 Arrays and Important Algorithms Flashcards
What is an array?
- 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
What are the advantages of an array?
+ 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
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
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
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
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
Declare an array to store 30 names
DECLARE names : ARRAY[1:30] OF STRING
Declare an array to store 100 ages
DECLARE ages : ARRAY[1:100] OF INTEGER
Declare a 2D array (called results) that has 10 rows and 8 columns of whole numbers
DECLARE results : ARRAY[1:10, 1:8] OF INTEGER
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
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
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”
Write a bubble sort algorithm that sorts an array of integers called AList into ascending order.