Try   HackMD

2 dimensional arrays

Loop through an 2D array.

We have to use a nested loop. That is a loop inside a loop.

Example Exam scores:

If we have a 2dArray SCORES[6][5] it means that we have 6 rows and 5 columns. So the total number of elements will be 30. Usually in this cases the rows are the students and the columns the scores. So we have 6 students with 5 scores each.

Output all the scores from the students. We have another array with names[6] where the name of the student is.

(This is similar to page 7 in advance book)

PSEUDOCODE SCORES[6][5] //We don't have to write this since is a given. Usually is easier for the student to write it so they have it clear. STUDENTS[6] //the linear array with the students loop STUDENT from 0 to 5 // the number of students -1 because we're using indexes output "The student " + STUDENTS[STUDENT] + "has this scores: " loop EXAM from 0 to 4 // the number of exams -1 output "Exam number " + (EXAM +1) output "Score: " + SCORES[STUDENT] [EXAM] end loop end loop

The output will be something like this

The student ALICE has a this scores: Exam number 1 Score: 5 Exam number 2 Score: 4 Exam number 3 Score: 3 Exam number 4 Score: 6 Exam number 5 Score: 3 The student ALVARO has a this scores: Exam number 1 Score: 6 Exam number 2 Score: 7 Exam number 3 Score: 4 Exam number 4 Score: 6 Exam number 5 Score: 3 ... And so on

Print the grades that are better than a 5 and who got them.

PSEUDOCODE SCORES[25][5] //We don't have to write this since is a given. Usually is easier for the student to write it so they have it clear. STUDENTS[25] //the linear array with the students loop STUDENT from 0 to 24 // the number of students -1 because we're using indexes loop EXAM from 0 to 4 // the number of exams -1 if (SCORES[STUDENT][EXAM] > 5) then output STUDENTS[STUDENT] output SCORES[STUDENT][EXAM] end if end loop end loop

The output will be something like this

ALICE 6 ALVARO 6 ALVARO 6 ALVARO 7 BOB 6 BOB 6 GERTRUDE 6 ABIGAIL 7 ABIGAIL 6 ..

Print the grades that are better than a 5 and who got them and which exam was it (exams go from 1 to 5)

Print the average of the grades of each student.

PSEUDOCODE SCORES[25][5] //We don't have to write this since is a given. Usually is easier for the student to write it so they have it clear. STUDENTS[25] //the linear array with the students loop STUDENT from 0 to 24 // the number of students -1 because we're using indexes output "Student " output STUDENTS[STUDENT] sum = 0 loop EXAM from 0 to 4 // the number of exams -1 sum = sum + SCORE[STUDENT][EXAM] end loop average = sum div 5 // the number of exams output " has this average: " output average end loop

Output the average of the exams

PSEUDOCODE SCORES[25][5] //We don't have to write this since is a given. Usually is easier for the student to write it so they have it clear. STUDENTS[25] //the linear array with the students loop EXAM from 0 to 4 //exams -1 output "the exam number" output (exam+1) output "has this average: " sum = 0 loop STUDENT from 0 to 24 sum = sum + SCORES[STUDENT][EXAM] //don't switch the order of access in the indexes! end loop average = sum / 25 //number of students output average end loop

Another example with students. We have an array of 30 students which names are stored in the linear array NAMES[]. They have done 6 exams and their grades are with percentage (So they go from 0 to 100). We want to print who did better in each exam (we can consider no ties).

The grades of the exams are stored in a 2D array called SCORES[30][6]

PSEUDOCODE SCORES[30][6] //We don't have to write this since is a given. Usually is easier for the student to write it so they have it clear. NAMES[30]] //the linear array with the students loop EXAM from 0 to 5 //Exams - 1 bestStudentIndex = 0 //start with 0 max = SCORE[0][EXAM] //first row of each column loop S from 0 to 29 // also from 1 to 29 because we are using the first value already if (max< SCORE[S][EXAM]) then max = SCORE[S][EXAM] bestStudentIndex = S end if end loop output "the best student of the exam" output (EXAM+1) output "was " output NAMES[bestStudentIndex] end loop

Other example

We're EXO manager (the group) and we have to divide the payment (this is fiction) to each person in the group using the time that they are singing in each Concert. They are very similar but they want to be precise.

The tour was called EXODUS and had 44 concerts. (that's kind of a lot)

The income that they have to split is saved in the array CONCERTINCOMES[44] that is splited in the different concerts.

The time that they have singed each singer in the performance is saved in a 2D array SINGINGTIME [9][44], being 9 the performers and 44 the concerts.

Create a code that calculates what is the total of each performer of EXO.

for each concert we need to make the splits between the 9
then we are going to add them.
finally we output

In this case we need to define a new array and then output the elements of the array.

PSEUDOCODE CONCERTINCOMES[44] SINGINGTIME[9][44] // new arrays ARTISTINCOMES[9] loop for Artist from 0 to 8 loop for C from 0 to 43 concertSingingTimeTotal = 0 loop for A from 0 to 8 concertSingingTimeTotal = concertSingingTimeTotal + SINGINGTIME[A][C] end loop proportion = SINGINGTIME[Artist][C] div concertSingingTimeTotal ARTISTINCOMES[Artist] = ARTISTINCOMES[Artist] + (CONCERTINCOMES[C] div proportion) end loop end loop loop for A from 0 to 8 output ARTISTINCOMES[A] end loop

Exercise about expenses in a year

We're a bank and our top directives have to declare expenses in their travels. We're using a 2D array to track this, being the rows the different executive and the columns the different months of the year. Expenses are in Euros.

This array looks like this

expenses[6][12] = {{130, 1500, 230 , 0, 0, 0, 2030, 120, 23, 0, 0, 0}{1500, 0,0,0, 200, 300,150, } }

Construct algorithms that find things and output them. Each sentence is an algorithm.

  • Find out the total of expenses in the whole year.
  • Find out what is the month with the most expenses in the year.
  • Find out what is the average of spending of the most spending executive.
  • Some of the expenses in some months are 0, this means that that month the executive didn't spend anything. Find which is the executive that has the most months without spending an euro.
  • These months start with january and end with december. Each trimester contains 3 months. (january, february, march, then april, may, june and so on). Find which is the trimester with less spending.

Total expenses

For this we only need to add all of the expenses

SUM = 0
loop for DIRECTIVE from 0 to 5
    loop for MONTH from 0 to 11
        sum = sum + EXPENSES [DIRECTIVE] [MONTH]
    end loop
end loop
output SUM 

Month with most expenses

For this we are going to find out the expenses of each month and compare the to a maximum. We need also to store the index of the maximum because that's the month name

Here we need to loop for each month and compare if their current sum is bigger than the others. Is like a case or finding "MAX_INDEX" but with 2D arrays.

MOST_SUM = 0
MOST_MONTH = 0
loop for MONTH from 0 to 11
    CURRENT_SUM = 0
    loop for DIRECTIVE from 0 to 5
        CURRENT_SUM = CURRENT_SUM + EXPENSES [DIRECTIVE] [MONTH]
    end loop
    if (CURRENT_SUM > MOST_SUM) then
        MOST_SUM = CURRENT_SUM
        MOST_MONTH = MONTH
    end if
end loop
output MOST_MONTH

About the CURRENT_SUM

CURRENT_SUM is set and re-set inside the loop of months because is not a cummulative sume but each month needs a different sum, so we store it separately. If we write this:

MOST_SUM = 0
MOST_MONTH = 0
CURRENT_SUM = 0
loop for MONTH from 0 to 11
    
    loop for DIRECTIVE from 0 to 5
        CURRENT_SUM = CURRENT_SUM + EXPENSES [DIRECTIVE] [MONTH]
    end loop
    if (CURRENT_SUM > MOST_SUM) then
        MOST_SUM = CURRENT_SUM
        MOST_MONTH = MONTH
    end if
end loop
output MOST_MONTH

In this case the MOST_MONTH will be always the last because in the second month CURRENT_SUM will be the sum of all the expenses of the MONTH and the previous one.

So as a rule of thumb, if we have to reset for each loop, the VARIABLE=0 goes inside. If it doesn't goes outside.

:::

Output average of the most spending directive

Find out what is the average of spending of the most spending directive.

First we have to find the most spending directive. Then output it's average. We can do it by parts and we can store averages and/or sums or not.

A solution without any new arrays

MOST_SUM = 0
MOST_DIRECTIVE = 0
loop for DIRECTIVE from 0 to 5
    CURRENT_SUM = 0
    loop for MONTH from 0 to 11
        CURRENT_SUM = CURRENT_SUM + EXPENSES [DIRECTIVE] [MONTH]
    end loop
    if (CURRENT_SUM > MOST_SUM) then
        MOST_SUM = CURRENT_SUM
        MOST_DIRECTIVE = DIRECTIVE
    end if
end loop
output MOST_SUM div 12 //number of months

Solution with a new array:

DIRECTIVE_SUMS [6] // new array to store SUMS of each directive
loop for DIRECTIVE from 0 to 5
    CURRENT_SUM = 0
    loop for MONTH from 0 to 11
        CURRENT_SUM = CURRENT_SUM + EXPENSES [DIRECTIVE] [MONTH]
    end loop
    DIRECTIVES_SUMS[DIRECTIVE] = CURRENT_SUM
end loop
//now we have to find the maximum in DIRECTIVES_SUM
MAX_DIRECTIVE = DIRECTIVES_SUMS[0]
loop for X from 1 to 5
    if MAX_DIRECTIVE < DIRECTIVES_SUMS[X] then
        MAX_DIRECTIVE = DIRECTIVES_SUMS[X] 
    end if
end loop
output MAX_DIRECTIVE div 12 //number of months

Another notation with filling a new 1D array

imagen
Credit: Z.A.

Output the directive with most months with 0 expense

We need to loop through each directive and have a counter with each month that is exactly 0

MOST_ZERO = 0
MOST_ZERO_DIRECTIVE = 0
loop for DIRECTIVE from 0 to 5
    ZERO_COUNT = 0
    loop for MONTH from 0 to 11
        if ( EXPENSES [DIRECTIVE] [MONTH] == 0) then
            ZERO_COUNT = ZERO_COUNT +1
        end if
    end loop
    if (ZERO_COUNT > MOST_ZERO) then
        MOST_ZERO = ZERO_COUNT
        MOST_ZERO_DIRECTIVE = DIRECTIVE
    end if
end loop
output MOST_ZERO_DIRECTIVE

imagen
imagen

Credit T.J.

Relevant lexicon about 2D arrays

2D arrays behave like 2D matrixes in math. We're not working on that (in IB they don't do matrixes even in HL AA) but we can be familiar with some names

The main diagonal is the conjuct of elements that go with the first row, first column, second row second element and so on:

1 fI0_NPLj_Pl7-z6ZOA72MA

Using the typical notation for looping through a

Typical problem

Check if some 2D array is valid or not. Usually we have to check item by item by some condition (or several conditions!)

MY_2D_ARRAY [50][100] // supose that we have an array with 50 rows and 100 columns VALID = TRUE // we start with a flag variable loop ROW from 0 to 49 loop COL from 0 to 99 if MY_2D_ARRAY [ROW][COL] blablablah then VALID = false //here check the conditions NOT to be valid end if end loop end loop if VALID output "is valid" //or do whatever if it's valid else output "is not valid" //or do whatever if it's not valid end if

Another option (a bit more tedious) is to count the valid elements. If all of them are valid, the total array is valid. Remember that the total number of elements

Getting the elements in the correct order

Sometimes we need to loop through the array in no order but some other times we actually need to traverse the array in the correct order.

Let's define a simple 2D array of 3x4 as an example. Let's call it MAT

indexes [0] [1] [2] [3]
[0] 3 4 8 9
[1] 100 46 12 7
[2] 0 90 1 22

If we want output from left to right and then top to botton then we need to loop through the rows and then through the columns. Let's say that we want to just output it. It would be like this

MAT //3 rows, 4 columns loop ROW from 0 to 2 loop COL from 0 to 3 output MAT[ROW][COL] end loop end loop

The output will be: 3,4,8,9,100,46,12,7,0,90,1,22

But if we want output first columns also written as from top to botton and then left to right we need to inverse these loops.

MAT //3 rows, 4 columns loop COL from 0 to 3 //remember that we still have 4 columns loop ROW from 0 to 2 output MAT[ROW][COL] end loop end loop

The output will be: 3,100,0, 4, 46, 90,8,12, 1, 9, 7, 22

We can go even further and go backwards going rom the bottom right to the top left, first rows, then columns. To do it we need to reverse the loop

In some info I have seen the keyword "downward to " written in the for loop that is also correct.

MAT //4 rows, 3 columns loop ROW from 2 to 0 loop COL from 3 to 0 output MAT[ROW][COL] end loop end loop

The output will be in this case:

22,1,90,0,7,12,46,100,9,8,4,3

You can always trace the algorithm with the values of ROW and COL to get the info. In the last case we would have a value that is 2,3 then 2,2 and so on.

Exercise of outputing in the correct order

From this 3x3 MAT

indexes [0] [1] [2]
[0] 1 2 3
[1] 4 5 6
[2] 7 8 9

We want to output the elements in this order

7,4,1,8,5,2,9,6,3

Solution on the spoiler:

![HkgK_wN66](https://hackmd.io/_uploads/HydfxTb2kl.png)


_credit Y.C._

Then we want to from right to left then top to bottom

Solution on the spoiler:

![imagen](https://hackmd.io/_uploads/SkS9_wV6p.png)
_credit Y.C._

Filling other arrays from one 2D Array

Sometimes we need to fill other linear arrays with one 2D array. For doing that we need to have those arrays initialized and with suficient size to have storage of what we want to fill in.

Then what we usually need to do is to know what are conditions to fill the array or arrays (we might have more than one). And also be aware of the index of the array that we are filling in

For my experience in the real world you call it "fill" the arrays but in IB exams I have seen "construct" or create as the verb to declare and fill the arrays with values

Let's start simple

If we have this array (MATHEW):

indexes [0] [1] [2] [3]
[0] 3 4 8 9
[1] 100 46 12 7
[2] 0 90 1 22

We can store in array the ODD elements in the array ODD

For doing this the could would be like this:

INDEX = 0 MATHEW //3 rows, 4 columns ODD // linear array loop ROW from 0 to 2 loop COL from 0 to 3 if MATHEW[ROW][COL] mod 2 ==1 then ODD[INDEX] = MATHEW[ROW][COL] INDEX = INDEX +1 end if end loop end loop

In exam questions I have seen that the array where we have to store the values is given but we need to create the variable to store the index.

Exercise of simple array filling

Now let's supose that we have to store not only the odds but also the even numbers in the array EVEN.

Construct the code for creating the 2 arrays (ODD and EVEN) from the 2D Array MATHEW.

We have 2 arrays with two independent indexes!

Solution on the spoiler section

imagen
credit to Y.C.

Another solution with 2 loops (longer but correct also)

INDEX = 0
MATHEW 
ODD 
loop ROW from 0 to 2
    loop COL from 0 to 3
        if MATHEW[ROW][COL] mod 2 ==1 then
            ODD[INDEX] = MATHEW[ROW][COL] 
            INDEX = INDEX +1
        end if
    end loop
end loop
INDEXE = 0
MATHEW
EVEN
loop ROW from 0 to 2
    loop COL from 0 to 3
        if MATHEW[ROW][COL] mod 2 ==0 then
            EVEN[INDEXE] = MATHEW[ROW][COL] 
            INDEXE = INDEXE +1
        end if
    end loop
end loop

credit to V.G.

Simple operations in a 2D array

Let's have a 2D array of numbers, for example 6 by 6

NUMBERS[6][6]  //36 elements, 6 columns, 6 rows

Find the average

Find the average of numbers.

NUMBERS[6][6]  //36 elements, 6 columns, 6 rows
SUM= 0
loop for X from 0 to 5
    loop for Y from 0 to 5
        SUM = SUM + NUMBERS[X][Y]
    end loop
end loop
AVERAGE = SUM div 36

Why div and not "/" if it's what we see in regular programming?

Slash is equivocous. It can be correct but with div we're making sure that we're talking about the integer division. So I suggest to use div.

The diagonals

The diagonals are a concept in 2D arrays (and matrixes)

image
They don't ask it directly but they expect you to handle this concept.

When we have a square matrix (same size in both dimensions) we can find the 2 diagonals. The main diagonal includes the elements with the indexes [0][0], [1][1], [2][2], [n-2][n-2], [n-1][n-1] where n is the size of the array (and the array size in total is

n2)

The other (secondary) diagonal starts with [0][n-1], [1][n-2], [2][n-3], [n-2][1], [n-1][0].

1_fI0_NPLj_Pl7-z6ZOA72MA

Why is it useful. For example in saving distances between stations (each index corresponds to a specific station). The elements in the main diagonal are the ones that corresponds to the distance between a station and itself and it would be always 0.

Here you would have the duplicated 2D array

indexes [0] [1] [2] [3] [4] [5]
[0] 0 4 7 2 1 8
[1] 4 0 8 9 1 5
[2] 7 8 0 2 4 3
[3] 2 9 2 0 10 2
[4] 1 1 4 10 0 11
[5] 8 5 3 2 11 0

Also since, usually, the distance is the same between A and B and between B and A, we don't have to write all the values in this matrix. We can write in one of the halves and leave the other empty. This is done to avoid having duplicate data that leads to inconsistencies.

indexes [0] [1] [2] [3] [4] [5]
[0] 0 0 0 0 0 0
[1] 4 0 0 0 0 0
[2] 7 8 0 0 0 0
[3] 2 9 2 0 0 0
[4] 1 1 4 10 0 0
[5] 8 5 3 2 11 0

With that information, let's go back to Numbers[6][6]

Find the sum of each of the diagonals

Main diagonal

SUMMAIN= 0
loop for X from 0 to 5
	SUMMAIN = SUMMAIN + Numbers[X][X]
end loop
output SUMMAIN

Credit V.G.

Secondary diagonal

SUM= 0
loop for X from 0 to 5
	SUM = SUMMAIN + Numbers[X][5-X]
end loop
output SUM

The tricky part here is the 5-X. Remember that the secondary diagonal goes [0][n-1], [1][n-2] and so on.

In this case having it with the same variable that changes over time ensures that we're only adding the elements of that diagonal.

Remember that this is going to sum all the elemnents!

This is a common mistake

SUM = 0
loop for X from 5 to 0
	loop for Y from 5 to 0
	SUM = SUM + Numbers[X][Y]
	end loop
end loop
output SUM

Find if numbers is a magic square

This was an exercise of a past exam. It's unlikely that it's going to happen again but having it in mind is useful for other cases.

A magic square is a square that their rows, their columns and their diagonals have the same sum. So to check it out you will need to have a flag variable and find if all the sums are equal.

Construct the code

Filling 2D arrays

When we create a 2D array it's usually empty and then we need to fill it with data. One way to do it is just by rows or by columns. The most basic way to do this would be something like this is to fill it with consecutive numbers from 1 to NxM (being NxM the size of the dimensions of the array)

This would be the example for a MATTHEW of 3x4

indexes [0] [1] [2] [3]
[0] 1 2 3 4
[1] 5 6 7 8
[2] 9 10 11 12

The code would be the following:

K = 1
loop for ROW from 0 to 2
    loop for COL from 0 to 3
         MATTHEW [ROW][COL] = K
         K = K +1
    end loop
end loop 

Example of filling. Tracing a 2D array algorithm

Other example that I've seen in some exams is to trace an algorithm that, for example, fills a 2D array.

Take this example

N = input
K = N*N
loop for ROW from N-1 to 0
    loop for COL from 0 to N-1
        A[ROW][COL] = K
        K = K-1
    end loop
end loop

They told as also that Ais a 2D array and they ask you to trace the algorithm to show the contents of the array A after the algorithm is executed for N = 3

Solutions on the spoiler

Trace table

N K ROW COL A[ROW][COL]
3 9 2 0 9
3 8 2 1 8
3 7 2 2 7
3 6 1 0 6
3 5 1 1 5
3 4 1 2 4
3 3 0 0 3
3 2 0 1 2
3 1 0 2 1

This leads to an array A that has this content:

indexes [0] [1] [2]
[0] 3 2 1
[1] 6 5 4
[2] 9 8 7

Exercise:

Fill the array A for N = 4

Solution after the spoiler

imagen
credit A.P.

imagen
Credit A.B.

Complicated follow the rithm in 2D array

In the last exercise we saw one way to fill an array but what happen if we want to fill it in weird ways?

Imagine that we want to do fill Matthew in a way that always the numbers that are consecutive they have a consecutive number nearby. We can do a snake.

Something like this:

indexes [0] [1] [2] [3]
[0] 1 2 3 4
[1] 8 7 6 5
[2] 9 10 11 12

So in this case we're going to go from left to right in the first row and from right to left in the second row and back left to right in the third and so on.

In some exams they tell the idea of this code and then you need to actually write it. For example the description for this will be

Initialize T=1
Every time that we allocate T in the 2D array MATTHEW we're going to increase the value of T by 1
Iterate until the 2D array is filled
Fill the elements of the first (top) row from left to right
Then fill the elements of the next row from right to left
Continue filling until is filled the whole array

Remember. If they tell you the initialization of a variable it means the first value it gets.

Construct the code described above

T = 1
loop for ROW from 0 to N-1 //size of rows of MATTHEW
    if ROW mod 2 = 0
        loop for COL from 0 to M-1 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
        end loop
    else
        loop for COL from M-1 to 0 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
    end loop
    end if 
end loop

Another posible solution without ifs nuances

T = 1
ROW = 0
loop while ROW < N //number of rows
        loop for COL from 0 to M-1 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
        end loop
    ROW = ROW +1
        loop for COL from M-1 to 0 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
    end loop
    ROW = ROW +1
end loop

Small execise: find the problem of this algorithm (is not a syntax error)

solution on the spoiler (a whole class of HL struggled with this)

If we have an odd number of rows we're going to be out of boundaries. In this case we're going to have ROW equal to N

One possible solution is this

T = 1
ROW = 0
loop while ROW < N //number of rows
        loop for COL from 0 to M-1 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
        end loop
    ROW = ROW +1
    if ROW = N then
        break
    end if
        loop for COL from M-1 to 0 //size of columns of MATTHEW
            A[ROW][COL] = T
            T = T+1
    end loop
    ROW = ROW +1
end loop

Another solution that can fill the array but doesn't follow the instructions given would be starting by 12 and fill backwards.

Tricky question, asking about indexes

Something that can be asked in an exam is

What are the indices (subscripts) of the first and the last element filled in the bottom row in the array PETER given that PETER is a 2D array with 4 rows and 6 colums (a 4x6 2D Array)

Hint: in this question they don't ask for the values, they ask for the indexes the numbers that go after PETER in PETER[0][3].

Also notice than PETER doesn't have the same size of MATTHEW

Solution in the spoiler

The fist element of the bottom rowe that will be the [3][5] and the last the [3][0]

2D Arrays as fields in a spreadsheet

Sometimes we find 2D arrays that represents data with fields. For example if we have an array called CLIENTS and each row respresents a client and each column represents a specific data.

indexes [0] [1] [2] [3]
[0] Pepito Perez Mataró 08304
[1] Juanita López Barcelona 08003
[2] Ivan Jokovich Madrid 28040

In this case the column [0] represents names, column [1] represents surnames, column [2] represents cities and column [3] represent postal codes.

Exercise with counting in a 2D array

Imagine that in this array instead of having 3 rows we have 256 rows.

Construct a code for counting how many clients live in Madrid and display the result. You have access to the 2D array CLIENTS

Working

Remember that in this cases we need to understand what is the size of the array (remember that sometimes the size of the array is not a number but a variable like N). In this case we have 256 rows and 4 colums so the size of the array is 256x4.

In this case we don't need to loop through all the elements but only through all the rows. To do that we write

loop for ROW from 0 to 255

end loop

Since we need to count and output the count we can also add it.

COUNT = 0
loop for ROW from 0 to 255

end loop
output COUNT

To identify the element that we need to check is allways in the same column. If we check again we can see that that column is [2] so we only need to check if the [2] element of the row is "Madrid". To access it we use CLIENTS[ROW][2]

COUNT = 0
loop for ROW from 0 to 255
    if CLIENTS[ROW][2] == "Madrid" then
    
    end if
end loop
output COUNT

Lastly inside the if we only need to add 1 to the counter that we're going to output later.

COUNT = 0
loop for ROW from 0 to 255
    if CLIENTS[ROW][2] == "Madrid" then
        COUNT = COUNT +1
    end if
end loop
output COUNT

Exercises:

Count how many clients come from Paris. And output the number.

Solution by the students on the spoiler

//First question
COUNT = 0
CLIENTS
 
Loop ROW from 0 to 255
	if CLIENTS[ROW][2] == "Paris" then
		COUNT = COUNT +1
	end if
end loop
output COUNT

credit M.S.

Output if we have any client that is called Rocco. We need to have a different message if we have and if we have not.

Solution by the students on the spoiler

//Second question
COUNT = 0
CLIENTS
 
Loop ROW from 0 to 255
	if CLIENTS[ROW][0] == "Rocco" then
		COUNT = COUNT +1
	end if
end loop
If COUNT > 0 then
	OUTPUT "We found Rocco and there are this many people named Rocco" + COUNT
else
	OUTPUT "There is no one named Rocco"
end if

credit M.S.

Y= false loop for ROW from 0 to 255       if CLIENTS[ROW][0] == "Rocco" then             Y = true             break       end if end loop If Y then       output "there is a Rocco" else       output "there is no Rocco" end if

credit K.B.

Output if we have any client that is called Rocco and lives in Paris. We need to have a different message if we have and if we have not.

Solution by the students on the spoiler

//Third question
COUNT = 0
CLIENTS
 
Loop ROW from 0 to 255
	if CLIENTS[ROW][0] == "Rocco" AND CLIENTS[ROW][2] == "Paris" then
		COUNT = COUNT +1
	end if
end loop
if COUNT > 0 then
	OUTPUT "There is people named Rocco and live in Paris and this is how many there are" + COUNT
else
	OUTPUT "There is no one named Rocco and lives in Paris"
end if

credit M.S.

Y = false loop for ROW from 0 to 255       if CLIENTS[ROW][0] == "Rocco" AND CLIENTS[ROW][2] == "Paris" then             Y = true             break       end if end loop If Y then       output "there is a Rocco from Paris" else       output "there is no Rocco from Paris" end if

credit K.B.

1D arrays as headers

Sometimes we can find an auxiliary 1D array to a 2D array as a header for example if we have a 2D array that tells the distances between train stations

This is the 2D array DISTANCES

indexes [0] [1] [2] [3]
[0] 0 0 0 0
[1] 10 0 0 0
[2] 15 50 0 0
[3] 35 40 25 0

And then you have the array STATIONS that follows this structure

indexes [0] [1] [2] [3]
values Concepcion Lota Chillan Los Angeles

Finding maximum and distances

Construct a code that finds the maximum distance between Stations and outputs the distance and the stations that are more far apart.

Solution from one student in the spoilers:

MAX1_I = 0
MAX2_I = 0
MAX = 0
loop for ROW from 0 to 3
     loop for COL from 0 to 3
         if MAX < DISTANCES[ROW][COL]then 
             MAX = DISTANCES[ROW][COL]
             MAX1_I = ROW
             MAX2_I = COL
         end if
    end loop
end loop
 
output "the max distances are between " + STATIONS[MAX1_I] +  "and"+  STATIONS[MAX2_I] + "and the distance is" + MAX

credit A.B.

Finding distances from stations

Statement

Construct an algorithm in pseudocode that inputs the names of two bus stations and outputs the distance between them. If any of the inputted names are not found, the method should output an appropriate message. The name of the subprogram is findDistance

Working

After the spoiler

In this case first we need to understand that we have 2 inputs that are strings that should be in the array STATIONS and we need to know the index of them in order to find the distance.

The "signature" of this method in this case should be something like findDistance(STATION1, STATION2).

So we have something like this right now

findDistance(STATION1, STATION2)

end findDistance 

we can work with other names for the STATION1 and STATION2 and also we can finish with "end method" instead of end findDistance

The next thing that we need to do is to know is the inputs (STATION1 and STATION2) are valid.

To do that we need to loop through the array, which one? STATIONS

findDistance(STATION1, STATION2)
    loop for N from 0 to 3 //last index of the array STATIONS
    end loop
end findDistance 

Inside that loop we're going to check if we have the value of STATION1 or STATION2 in 2 different ifs.


findDistance(STATION1, STATION2)
    loop for N from 0 to 3 
        if STATION[N] == STATION1 then
        
        end if
        if STATION[N] == STATION2 then
        
        end if
    end loop
end findDistance 

What do we do in those ifs? Store the index. In this case I'm going to use those indexes as a flag variable and I'm going to initialize them as -1. If we have found the STATIONS the indes should be a number from 0 to 3 (cannot be -1 because there is no -1 index) so I can use an if to see if there is no problem with that.


findDistance(STATION1, STATION2)
    INDEX1 = -1
    INDEX2 = -1
    loop for N from 0 to 3 
        if STATION[N] == STATION1 then
            INDEX1 = N
        end if
        if STATION[N] == STATION2 then
            INDEX2 = N
        end if
    end loop
end findDistance 

Now we have to check if we have updated the indexes, to do it we need to compare those indexes. We can either check if both of them are between 0 and 3 or check if they are still -1. The former indicates that we have change both of them. The later checks if any of those indexes did not update at all.

This goes after the loop through STATIONS.

findDistance(STATION1, STATION2)
    INDEX1 = -1
    INDEX2 = -1
    loop for N from 0 to 3 
        if STATION[N] == STATION1 then
            INDEX1 = N
        end if
        if STATION[N] == STATION2 then
            INDEX2 = N
        end if
    end loop
    if INDEX1 == -1 OR INDEX2 == -1 then 
        output "One of the inputs is wrong"
        return //end method
    end if
end findDistance 

In the case that we have found that either one or the other index is -1, there is no need to continue with the execution. We can stop it through a return (return always stops the method) or using an else to execute the rest of the code.

Now we have to check what are the indexes relatively from each other. In this case we have to because the information of the distance is in the lower diagonal (where ROW > COL)

So we need an if

If INDEX1 is equal than INDEX2 the distance is 0 because is the same station.
If INDEX1 is bigger than INDEX2 we need to check DISTANCES[INDEX1][INDEX2] to find the distance
If INDEX2 is bigger than INDEX1 we need to swap and check DISTANCES[INDEX2][INDEX1] to find the distance.

To write it down it would look like this:

findDistance(STATION1, STATION2)
    INDEX1 = -1
    INDEX2 = -1
    loop for N from 0 to 3 
        if STATION[N] == STATION1 then
            INDEX1 = N
        end if
        if STATION[N] == STATION2 then
            INDEX2 = N
        end if
    end loop
    if INDEX1 == -1 OR INDEX2 == -1 then 
        output "One of the inputs is wrong"
        return //end method
    end if
    if INDEX1 == INDEX2 then
        output "Is the same station, the distance is 0"
    else if INDEX1 > INDEX2 then
        output "The distance is " 
        output DISTANCES[INDEX1][INDEX2]
    else
        output "The distance is " 
        output DISTANCES[INDEX2][INDEX1]  
    end if
end findDistance 

Why the change between Index2 and index1? Because we are making sure that we have access to the element that has information.

:::

Result

After the spoiler

findDistance(STATION1, STATION2)
    INDEX1 = -1
    INDEX2 = -1
    loop for N from 0 to 3 
        if STATION[N] == STATION1 then
            INDEX1 = N
        end if
        if STATION[N] == STATION2 then
            INDEX2 = N
        end if
    end loop
    if INDEX1 == -1 OR INDEX2 == -1 then 
        output "One of the inputs is wrong"
        return //end method
    end if
    if INDEX1 == INDEX2 then
        output "Is the same station, the distance is 0"
    else if INDEX1 > INDEX2 then
        output "The distance is " 
        output DISTANCES[INDEX1][INDEX2]
    else
        output "The distance is " 
        output DISTANCES[INDEX2][INDEX1]  
    end if
end findDistance 

EXAMPLE OF THE SPIRAL OF DEATH

This is an exercise of filling a 2D array now in a spiral way. The idea is that we create a way spiral the creation.

We have a NxM 2D array

We need to fill it in an spiral from left to right then top to bottom, right to left and finally bottom to top.

To do that first we need to define the name of the array and the NxM

SPIRAL //N rows times M columns
VALUE = 1


Then, we need to do a loop. That loop would need to last until we finish the filling. We can tell because the number of VALUE that will be increasing will achive up to the value of

SPIRAL 
VALUE = 1
loop while VALUE <= N * M //N rows times M columns


end loop

Now we need to know what are the boundaries that we're going to loop through.

Those boundaries can be described as TOPROW, BOTTOMROW, LEFTCOL and RIGHTCOL

The initial values of these are the indexes of those elements (Rows and columns)

TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns

Those values are set before the loop since we're not going to reset them.

SPIRAL 
VALUE = 1
TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns
loop while VALUE <= N * M //N rows times M columns


end loop

The first loop will go from left to right in the top row. So we can write it like this

loop COL from LEFTCOL to RIGHTCOL

end loop

inside we need to set the value into the array (SPIRAL) and increment value by 1.

loop COL from LEFTCOL to RIGHTCOL
    SPIRAL[TOPROW][COL] = VALUE
    VALUE = VALUE + 1
end loop

we set this in the while:

SPIRAL 
VALUE = 1
TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns
loop while VALUE <= N * M //N rows times M columns
    loop COL from LEFTCOL to RIGHTCOL
        SPIRAL[TOPROW][COL] = VALUE
        VALUE = VALUE + 1
    end loop
end loop

Not to repeat the values we need to add the +1 to TOPROW

SPIRAL 
VALUE = 1
TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns
loop while VALUE <= N * M //N rows times M columns
    loop COL from LEFTCOL to RIGHTCOL
        SPIRAL[TOPROW][COL] = VALUE
        VALUE = VALUE + 1
    end loop
    TOPROW= TOPROW +1
end loop

Now we need to loop through the right column like this

loop ROW from TOPROW to BOTTOMROW
    SPIRAL[ROW][RIGHTCOL] = VALUE
    VALUE = VALUE + 1
end loop

And we add it as before.

SPIRAL 
VALUE = 1
TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns
loop while VALUE <= N * M //N rows times M columns
    loop COL from LEFTCOL to RIGHTCOL
        SPIRAL[TOPROW][COL] = VALUE
        VALUE = VALUE + 1
    end loop
    TOPROW= TOPROW +1
    loop ROW from TOPROW to BOTTOMROW
        SPIRAL[ROW][RIGHTCOL] = VALUE
        VALUE = VALUE + 1
    end loop
end loop

We add the substract 1 to RIGHTCOL not to overwrite the value

SPIRAL 
VALUE = 1
TOPROW=0
BOTTOMROW=N-1 //N is for rows
LEFTCOL= 0
RIGHTCOL= M-1 //M is for columns
loop while VALUE <= N * M //N rows times M columns
    loop COL from LEFTCOL to RIGHTCOL
        SPIRAL[TOPROW][COL] = VALUE
        VALUE = VALUE + 1
    end loop
    TOPROW= TOPROW +1
    loop ROW from TOPROW to BOTTOMROW
        SPIRAL[ROW][RIGHTCOL] = VALUE
        VALUE = VALUE + 1
    end loop
    RIGHTCOL = RIGHTCOL -1
end loop

We continue doing like this. The solution afther the spoiler

imagen
credit Z.A.

Exercise of diagonals

Let's have a 2DArray called MAT

Construc in pseudocode a sub-program that accepsts a 2D array MAT and finds if the main diagonal has a bigger sum than the secondary diagonal and outputs the conclusion. If that information cannot be found, an appropiate message should be displayed.

Solution in the spoiler:

Solution 1

if MAT.length == MAT[0].length then 
    SUM1=0 //main diagonal sum
    SUM2=0 //secondary diagonal sum
    loop for Y from 0 to MAT.length
        loop for X from 0 to MAT[0].length //can be also MAT.length
            if X=Y then 
                SUM1 = SUM1 + MAT[X][Y]
            else if X+Y == N-1
                SUM2 = SUM2 + MAT[X][Y]
            end if
        end loop
    end loop
    if SUM1> SUM2 then
        output "Main diagonal is bigger"
    else if SUM2 > SUM1 then
        output "Secondary diagonal is bigger"
    else then
        output "Both diagonals are equal"
    end if
else
    output "MAT is not suitable for finding diagonals"
end if

Credit C.V. (with modifications)

if MAT.length == MAT[0].length then 
    sumFirst = 0 //mainDiagonal
    sumSecond = 0 //secondaryDiagonal
    loop for x from 0 to n-1
        sumFirst = sumFirst + MAT[x][x]
    end loop
    loop for y from 0 to n-1
        sumSecond = sumSecond + MAT[y][n-y-1]
    end loop
    if sumFirst>sumSecond
        output "first diagonal sum is bigger"
    else if sumSecond> sumFirst
            output "Secondary diagonal sum is bigger"
        else 
        output "they are equal"
        end if
    end if
else 
    output "array is not suitable"
  end if

Credit K.B.