Inputting an array in RAPTOR involves using a loop to repeatedly prompt the user for input and storing each value in a specific indexed position of the array.
Unlike many programming languages, RAPTOR does not require a formal declaration of an array's size beforehand; the array grows dynamically as elements are added. You simply begin using the array with an index, and RAPTOR creates and expands it as needed.
Step-by-step process for inputting an array
The most common method for populating an array with user input is to use a loop structure, such as a While loop or a For loop, which processes a specific number of inputs.
Method 1: Using a For loop (Count-controlled)
This method is most straightforward when the number of elements is known in advance.
- Start a new RAPTOR flowchart.
- Add a variable for the array size. Use an
Inputsymbol to ask the user how many elements they want to enter. Store the result in a variable, such asarray_size. - Create an index variable. Use an
Assignmentsymbol to set a counter variable, such asi, to1. RAPTOR arrays are 1-indexed, meaning the first element is at position 1. - Insert a loop. Drag a
Loopsymbol and set the condition toi <= array_size. - Add an input inside the loop. Inside the loop, add an
Inputsymbol.- Prompt: For a clear user experience, use a descriptive prompt like
"Enter value for position " + i + ":". - Get: Set the input variable to the array element using the index, for example,
my_array[i]. This creates the array and its elements as the loop runs.
- Prompt: For a clear user experience, use a descriptive prompt like
- Increment the counter. Add an
Assignmentsymbol to increment the index variable:i <- i + 1. - Run the flowchart. The program will prompt the user for input for each position in the array.
Example walkthrough:
- Symbol:
Input- Prompt:
"Enter the number of elements:" - Get:
array_size
- Prompt:
- Symbol:
Assignment- Set:
i - To:
1
- Set:
- Symbol:
Loop- Condition:
i <= array_size
- Condition:
- Inside the loop:
- Symbol:
Input- Prompt:
"Enter number for position " + i - Get:
my_array[i]
- Prompt:
- Symbol:
Assignment- Set:
i - To:
i + 1
- Set:
- Symbol:
Method 2: Using a While loop (Sentinel-controlled)
This method is useful when the number of elements is unknown beforehand, allowing the user to stop entering values by providing a specific "sentinel" value (e.g., a negative number or zero).
- Start a new RAPTOR flowchart.
- Initialize variables. Use an
Assignmentsymbol to set up the loop index and a variable to hold the input. For example,i <- 1andinput_value <- 0. - Add an initial
Inputsymbol. Prompt the user for the first value, telling them what the sentinel value is.- Prompt:
"Enter a positive number (or 0 to stop):" - Get:
input_value
- Prompt:
- Insert a loop. Add a
Loopsymbol and set the condition to continue as long as the input is not the sentinel value:input_value != 0. - Store the input. Inside the loop, add an
Assignmentsymbol to place theinput_valueinto the array at the current index:my_array[i] <- input_value. - Increment the counter. Add an
Assignmentsymbol to increase the index for the next element:i <- i + 1. - Add another
Inputsymbol. At the end of the loop, prompt the user for the next value. This is a crucial step for the loop to continue:- Prompt:
"Enter next number (or 0 to stop):" - Get:
input_value
- Prompt:
- Run the flowchart. The loop will continue until the user enters the designated sentinel value.
Example walkthrough:
- Symbol:
Assignment- Set:
i - To:
1
- Set:
- Symbol:
Input- Prompt:
"Enter a number (0 to stop):" - Get:
input_value
- Prompt:
- Symbol:
Loop- Condition:
input_value != 0
- Condition:
- Inside the loop:
- Symbol:
Assignment- Set:
my_array[i] - To:
input_value
- Set:
- Symbol:
Assignment- Set:
i - To:
i + 1
- Set:
- Symbol:
Input- Prompt:
"Enter next number (0 to stop):" - Get:
input_value
- Prompt:
- Symbol:
Key concepts and best practices
Array creation and dynamic sizing
In RAPTOR, a separate declaration step to create an array is not needed. An array is automatically created and resized the moment a value is assigned to an indexed variable, like my_array[1]. This simplifies array handling but can lead to errors if attempting to access an element beyond the current highest index.
Array indexing
RAPTOR's array indices begin at 1, not 0. This is a key difference from many other languages and is important to remember when setting up loops and accessing elements.
Using Length_Of
After the user has finished inputting values, the final size of the array can be determined using the Length_Of function, for example, Length_Of(my_array). This is useful for subsequent processing, such as displaying the array's contents or finding the average.
Error handling
Consider how the program will handle unexpected user input.
- Non-numeric input: If the program expects numbers but the user enters text, RAPTOR will produce a runtime error. Nested loops and validation checks can prevent this, but for simple flowcharts, it's often assumed the user will provide valid input.
- Sentinel value issues: Ensure the sentinel value is a number the user is unlikely to enter as a valid data point. For example, using
0for an array of positive numbers is a good choice.
Displaying the array
After inputting the data, another For loop can iterate through the array and display its contents to the user, confirming that the input process was successful.
Example:
- Symbol:
Assignment- Set:
j - To:
1
- Set:
- Symbol:
Loop- Condition:
j <= Length_Of(my_array)
- Condition:
- Inside the loop:
- Symbol:
Output- Output:
"Element at position " + j + ": " + my_array[j]
- Output:
- Symbol:
Assignment- Set:
j - To:
j + 1
- Set:
- Symbol:
Summary of RAPTOR array input logic
The logical process for creating and populating an array with user input in RAPTOR is as follows:
- Prepare: Initialize a counter variable, typically named
i, to1. - Loop: Create a loop, either a
Whileloop for an unknown number of items (using a sentinel value) or aForloop for a fixed number of items. - Prompt: Inside the loop, use an
Inputsymbol to prompt the user for a value. - Assign: Store the user's input directly into the array using the counter variable as the index (e.g.,
my_array[i]). RAPTOR automatically creates or resizes the array during this assignment. - Increment: After the assignment, increment the counter variable to move to the next position in the array.
- Repeat: The loop continues until the exit condition is met, at which point the array is fully populated with user-provided data.