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

Lecture 3b

The document discusses arrays including defining arrays, accessing array elements using indexes, one-dimensional and multi-dimensional arrays, and examples of declaring and initializing arrays in different programming languages. Arrays are defined as a collection of similar data types stored in contiguous memory locations that can be accessed using indexes.

Uploaded by

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

Lecture 3b

The document discusses arrays including defining arrays, accessing array elements using indexes, one-dimensional and multi-dimensional arrays, and examples of declaring and initializing arrays in different programming languages. Arrays are defined as a collection of similar data types stored in contiguous memory locations that can be accessed using indexes.

Uploaded by

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

PROGRAMMING

TECHNIQUES
Lecture 1c
Lecture Outline
■ Arrays
– Accessing an Array
– Types of Arrays
Arrays
■ An array is a collection of similar type of objects or
elements stored in a contiguous memory location.
– Arrays are a collection of variables of the same type
and referenced by the same name.
■ They can also be referred to as a list of related values.
■ The individual elements or objects of an array have the
same data type but have a different position in memory.
Example:
Declare Age[6] as Integer
Age [ ] = {5, 10, 15, 20, 25, 30}
Syntax for Declaring and Creating Arrays
■ One dimensional array:
datatype arrayName [size declarator]

■ Multi-dimensional array:
datatype arrayName [size declarator] [size declarator]

■ The datatype specifies the type of data we want to save in


the array.
– Type can be a float, integer, string, character, etc.
Arrays
■ The ARRAY NAME refers to how we can identify the array.
■ The SIZE DECLARATOR of the array specifies the number
of elements the array can hold or save.
■ Each element saved in an array has an index.
– Index may refer to the uniquely reserved spaces
specified by the array’s size declarator for an element
to be saved.
■ Arrays have an index of zero (0) to one less than the
number of elements (i.e., number of elements - 1) of the
array.
Example
■ One-dimensional arrays:
– Pseudocode: Declare scores [10] as Integer
– Python: scores = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
– C: int scores [10];
– Java: int score [] = new int [10];
■ Multi-dimensional arrays:
– Pseudocode: Declare scores [10][10] as integer
– Java: int [][] scores = new int [10][10]
– Java int [][] b = {{1, 2}, {3, 4}}
Array Index Explained
■ The position number of the
element is called the
element’s index.
■ Example:
– This is an integer array
called c.
– This array contains 12
elements.
– The first element in every
array has index zero
Accessing an Array
■ You can refer to any one of the elements in an array with
an expression that includes the name of the array
followed by the index of the particular element in square
brackets ([]).
– The index or subscript of an array indicates the
position of an element in the array.
■ The syntax for accessing an array element is:
arrayName[index];
■ Example:
– scores[3], Names[0], marks[10], rooms[2], etc.
Example
1. Start
2. Declare num [4] as integer
3. Set num [0] = 5
4. Set num [1] = 10
5. Set num [2] = 15
6. Set num [3] = 20
7. Write num [0]
8. Write num [1] , num [2], num [3]
9. End
One Dimensional Arrays
■ A one-dimensional array is a list of related data of the
same type referred to by a single variable name with a
single index number to identify each item.
■ Example:
– Pseudocode: Set scores [4] = {3, 6, 8, 2}
– Python: scores = [1, 2, 5, 4, 3]
– Java/C/C++: char grades [] = ‘A’,’B’,’C’,’D’,’E’,’F’;
Example
■ To input the final exam scores of a class of 25 students
1. Start
2. Declare scores [25] as Integer
3. Declare k as integer
4. For (k = 0; k < 25; k++)
5. Write “Enter score: ”
6. Input score[k]
7. End for
8. End
Multi-Dimensional Arrays (Matrix)
■ Each element in the array is represented by two
subscripts.
■ A two dimensional m by n array A has m rows and n
columns and contains m*n elements.
■ It is also called matrix array because in it the elements
form a matrix.
■ Example:
– scores [2] [3] has 2 rows and 3 columns, hence 2*3 =
6 elements.
Example
1. Start
2. Declare scores [2][2] as Integer
3. Set scores[0][0] = 1
4. Set scores[0][1] = 2
5. Set scores[1][0] = 3
6. Set scores[1][1] = 4
7. Write scores[0][0]
8. Write scores[1][1]
9. End
Example: loading multi-dimensional arrays
1. Start
2. Declare scores [2][2] as Integer
3. Declare k, x as Integer
4. For (k = 0; k < 2; K++)
5. Write “Enter score “+ (k + 1)
6. For (x = 0; x < 2; x++)
7. Input score[k][x]
8. End for
9. End for
10.End
Advantages of Using Arrays
■ Arrays can reduce the number of variable names needed
in a program because we can use a single array instead
of a collection of simple variables to store related data.
■ Also arrays can help create more efficient programs.
– Once data are entered into an array, that data can be
processed many times without having to be input
again.
■ Arrays save time and effort.
■ Arrays make programming easier and conise

You might also like