0% found this document useful (0 votes)
4 views

arrays

An array is a collection of elements of the same data type, accessed by an index. It allows for the storage and management of multiple values, with examples including one-dimensional arrays and methods for accessing and manipulating data. Key points include indexing, data type consistency, and exercises for practical application.

Uploaded by

Nourhene Aouidi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

arrays

An array is a collection of elements of the same data type, accessed by an index. It allows for the storage and management of multiple values, with examples including one-dimensional arrays and methods for accessing and manipulating data. Key points include indexing, data type consistency, and exercises for practical application.

Uploaded by

Nourhene Aouidi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Explanation of Arrays

An array is a collection of elements, all of which must be of the same data type (e.g.,
integers, strings, etc.). The elements in the array are stored under a single identifier, and
each element can be accessed using an index (or position) within the array.

Here’s how arrays work, explained in detail:

1. Basic Array Concept

An array allows you to store multiple values, but unlike variables, which hold a single
value, an array can hold many values. You use an array when you need to store and
manage a collection of data.

For example:

• If you want to store multiple integers, you can use an array. If you want to store
multiple colors as strings, you can use an array for strings.

2. One-Dimensional Arrays

A 1-dimensional array is the simplest type of array. It has only one "row" of data, which
can be thought of as a single list of items.

For example:

Index: 0 1 2 3 4
Data: [10, 5, 2, 90, 3]

This array has 5 spaces (or slots), each identified by an index. The index starts from 0.
The first item, 10, is stored at index 0, the second item, 5, is stored at index 1, and so
on.

3. Accessing and Manipulating Arrays

Accessing Data in Arrays

To access an element from an array, you use its index inside square brackets.
• Array[0] gives you the first element.
• Array[3] gives you the fourth element (since indexes start from 0).

For example:

Colours = ["red", "blue", "green", "yellow"]

If you want to access the first color:

Colours[0] // Outputs: "red"

Example 1: Store Data in an Array

You store data in an array by specifying the array name and the index (position) where
you want to store the data.

Colour[0] = "red" // Store "red" in the first position of the array

Example 2: Access Data from an Array

To access and display a value from an array, you use its index.

OUTPUT(Colours[0]) // This will output "red"

Using Variables as Indices

Instead of directly using a number to access an array, you can use a variable. The value
of the variable will determine which element of the array is accessed.

For example:

OUTPUT("Enter the array element you want to output")


INPUT ToOutput // User inputs a number
OUTPUT(Colours[ToOutput]) // Outputs the color at the index
specified by the user
4. Example: Store Numbers in an Array

If you want to store multiple numbers in an array, you can use a loop to input those
numbers from the user.

FOR counter = 0 TO 19
OUTPUT("Enter a number:")
INPUT Numbers[counter] // Store each number in the Numbers
array at position 'counter'
NEXT counter

Here’s what happens:

• The loop will run 20 times (from counter = 0 to counter = 19).


• Each time, it will ask the user to input a number and store that number in the
Numbers array at the current index.

Key Points about Arrays

• Indexing: The index of an array is the position of each element. The index
typically starts at 0 but can start at 1 in some languages.
• Array Elements: All elements in the array must be of the same data type (e.g., all
integers, all strings, etc.).
• Array Length: The size of the array is determined by the number of elements it
can hold. This is called the length of the array.
• Accessing: To access an element, use the array name and the index inside
square brackets.
• Updating Elements: You can update an element in the array by assigning a new
value to an index.

Exercises for Practice:

1. Exercise 1: Create an array named Scores and store 5 integer values in it (e.g.,
85, 90, 75, 88, 92). Then, output the second and fourth elements in the array.
2. Exercise 2: Write a program that stores 10 numbers input by the user into an
array and then outputs the sum of all the numbers.
3. Exercise 3: Create an array of 5 strings (e.g., ["apple", "banana", "cherry", "date",
"elderberry"]). Ask the user to input a number between 0 and 4, and output the
corresponding fruit from the array based on the number.
4. Exercise 4: Write a program where the user enters 5 numbers into an array.
Then, output the largest number from the array.

Here are the solutions to the exercises I gave you:

Exercise 1:

Task: Create an array named Scores and store 5 integer values in it (e.g., 85, 90, 75, 88,
92). Then, output the second and fourth elements in the array.

Solution:

// Declare and initialize the Scores array with 5 integer values


Scores = [85, 90, 75, 88, 92]

// Output the second element (index 1) and the fourth element (index
3)
OUTPUT(Scores[1]) // Outputs: 90
OUTPUT(Scores[3]) // Outputs: 88

Exercise 2:

Task: Write a program that stores 10 numbers input by the user into an array and then
outputs the sum of all the numbers.

Solution:

// Declare an array to store the 10 numbers


Numbers = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

// Ask the user to input 10 numbers and store them in the array
FOR counter = 0 TO 9
OUTPUT("Enter a number:")
INPUT Numbers[counter]
NEXT counter

// Calculate the sum of all the numbers


Sum = 0
FOR counter = 0 TO 9
Sum = Sum + Numbers[counter]
NEXT counter

// Output the sum


OUTPUT("The sum of the numbers is: " + Sum)

Explanation:

• We use a FOR loop to input 10 numbers from the user and store them in the array
Numbers.
• Then, we use another FOR loop to add all the numbers together and store the
result in the Sum variable.
• Finally, the sum is displayed.

Exercise 3:

Task: Create an array of 5 strings (e.g., ["apple", "banana", "cherry", "date",


"elderberry"]). Ask the user to input a number between 0 and 4, and output the
corresponding fruit from the array based on the number.

Solution:

// Declare and initialize the array of fruits


Fruits = ["apple", "banana", "cherry", "date", "elderberry"]

// Ask the user to input a number between 0 and 4


OUTPUT("Enter a number between 0 and 4:")
INPUT Index

// Output the corresponding fruit from the array


IF Index >= 0 AND Index <= 4 THEN
OUTPUT("The selected fruit is: " + Fruits[Index])
ELSE
OUTPUT("Invalid input! Please enter a number between 0 and 4.")
ENDIF

Explanation:
• The array Fruits contains 5 strings.
• We ask the user to input a number between 0 and 4.
• If the number is within range, we output the corresponding fruit from the array.
Otherwise, we show an error message.

Exercise 4:

Task: Write a program where the user enters 5 numbers into an array. Then, output the
largest number from the array.

Solution:

// Declare an array to store the 5 numbers


Numbers = [0, 0, 0, 0, 0]

// Ask the user to input 5 numbers and store them in the array
FOR counter = 0 TO 4
OUTPUT("Enter a number:")
INPUT Numbers[counter]
NEXT counter

// Find the largest number in the array


Largest = Numbers[0] // Start by assuming the first number is the
largest
FOR counter = 1 TO 4
IF Numbers[counter] > Largest THEN
Largest = Numbers[counter]
ENDIF
NEXT counter

// Output the largest number


OUTPUT("The largest number is: " + Largest)

Explanation:

• The array Numbers is initialized to store 5 values.


• We input 5 numbers from the user and store them in the array.
• We start by assuming that the first number is the largest. Then, we compare
each of the remaining numbers with the current largest value and update
Largest if a bigger number is found.
• Finally, we output the largest number.

You might also like