0% found this document useful (0 votes)
3 views16 pages

Arrays

The document explains the concept of arrays in programming, highlighting their structure, types, and practical applications. It covers how to declare, assign values, and read from one-dimensional arrays using loops. Additionally, it provides examples to illustrate the use of arrays for managing multiple data items efficiently.

Uploaded by

Faraz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Arrays

The document explains the concept of arrays in programming, highlighting their structure, types, and practical applications. It covers how to declare, assign values, and read from one-dimensional arrays using loops. Additionally, it provides examples to illustrate the use of arrays for managing multiple data items efficiently.

Uploaded by

Faraz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Arrays

Objectives
i. Declare and use one-dimensional arrays

ii. Show understanding of the use of one-dimensional arrays,


including the use of a variable as an index in an array

iii. Read or write values in an array using a FOR … TO … NEXT loop


What are arrays?
• Array in programming is a series or list of objects that have the same type.

• Array is a systematic arrangement of objects of same data type.

• It is a special kind of variable like integer, character and Boolean etc.

• Array is a data structure consisting of collection of same

• elements/data types and each element is identified by a unique index.


Example/Use in daily life
You use arrays all of the time in your daily lives.

• Whenever you need to keep track of an ordered list of items

• A playlist of songs

• A list of each keystroke a user clicks.


Types of an Array?
• Single dimension Arrays: A one-dimensional array is a type of
linear array. Example: List of student names, List of student marks etc.

• Multiple dimension Arrays: Multidimensional array represents


multiple data items as a table consisting of multiple rows and
columns
Structure of array
• Point To Remember
• In many programing languages the index starts from ZERO.so if we
declare an array of 10 elements, it means the index will start from 0
till 9.
• Once the Array length is declared it can’t be changed.

Example to understand
This is a four story building.
Ground floor and three floors.
Ground floor is considered as 1st ,first floor as 2nd , second floor as 3rd
and third floor as 4th .
Similarly the first index (0) in an array is considered as 1st position
/Location where element/data will be stored.
Example to understand the need of Array :
Pseudo code to Input test scores of 3 students

• Input score1
• Input score2
• Input score3
Need to declare three variables to input the values.

Question :Pseudo code to input test scores of 10 students

What if you want to input scores of 2000 students ?


• 2000 different variables will be declared
• 2000 codes will be written
• Program will become more complex and lengthy

To avoid all these complexities ,use of an Array is a suitable option
Declaration of an Array
1. Name of the array
2. Type of data the array will hold
3. How many data elements will it store

DECLARE <identifier> : ARRAY [<1>:<n>] of <data type>

Example :
In previous example, test scores of students were needed to input , test
score is an integer type of data and it was required to save 10 elements, so
the array will be declare as :
DECLARE testscore[1:10] of integer

where testscore =name of Arrray(Identifier)


int = integer data type
[10] = length of array
Declare the following arrays

1. Array of names of 1000 students

2. Array of 300 grocery items

3. Array of 15 sections of class 10


Assigning Values in an Array
We have declared an array in previous example using code
testscore int [10]
0 1 2 3 4 5 6 7 8 9

Now this code will assign value to specific index/location


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

Similarly testscore [5] = 8


0 1 2 3 4 5 6 7 8 9
8
Assigning Values in an Array
Keeping in mind testscore int [10]

1. Assign value 200 at index 7

2. Assign value 35 at index 3

3. Assign value 26 at index 6


Assigning Values in an Array

Input values with the help of For…to…Next loop

testscore int [10] (declaration)

For count = 1 to 10 (For loop)

Input testscore [count ] (input values in array)

Next count
Read / output / Print values from Array
Example
0 1 2 3 4 5 6 7 8 9
6 12 34 65 78 90 44 34 87 23

To read data elements from array we use PRINT /output command


Now if we want to read index 6 or want to know what value is saved at
position 6, we use following command
Print testscore [6]

If you want to read what is saved at position 8 following command will be


used
Print testscore [8]
Read / output values from Array

For ...to. Next loop can be used to read the entire array. The program will be :

For count = 1 to 10
Print testscore [count]
Next count

Complete pseud code to input 10 test scores of students and display on the screen
using an Array

Testscore int [10]


For count = 1 to 10
Input testscore [count] Declaration and input values in array
Next count

For count = 1 to 10
Print testscore [count] Read from Array and display
Next count
Any Question?

You might also like