Senin, 23 Maret 2020

SMALL BASIC: Playing with multi-array

Hello, welcome SMALL Basic programmer.

In this tutorial, we will played with multi-array in SMALL basic, and doing stuff like:
1. Printing all element forward; from the first element to the last element.
2. Printing all elements backward; from the last element to the first element.
3. Printing range of elements of a row or line
4. Printing range of elements of a colomn
5. Printing few of elements of a line.
6. Printing few of elements of a colomn.
7. Printing all element of a row
8. Printing all element of a colomn
9. Printing all element of two rows
10. Printing cyclic row
10.
6. Printing a specific or an element of a row
7. Printing a specific or an element of a colomn
8. Diagonal left
9. Diagonal right

Pre-requisite
So, to be able to work this material, you need to learn:
1. How to create multi-array in SMALL Basic
2. Nested for in SMALL Basic

So, welcome to the journey of multi-dimension array! It is fun journey!
FACT
1. Notice that we use an example that consist of square matrix, it means the length for the column is the same for all the row.
2. So, we can use array.getitemcount(ourarray) operation to get the length of each for row and for colomn of our array - ourarray.
3. It is often good to think multi array as m-floor building with n-room in each floor.

But we devide this tutorial as:
1. Just printing the element matrix
2. Playing arithmatic with matrix like add, substract, divide, or multiply.

First, off course, we need to build a multi array; a 3 dimension array; a 3 row times 3 colomn array, how? like below:

But first, you know, you can make matrix 2 row times 2 colomns like below:
ourarray[0][0] = 1
ourarray[0][1] = 1
ourarray[1][0] = 0
ourarray[1][1] = 0

But it to small for us to play. So, we will make a matrix with 3 row times 3 colomn instead. Like below:
ourarray[0][0] = 1
ourarray[0][1] = 1
ourarray[0][2] = 0
ourarray[1][0] = 0
ourarray[1][1] = 0
ourarray[1][2] = 1
ourarray[2][0] = 1
ourarray[2][1] = 1
ourarray[2][2] = 0

And then printing out
1. Printing all element forward

  For i = 0 To Array.GetItemCount(ourarray) - 1
    For j = 0 To Array.GetItemCount(ourarray) - 1
      TextWindow.WriteLine(ourarray[i][j])
    EndFor
  EndFor

2. Printing out backward

You will notice that both of the loop variable are minus
For i = Array.GetItemCount(ourarray) - 1 to 0 step -1
  For j = Array.GetItemCount(ourarray) - 1 To 0 Step -1
    TextWindow.WriteLine("This is our array [" +i+"]["+j+"]: "+ ourarray[i][j])
    EndFor
  EndFor



3. Printing range of elements of a row
For instance, I want to print element column range from colomn 0 to element colomn 1 of a row, for instance row 1.

The range itself, I put into an array called range. So the program is below:

row = 1
range_col[0] = 0
range_col[1] = 1

For i = row To row
      For j = range_col[0] To range_col[1]
        TextWindow.WriteLine("This is our array [" + i + "]["+ j +"] : " + ourarray[i][j])
      EndFor
    EndFor

Notice that the row loop variable, i.e. i is stick to the number of the row that you want to retrieve.

4. Printing range of elements of a colomn
Printing range of elements of a colomn is printing start from and end with.

The range itself as example 3, I put into an array called. So the program is below:
col = 1
range_row[0] = 1
range_row[1] = 2

For i = range_row[0] to range_row[1]
      For j = col To col
        TextWindow.WriteLine("This is our array [" + i + "][" + j + "] :" + ourarray[i][j])
      EndFor
    EndFor

5. Printing few elements of a colomn
The difference this printing few of elements of a colomn and printing range of element (example 4) is this will print uncontinuos elements. While printing range of element prints continous elements start from start point, end from end point.

In this example, we will print element's value of elemen 0 and elemen 2.

For i = row to row
      For j = 0 To Array.GetItemCount(ourarray) - 1
        For k = 0 To Array.GetItemCount(fewcol) - 1
          If j = fewcol[k] then
            TextWindow.WriteLine("This is our array [" + i + "][" + j + "]: " + ourarray[i][j])
          EndIf
        EndFor
        EndFor
        endfor

As you notice, here we use three loops, that are loop i, lo

6. Printing few elements of a row
The difference this printing few of elements of a row and printing few element


7. Printing few elements of a col

For i = 0 to Array.GetItemCount(ourarray) - 1
      For j = col To col
        For k = 0 To Array.GetItemCount(fewrow) - 1
          If i = fewrow[k] then
            TextWindow.WriteLine("This is our array [" + i + "][" + j + "]: " + ourarray[i][j])
          EndIf
        EndFor
        EndFor
        endfor
7. Printing all element of a row
The clue when printing all element of a line is: the line loop variable is stick to the number of the line that you like to print, but the colomn loop variable is changing.

For example:

5. Printing all element of a colomn
The clue when printing all element of a colomn is: the colomn loop variable is stick to the number of the colomn that you like to print, but the line loop variable is changing.

For example below we want to print all the element's value of colomn 2:
For i = 0 to 2
For j = 1 to 1
textwindow.writeline(ourarray[i][j])
endfor
endfor


6. Printing



8. Printing diagonal left or decline diagonal (if we look or start from the left side of the matrix)
Printing diagonal left using loop is unique; it only uses 1 loop not two as like other examples. So like this:

For i = 0 to Array.GetItemCount(ourarray) - 1
j = i
textwindow.writeline(ourarray[i][j])
endfor

If you use two loops it would produce "printing all elements" program.

9. Printing diagonal right or incline diagonal (if we look or start from the left side of the matrix)
Printing diagonal right or incline diagonal is uniqe, because one of the loop is counting backward that is the colomn

For i = 0 to array.getitemcount(ourarray)
for j = array.getitemcount(ourarray) - 1 to 0 step -1
textwindow.writeline(ourarray[i][j])
endfor
endfor

Notice

10. Printing cyclic row

This is when you want to print from different row other than row 0 and then back to behind your start or desired row.

For instance, I started printing all colomns start from row 1 to row 2 and then back to row 0.

textwindow.writeline("Enter the source or the start: ")
source = textwindow.read()

For i = source To array.GetItemCount(ourarray) - 1
  For j = 0 To Array.GetItemCount(ourarray[i]) - 1
    TextWindow.WriteLine(ourarray[i][j])
  EndFor
EndFor

If source > 0 Then
  For i = 0 To source
    For j = 0 to Array.GetItemCount(ourarray[i]) - 1
    TextWindow.WriteLine(ourarray[i][j])
    endfor
    EndFor
EndIf

i

Tidak ada komentar:

Posting Komentar