Computer Science
IB
For this section I'm relying heavily in this sources:
https://www.programiz.com/cpp-programming/arrays
https://www.w3schools.com/cpp/cpp_arrays.asp
Arrays are a particular type of collection. The idea is that we are going to store in one variable more than one value. One way to do it is using an array.
This is the declaration of an array of integers (int) with 5 numbers:
The length (or size) of a specific array is how many elements can contain.
Arrays in most programming languages are very efficient (specially regarding memory usage) but they have some inconvenients. The main one is that once we have declared the size of an array it cannot be changed during the execution.
The first problem that we need to address is that we want to access a particular element (also called member) of the array. To do that we need to use the concept of index. The index is the address inside of the array. To access one of the elements we need to write the name of the array and, in brackets write the number of the index.
Indexes in computer science start with zero, so the first element is the [0] element, the second is the [1] element and the last is [n-1] being n the length of the array.
Let's supose the example of the web in programiz. In C++ we can initialize the array like this:
Here is the visualization of the elements
So if we write this code we can use those members to asign other values or we can use it in if statements as we please.
For example
You may see that sometimes we are using MyArray[100] and other times is MyArray[0]. Why is that? Is not the size of the array constant?
When we define the array in many programming languages we can define the array size in the same line. For example:
Write the output of this code that it's going to happen through serial.
This and the next is a typical exam exercise to see if you get the concept of index of arrays
The solution with the explanation in the comments is in the spoiler section
Solution:
The answer in this case are separated through lines since we're using Serial.println but in an exam I will accept them separated in other ways as long as they are in the correct order.
Write the output of this code that it's going to happen through serial.
In this case we are going to use also values as indexes. Beware!
The solution with the explanation in the comments is in the spoiler section
Solution:
The answer in this case are separated through lines since we're using Serial.println but in an exam I will accept them separated in other ways as long as they are in the correct order.
As a teacher I observed that the concept of length of an array sometimes is not clear and is key for understanding the loops through arrays.
The length of the array will be usually given in a variable or as part of the problem "we have a hotel with 100 rooms, and each number of the room is stored in an array" means that we have an array of length 100.
In C++ we cannot use length of an array as propiety so it's common to have something like this
This calculates the size in memory of the whole array and divides it by the size of the first element. This gives the numbers of elements.
Sometimes we have a more complex version of this. In the code that we used in the melody player we found this code in the line 172
This was because in this code the melody array has 2 elements for each note (frequency and the divider of the duration).
More info here
In other programming languages (such as java) we usually can access the length of an array using a length propiety.
In class we have discussed the implementation of simple algorithms such as min, max and average. For doing that we need to use loops (for loops or while loops).
This types of algorithms are called iterate through an array.
Usually we can see 2 types of iteration algorithms (but there are more!).
In the for loop usually will be something like this:
Is common to use i of index, but this is usually up to the programmer and I will consider correct any name for the variable.
The ARRAY_LENGTH is the array length given by the context. Consult the length problem to revise if you need.
In the code usually we want to do something with the element of the array. If the array is called "myArray" this would be "myArray[i]".
A selective loop would look like this
The condition usually will look for a condition of the specific element so we can expect to see in the condition "myArray[i]".
We can use for loops, while loops and for each loops. I haven't covered for each loops but I will mark it as correct (if they are correctly written). You can find more info about for each loops in this LINK
Now that we have discussed the abstraction we can see the basic algorithms.
Now we're running a foodtruck about selling corn.
We have an array with all the prices of our products (23 different ways of having a cup of corn with stuff on it) with a range that goes from 2 euros to 10 euros.
Output the minimum, the maximum and the average of the prices that are available.
The solutions with the explanation in the comments is in the spoiler section
This exercise is not an exam like question but the student should know how these works in order to create more complex algorithms
Maximum
Minimum
Average
We have an array of 50 different grades from students that go from 0.0 to 10.
We want to know the index of the highest array and the number of passes (they pass with a 5.0 or higher grade).
Construct a code that outputs this information. The name of the array is grades.
The solutions with the explanation in the comments is in the spoiler section.
This exercise is an exam like question.
We can do it in just one loop or two different loops. Both options are fine for this purpose.
Finding the maximum index with pseudocode
Output the number of passes
Solution with only one loop (a bit more confusing but legal for the exam)
This exercise is the same as 18 but is asking about construting functions. Take notice of the differences in the statement
We have an array of 50 different grades from students that go from 0.0 to 10.
We want to know the index of the highest array and the number of passes (they pass with a 5.0 or higher grade).
We're going to implement a couple of functions that return this information. The first one is "highestGradeIndex" and the second is "passes". The name of the array is grades and is a double type and is a global variable.
Remember that global variables are variables that we can access in any part of the code.
We can consider that the instruction Serial.begin(9600); has been already called.
The solutions with the explanation in the comments is in the spoiler section.
We need to split this into two different functions, we cannot do them in just one algorithm.
The highestGradeIndex function:
Here you have more activities to do to practice (with pseudocode and C++)
Strings are arrays of characters. We have access to several functions that are going to do something
When we have 2 or more arrays which elements of the same index refer to the same entity (a student, a car, an employee, a flight) we call them Parallel arrays.
We usually have to collect information from one of the arrays and output the other one.
Classic example is to store in one array the grades of the students and in other the names of the students. So, if you want to output who got the best (or worst) grade you will check the array of the grades but you will use the same index to output the name of the student using the array which has the names of the students
Number of followers of EXO.
We're the managers of EXO and we want to check the performance of these members in social media.
We have 2 arrays. One array is EXObandMember with the names of the band members ( Baekhyun, Chen, Lay, Sehun, Chanyeol, D.O., Kai, Suho, Xiumin) and the other is a parallel array called followersInstagram
Create in pseudocode an algorithm that outputs who has the most number of followers in instagram and the number of follows that they have.
The solutions with the explanation in the comments is in the spoiler section.
Result only:
Process
In the same context of the last exercise we need to know the member that is nearest the average amount of followers in instagram.
For this we need 2 loops. First to find the average. Second to decide which band member has a lower difference with the average.
The solutions with the explanation in the comments is in the spoiler section.
Using the same context of EXO members, output the name of all the members that are over 10 000 000 followers in instagram.
(solution by Y.C)
Flag variables are boolean variables that we use to store information.
Flights destinations.
We have in an array called flightDestinations[4500] the destination code for all the flights that had happen in one month in Barcelona's Airport. This destination codes are 3 letters that are unique for each airport ("IBZ" for Ibiza, "SCL" Santiago de Chile, "EZE" for Buenos Aires, "MAD" for Madrid and so on). These flights can have the same destination so they will be repeated. For example we can expect that IBZ could be even repeated 100 times in a month.
1st part. Output how many times there is a flight to ICN (Seoul)
We have another array that is called SpanishDestinations[] with the codes of the Spanish airports. There are 48 airports in Spain.
The arrays SpanishDestinations and flightDestinations are not parallel (they don't refer to the same flight)
First we're going to check if the first flight is a National flight or an international flight. To do that we need to check if the String is inside Spanish Destinations.
So let's start simple and let's see if the 3rd flight of the month(index number 2, remember) is a National or international flight. Output "national" if it is, "international" otherwise
Now let's output how many national flights have we had in Barcelona last month.
In this case we are going to need that flag variable and we're not looking for only one of the flights in flightDestinations but all of them. So we need to loop through all of them. We're going to need a nested loop (a loop inside a loop)
Change the previous code so it displays the international destinations instead of the national ones.
Solution in the spoilers:
Three posible solutions
This solution is not correct because it doesn't evaluate properly
Output the percentage of flights that were international last month. (Small variation from the last exercise)
Solution:
credits to S.A.
Output how many different locations are there in the array flightDestinations.
Let's trace this table for this 6 values of flightDestinations
{MAD, IBZ, MAD, SCL, EZE, IBZ}
For being more understandable we are going to write the line of the code that we are. In an exam you are not going to be asked to do that.
line | COUNT | N | flightDestinations[N] | X | differentLocations [X] | newDestination |
---|---|---|---|---|---|---|
5 | 1 | |||||
7 | 1 | 1 | IBZ | |||
8 | 1 | 1 | IBZ | true | ||
8 | 1 | 1 | IBZ | 0 | MAD | true |
10 | 1 | 1 | IBZ | 0 | MAD | true |
15 | 1 | 1 | IBZ | / | / | true |
16 | 1 | 1 | IBZ | / | / | true |
8 | 2 | 2 | MAD | / | / | true |
9 | 2 | 2 | MAD | 0 | MAD | true |
10 | 2 | 2 | MAD | 0 | MAD | true |
Do a trace table with this data in Flight Destinations
{MAD, CDG, MAD, SCL, CDG, IBZ}
COUNT | N | flightDestinations[N] | X | differentLocations [X] | newDestination |
---|---|---|---|---|---|
- - | –––– | –––– | –––– | –––– |
Output which Spanish airport has had the most number of flights from Barcelona last month.
In this case we have these arrays
flightDestinations[4500]
SpanishDestinations[48]
So for doing that we are going to need another array (SpanishCounters) to store the counts of each of the 48 Spanish airports. After that we just have to find the maximum and output the name using the code stored in SpanishDestinations
Tell (output) which destination is the most popular in the last month
For doing that we need the array with diffent possible destinations. If it's not given, we can do it with the previous code.
After this, then we need to populate an array.
We selling lemonade and other juices for having some extra fundings. To analyze the different sales we have filled an array with all the names of the different products PRODUCTS with these values [“Lemonade”, “Orange juice”, “Orange banana”, “Orange carrot”, “Tropical smash”] and we have another parallel array with their corresponding prices called PRICES with values that go from 500 to 3000 Chilean Pesos.
A.1) State the size of the array PRICES [1 mark]
5
Last weekend we have been taking notes of the sales and we have an array called SALES with the 80 sales with the name of the different products we have sold. Here you can see the first values of the array SALES:
{“Orange juice”, “Orange juice”, “Lemonade”, “Orange carrot”, “Lemonade”, “Orange juice”, …}
All the possible values of SALES are inside PRODUCTS.
A.2) Construct a code that outputs the number of times that we have sold lemonade last weekend. [4 marks]
Marking: 1 for correct loop, 1 for output, 1 for updating the variable, 1 for correct if
A.3) Construct a code that outputs the total amount of income in chilean pesos this weekend. Use the parallel array PRICES. [6 marks]
The solution is at the end of the explanation.
For doing this we need to understand that each sale has associated with one name (in products) and each product has a price.
To find the total amount of income we need to add the prices of all the elements that we have sold.
So first we need a loop through the array of SALES (that one has a length of 80).
For each element we need to loop through the array of PRODUCTS (that one has a length of 5) to find which index is it.
Once we know the index of the PRODUCT then we can add to SUM the price of the product since PRICES is a parallel array of PRODUCTS.
Marking: 1 for correct each loop, 1 for output, 1 for updating the variable, 1 for correct if, 1 for using the parallel array
(inspired by Nov 22 exercise)
Consider this one-dimensional array COUNTRIES
[0] | China |
[1] | Tailand |
[2] | Australia |
[3] | Egypt |
[4] | Denmark |
Construct a trace table for the following algorithm
Solution:
T | T > = 0 | B | Output |
---|---|---|---|
4 | true | 0 | China |
3 | true | 3 | Egypt |
2 | true | 2 | Australia |
1 | true | 1 | Tailand |
0 | true | 0 | China |
-1 | false | 0 | - |
(I changed the algorithm)
Consider this one-dimensional array COUNTRIES
[0] | China |
[1] | Tailand |
[2] | Australia |
[3] | Egypt |
[4] | Denmark |
Construct a trace table for the following algorithm
Solution
credit to Y.C.
(I changed the algorithm)
Consider this one-dimensional array COUNTRIES
[0] | China |
[1] | Tailand |
[2] | Australia |
[3] | Egypt |
[4] | Denmark |
Construct a trace table for the following algorithm
(credit: R.G. with modifications)
There is a calculation that you can use to find the Goal Assist that is a ratio that ponders a little bit more the Goal than the assist (we don't need to know the details). To find out you need to use the function calcGA(Goals, Assist) that if you send the number of goals and the number of assist it will send you back the info that ratio.
We have 3 parallel arrays that refer to 30 players, with NAMES, GOALS and ASSIST that they have done over a season.
Construct an algorithm which will output the names of all the football players whose GA (Goal/assist)is greater than this group's average GA.
Be aware that you should use "calcGA()" in your answer
Solution in the spoiler
sum = 0
loop for K from 0 to 29
sum = sum + calcGA(GOALS[K], ASSIST[K])
end loop
average = sum/30
loop for K from 0 to 29
if calcGA (GOALS[K], ASSISTS[K]) > average then
output NAME[K]
end if
end loop
In the first part of this code (1-5 line) we are counting group's average GA, using "for" and then dividing sum to 30 (30 people).
In the second part we are already writing the code itself, wich calculates how many people have GA greater than the group's average
Starts with the first element and compares each elemen to the one we're looking for until it is found.
Works on every type of array, no matter if it's sorted and it has a O(n) complexity.
With collections work this way:
Example 1:
We have a couple of parallel arrays with a size 20. One that is called NAMES and the other is called TIMES_THAT_HAVE_GONE_TO_SKI
If we want to know if somebody has gone to ski exactly 3 times we can use a linear search.
Construct the pseudocode to output the name of somebody who has been skiing exactly 3 times
Also known as "half-interval search". This is a good algorithm (O(log (n)) of complexity) but requires that the array has to be sorted before executing it.
So the steps are
Compare the search value with the element of the middle of the array. The posibilities are 3.
Here you have an image of how it works
source geeksforgeeks
Beware: this array doesn't work! find out why using a trace table!
Implementation transalated from here:
https://www.geeksforgeeks.org/binary-search/
Remember that LOW, MID and TOP are refering to indexes and not about values.
The index of the Searched value will be stored in the MID variable in this implementation.
Example:
We have a dictionary of slang words used in the 70s in London in a corpus that has 1000 words. All those words are sorted alphabetically. The name of the array is WORDS_CORPUS
Corpus linguistics is the study of a language as that language is expressed in its text corpus (plural corpora), its body of "real world" text. More info here
Construct a pseudocode algorithm using binary search to try to find if "punk" is in that dictionary. It will output if it's or not.
In most programming languages you can compare words in English with usual comparators and it will work fine. For not English languages things are usually tricker.
In this way 'A' is "smaller" than 'B' and 'D' is "bigger" than 'C'
Also "Aaron" will be evaluated as smaller if you compare it with "Berthe" and "Sofya" will be bigger than "Chenxi"
Solution in the spoiler section.
We have the following array:
TIMESTAMPS = {1002, 1003, 1023, 1240, 1560, 1850, 1950}
Write a tracetable using a binary search algorithm. One of the colums should be output. The searched value is 1560
Possible solution
credit to Y.C.
Do the same with the searched values of 900, 1003 and 1951
Because if we have the elements sorted, we can use binary search, so everything is way more easy to find.
(source)
Imagine that we have a dictionary without sorting, it would be very complicated to use!
For sorting we need to one way or another swap elements between them. For doing that the main way is with this template.
Remember that MY_ARRAY depends on context and it's the name of an array, and N and M are variables that can vary on context or be dependent one to the other.
In python
There are more sorting methods but IB wants you to know these:
Bubble sort works by comparing pairs of elements, if they match they swap, if not, we continue looking.
More info on that here:
https://www.geeksforgeeks.org/bubble-sort/
Citing them:
The complexity is so is not good. But is easy to implement.
Generic implementation in IB pseudocode
Remember that we are looking into pairs of elements. So, if we're looking to the element J and the next, J+1, the last element index is J+1. The last index is X-1 so the maximum value of J is X -2
Using math:
More info here:
https://www.geeksforgeeks.org/selection-sort/
(similar to Nov 2018)
We have 2 parallel arrays: FIRSTNAMES [6000] and LASTNAMES [6000]. They hold the information of the last 6000 people that want a ticket to go to a concert of Coldplay. Since we want to use binary search to get the information of these people, we need to sort these arrays.
(source I think that 6000 is maybe too few)
Construct in pseudocode an algorithm that sort these arrays by last name using bubble sort (A not very good idea but you have to follow orders). Take into account that you will also need to change the first names so the arrays keep being parallel.
Credit Y.C.
Credit L.C. (with edit)
Construct in pseudocode an algorithm that sort these arrays by last name using selection sort (A not very good idea but you have to follow orders). Take into account that you will also need to change the first names so the arrays keep being parallel.
Credit A.P. (with edit)
We have an array of the grades of students. There are 2 parallel arrays, one is NAMES and the othere is GRADES (50 students). The grades are integers that go from 1 to 7 (IB style)
First we want to order the Students so the students with the best grades are in the first part of the array and the last are the ones with lower grades.
Construct an algorithm that using selection sort sort both arrays.
Construct an algorithm that using bubble sort sort both arrays.
This is just for practice. I don't recall any problem with 2 levels of order in an IB exam.
In the same context (NAMES, GRADES, 50 elements)
Now we want to order not only by grades (that we have from 1 to 7 but we want also a sub-order that is going to be by name) so if there are 2 students with the same grade, the first in the alphabetical order will be displayed first.
For this one we are going to order it by parts. We need to detect if they are the same and them use a function to sort the sub-arrays.