Arrays #
This page contains information and coding exercises for arrays
.
💻 Open up the pseudocode compiler in a new tab. You will be using this website to complete the exercises below.
Examples of how to use Arrays #
Creating Arrays #
// new array without data
MYSTUFF = []
// new array with data
COLORS = ["aqua","crimson","mauve","rust","taupe","jade","navy", "lemon"]
Adding to Arrays #
// make empty array
EVEN_NUMS = []
// loop to create each index
loop I from 0 to 9
// add to array
EVEN_NUMS[I] = I*2
end loop
Accessing Elements of an Array #
// Example of accessing elements of an array
MONTHS = ["January","February","March","April","May","June","July", "August", "September","October","November","December"]
output MONTHS[0]
output MONTHS[4]
output MONTHS[12]
Output:
January
May
undefined
Looping through an Array #
// Using an auto loop with an array
MONTHS = ["January","February","March","April","May","June","July", "August", "September","October","November","December"]
loop INDEX from 0 to MONTHS.length-1
output MONTHS[INDEX]
end loop
Practice Exercises #
Exercise 1: Searching #
💻 Search through an Array to find the user input.- Create an array with 10 numbers
- Get user input
- Check whether the user input is in the array
Here are two example interactions for this array:
NUMS = [20, -1, 9, 4, 9, 30]
Input a number
>>> 3
Not in the array
Input a number
>>> 9
In the array
Exercise 2: Sorted/Unsorted #
💻 Check whether an array is sorted.- Create an array with 10 numbers
- Output whether user array is sorted or not
Here are two arrays you can use to test your code:
UNSORTED = [32,43,55,31,29,45,46,55,60,99]
SORTED = [19,21,25,27,30,35,37,41,42,50]
Exercise 3: Movie Ratings #
💻 Categorize the movies into arrays based on their ratings.- Create 3 empty arrays: BAD, GOOD, and MID
- Get the user to input the title of a movie and 1-5 star rating for the movie
- Get the user to input a 1-5 star rating for the movie
- Add the movie title to one of the arrays, based on its rating
- After the user has added 5 movies, print out the arrays
Here is an example interaction:
What is the title of the movie?
>>> Aladdin
What is your rating? (1-5)
>>>3
What is the title of the movie?
>>> Winnie the Pooh
What is your rating? (1-5)
>>>3
What is the title of the movie?
>>> Oppenheimer
What is your rating? (1-5)
>>>5
What is the title of the movie?
>>> Barbie
What is your rating? (1-5)
>>>5
What is the title of the movie?
>>> Morbius
What is your rating? (1-5)
>>>1
>
---BAD movies---
Morbius
---MID movies---
Aladdin,Winnie the Pooh
---GOOD movies---
Oppenheimer,Barbie
>