Sheerpower Logo
O.3  Working with Arrays

Lesson 3: Arrays in Sheerpower

Arrays are an essential part of programming, allowing you to store and manipulate collections of data efficiently. Sheerpower supports arrays of reals, strings, and booleans. By default, Sheerpower arrays are one-origin, meaning the first element of an array is element number one. However, you can change this to zero-origin using the option base 0 statement.

1. Declaring Arrays

To define an array in Sheerpower, you use the dim statement. Let's start by declaring a simple array of numbers.

dim ages(5) // five reals: 1, 2, 3, 4, and 5 dim names$(5) // five string elements. ages(1) = 25 ages(2) = 30 ages(3) = 45 ages(4) = 50 ages(5) = 60 names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" names$(4) = "Diana" names$(5) = "Eve" print "Age of Alice: "; ages(1) print "Name of person with age 45: "; names$(3)

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This program declares an array of ages and an array of names, assigns values to them, and prints some of the values.

2. Multi-Dimensional Arrays

Sheerpower also supports multi-dimensional arrays. These are useful when you need to store data in a table-like structure. For example, let's create an array that represents the hours worked each day of the week.

dim ages(5) // five reals: 1, 2, 3, 4, and 5 dim names$(5) // five string elements. ages(1) = 25 ages(2) = 30 ages(3) = 45 ages(4) = 50 ages(5) = 60 names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" names$(4) = "Diana" names$(5) = "Eve" print "Age of Alice: "; ages(1) print "Name of person with age 45: "; names$(3) dim weekly_hours(7, 24) // 7 days a week, 24 hours a day // Assign random hours for each day for day = 1 to 7 for hour = 1 to 24 weekly_hours(day, hour) = rnd(8) next hour next day // Print hours worked on the first day of the week print "Hours worked on Monday:" for hour = 1 to 24 print "Hour "; hour; ": "; weekly_hours(1, hour) next hour

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This program creates a two-dimensional array to represent hours worked each day of the week and assigns random values to it.

3. Resizing Arrays

You can increase the size of an existing array using the redim statement, which is particularly useful when your program requires the array to grow dynamically. When you resize an array with redim, the existing data remains intact, and additional elements are added to the array as needed. For example, if you resize an array from 5 to 10 elements, the original 5 elements will be preserved, and 5 new elements will be created, though these new elements will be uninitialized.

dim ages(5) // five reals: 1, 2, 3, 4, and 5 dim names$(5) // five string elements. ages(1) = 25 ages(2) = 30 ages(3) = 45 ages(4) = 50 ages(5) = 60 names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" names$(4) = "Diana" names$(5) = "Eve" print "Age of Alice: "; ages(1) print "Name of person with age 45: "; names$(3) dim weekly_hours(7, 24) // 7 days a week, 24 hours a day // Assign random hours for each day for day = 1 to 7 for hour = 1 to 24 weekly_hours(day, hour) = rnd(8) next hour next day // Print hours worked on the first day of the week print "Hours worked on Monday:" for hour = 1 to 24 print "Hour "; hour; ": "; weekly_hours(1, hour) next hour max_ages = 5 dim ages(max_ages) // five reals: 1, 2, 3, 4, and 5 max_ages = 10 redim ages(max_ages) // Now we have 10 elements ages(6) = 65 ages(7) = 70 print "New age added: "; ages(6)

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code demonstrates how to resize an array using the redim statement.

4. Dynamically Expanding Arrays

If you don't know the size of the array you need beforehand, you can create a dynamically expanding array. This allows the array to grow as needed during the program's execution.

dim ages(5) // five reals: 1, 2, 3, 4, and 5 dim names$(5) // five string elements. ages(1) = 25 ages(2) = 30 ages(3) = 45 ages(4) = 50 ages(5) = 60 names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" names$(4) = "Diana" names$(5) = "Eve" print "Age of Alice: "; ages(1) print "Name of person with age 45: "; names$(3) dim weekly_hours(7, 24) // 7 days a week, 24 hours a day // Assign random hours for each day for day = 1 to 7 for hour = 1 to 24 weekly_hours(day, hour) = rnd(8) next hour next day // Print hours worked on the first day of the week print "Hours worked on Monday:" for hour = 1 to 24 print "Hour "; hour; ": "; weekly_hours(1, hour) next hour max_ages = 5 dim ages(max_ages) // five reals: 1, 2, 3, 4, and 5 max_ages = 10 redim ages(max_ages) // Now we have 10 elements ages(6) = 65 ages(7) = 70 print "New age added: "; ages(6) dim cash(0) // this is how we declare a dynamically expanding array cash(0) = 10.99 cash(1) = 20.50 print 'Cash elements:'; size(cash)

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code shows how to create a dynamically expanding array and add elements to it.

5. Rolling Dice Using Arrays

Let's put everything we've learned so far together in a fun example. We'll create an array to represent rolling five dice and print the value of each die.

dim ages(5) // five reals: 1, 2, 3, 4, and 5 dim names$(5) // five string elements. ages(1) = 25 ages(2) = 30 ages(3) = 45 ages(4) = 50 ages(5) = 60 names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" names$(4) = "Diana" names$(5) = "Eve" print "Age of Alice: "; ages(1) print "Name of person with age 45: "; names$(3) dim weekly_hours(7, 24) // 7 days a week, 24 hours a day // Assign random hours for each day for day = 1 to 7 for hour = 1 to 24 weekly_hours(day, hour) = rnd(8) next hour next day // Print hours worked on the first day of the week print "Hours worked on Monday:" for hour = 1 to 24 print "Hour "; hour; ": "; weekly_hours(1, hour) next hour max_ages = 5 dim ages(max_ages) // five reals: 1, 2, 3, 4, and 5 max_ages = 10 redim ages(max_ages) // Now we have 10 elements ages(6) = 65 ages(7) = 70 print "New age added: "; ages(6) dim cash(0) // this is how we declare a dynamically expanding array cash(0) = 10.99 cash(1) = 20.50 print 'Cash elements:'; size(cash) dim dice(5) // five dice for idx = 1 to 5 dice(idx) = rnd(6) // assign each die a random number between 1 and 6 print 'dice#'; idx; ' value '; dice(idx) next idx

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This program simulates rolling five dice and displays the value of each die.

Summary: In this lesson, you've learned how to work with arrays in Sheerpower, including how to declare, resize, and dynamically expand arrays. Arrays are a powerful tool for managing collections of data, and they are essential for writing efficient and effective programs. Keep practicing to master arrays and take your Sheerpower programming skills to the next level!
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen