Top Banner
Arrays Trupti Agrawal 1
18
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Arrays

Arrays

Trupti Agrawal 1

Page 2: Arrays

ARRAYS The Array is the most commonly used Data Structure.

An array is a collection of data elements that are of the same type (e.g.,a collection of integers, collection of characters, collection of doubles).

OR Array is a data structure that represents a collection of the same types

of data.

The values held in an array are called array elements

An array stores multiple values of the same type – the element type

The element type can be a primitive type or an object reference

Therefore, we can create an array of integers, an array of characters, anarray of String objects, an array of Coin objects, etc.

Trupti Agrawal 2

Page 3: Arrays

Array Applications Given a list of test scores, determine the maximum and minimum scores.

Read in a list of student names and rearrange them in alphabetical order(sorting).

Given the height measurements of students in a class, output the names ofthose students who are taller than average.

Trupti Agrawal 3

Page 4: Arrays

Array Declaration Syntax:

<type> <arrayName>[<array_size>]

Ex. int Ar[10];

The array elements are all values of the type <type>.

The size of the array is indicated by <array_size>, the numberof elements in the array.

<array_size> must be an int constant or a constant expression.Note that an array can have multiple dimensions.

Trupti Agrawal 4

Page 5: Arrays

Array Declaration// array of 10 uninitialized ints

int Ar[10];

-- -- ----Ar -- -- ---- -- --

4 5 630 2 8 971

0 1 2 3 4 5Trupti Agrawal 5

Page 6: Arrays

Subscripting Declare an array of 10 integers:

int Ar[10]; // array of 10 ints

To access an individual element we must apply a subscript to array namedAr. A subscript is a bracketed expression.

The expression in the brackets is known as the index. First element of array has index 0.

Ar[0] Second element of array has index 1, and so on.

Ar[1], Ar[2], Ar[3],… Last element has an index one less than the size of the array.

Ar[9]

Incorrect indexing is a common error.

Trupti Agrawal 6

Page 7: Arrays

Subscripting// array of 10 uninitialized ints

int Ar[10];

Ar[3] = 1;

int x = Ar[3];

-- -- 1--Ar -- -- ---- -- --

4 5 630 2 8 971

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

Trupti Agrawal 7

Page 8: Arrays

Subscripting Example#include <stdio.h>

int main ()

{

int n[10]; /* n is an array of 10 integers */

int i,j;

/* initialize elements of array n to 0 */

for ( i = 0; i < 10; i++ )

{

n[ i ] = i + 100; /* set element at location i to i + 100 */

}

/* output each array element's value */

for (j = 0; j < 10; j++ )

{

printf("Element[%d] = %d\n", j, n[j] );

}

return 0;

}

Trupti Agrawal 8

Page 9: Arrays

Multidimensional Arrays An array can have many dimensions – if it has more than one dimension, it

is called a multidimensional array

Each dimension subdivides the previous one into the specified number of elements

Each dimension has its own length constant

Because each dimension is an array of array references, the arrays within one dimension can be of different lengths

Trupti Agrawal 10

Page 10: Arrays

Multiple dimensions get difficult to visualize graphically.

double Coord[100][100][100];

Multidimensional Arrays2 Dimensional 3 Dimensional

Trupti Agrawal 11

Page 11: Arrays

Processing Multi-dimensional ArraysThe key to processing all cells of a multi-dimensional array is nested loops.

[0] [1] [2] [3]

[0]

[1]

[2]

10 11 12 13

14 15 16 17

18 19 20 21

22 23 24 25

26 27 28 29

[3]

[4]

for (int row=0; row!=5; row++) {

for (int col=0; col!=4; col++) {

System.out.println( myArray[row][col] );

}

}

for (int col=0; col!=4; col++) {

for (int row=0; row!=5; row++) {

System.out.println( myArray[row][col] );

}

}

Trupti Agrawal 12

Page 12: Arrays

Two-Dimensional Arrays

two

dimensions

one

dimension

• A one-dimensional array stores a list of elements

• A two-dimensional array can be thought of as a table of elements, with rows and columns

Trupti Agrawal 13

Page 13: Arrays

Two-Dimensional Arrays [Cont…]A two-dimensional array consists of a certain number of rows and columns:

const int NUMROWS = 3;const int NUMCOLS = 7;int Array[NUMROWS][NUMCOLS];

Array[2][5] 3rd value in 6th column Array[0][4] 1st value in 5th column

The declaration must specify the number of rows and the number of columns, and both must be constants.

0 1 2 3 4 5 6

0 4 18 9 3 -4 6 0

1 12 45 74 15 0 98 0

2 84 87 75 67 81 85 79

Trupti Agrawal 14

Page 14: Arrays

Address calculation using column

and row major ordering In computing, row-major order and column-major order describe methods

for storing multidimensional arrays in linear memory.

By following standard matrix notation, rows are numbered by the first indexof a two-dimensional array and columns by the second index.

Array layout is critical for correctly passing arrays between programswritten in different languages. It is also important for performance whentraversing an array because accessing array elements that are contiguous inmemory is usually faster than accessing elements which are not, dueto caching.

Trupti Agrawal 15

Page 15: Arrays

Row-major order In row-major storage, a multidimensional array in linear memory is

organized such that rows are stored one after the other. It is theapproach used by the C programming language, among others.

For example, consider this 2×3 array:

An array declared in C as

int A[2][3] = { {1, 2, 3}, {4, 5, 6} };

is laid out contiguously in linear memory as:

1 2 3 4 5 6

1 2 3

4 5 6

Trupti Agrawal 16

Page 16: Arrays

Row-major order[Cont…] To traverse this array in the order in which it is laid out in memory, one

would use the following nested loop:

for (row = 0; row < 2; row++)

for (column = 0; column < 3; column++)

printf("%d\n", A[row][column]);

Trupti Agrawal 17

Page 17: Arrays

Column-major order Column-major order is a similar method of flattening arrays

onto linear memory, but the columns are listed in sequence.

The scientific programming languages Fortran and Julia, thematrix-oriented languages MATLAB and Octave, use column-major ordering. The array

if stored contiguously in linear memory with column-majororder looks like the following:

1 4 2 5 3 6

1 2 3

4 5 6

Trupti Agrawal 18

Page 18: Arrays

THANK YOU….. !!!

Trupti Agrawal 19