28
1 Topic: Array Topic: Array

1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

Embed Size (px)

Citation preview

Page 1: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

11

Topic: ArrayTopic: Array

Page 2: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

22

ArraysArrays

In this chapter, we will :In this chapter, we will : Learn about arraysLearn about arrays Explore how to declare and manipulate data into Explore how to declare and manipulate data into arraysarrays

Become familiar with the restrictions on array Become familiar with the restrictions on array processingprocessing

Discover how to pass an array as a parameter to a Discover how to pass an array as a parameter to a functionfunction

Example of 1-D arrayExample of 1-D array Discover how to manipulate data in a two-Discover how to manipulate data in a two-dimensional arraydimensional array

Example of 2-D arrayExample of 2-D array HomeworkHomework

Page 3: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

33

A data type is called A data type is called simplesimple if variables of that type can store if variables of that type can store only one value at a time.only one value at a time.

A A structured data typestructured data type (or data structure) is one in which (or data structure) is one in which each data item is a collection of other data items.each data item is a collection of other data items.

An An arrayarray is a collection of a fixed number of components is a collection of a fixed number of components wherein all of the components are of the same data type. wherein all of the components are of the same data type.

A group of same data type is called array.A group of same data type is called array. An array is of three types:An array is of three types: a)One Dimensional arraya)One Dimensional array b)Two Dimensional arrayb)Two Dimensional array c)Multi Dimensional arrayc)Multi Dimensional array one-dimensional arrayone-dimensional array is an array in which the components is an array in which the components

are arranged in a list form. are arranged in a list form. The general form of declaring a The general form of declaring a one-dimensional array is:one-dimensional array is:

DataType arrayName[intExp];DataType arrayName[intExp];

Page 4: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

44

ExampleExample: :

The statement The statement intint list[10]; list[10];

declares an array declares an array listlist of of 1010 components. Each component is of the components. Each component is of the type type intint. The components are . The components are list[0]list[0],, list[1] list[1]… list… list[9]. [9].

Page 5: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

55

Accessing Array ComponentsAccessing Array Components

The general form (syntax) of accessing an array component is:The general form (syntax) of accessing an array component is:

arrayName[indexExp]arrayName[indexExp]

where where indexExpindexExp, called , called indexindex, is any expression whose value is a , is any expression whose value is a nonnegative integer. nonnegative integer.

Index value specifies the position of the component in the array.Index value specifies the position of the component in the array. In C++, In C++, [][] is an operator, called the is an operator, called the array subscripting operatorarray subscripting operator.. In C++ array index starts at In C++ array index starts at 00..

Page 6: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

66

list[5] = 34;list[5] = 34;

Page 7: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

77

Processing One-Dimensional ArraysProcessing One-Dimensional Arrays

Some of the basic operations performed on a one-dimensional array Some of the basic operations performed on a one-dimensional array are initialize, input data, output data stored in an array, find the are initialize, input data, output data stored in an array, find the largest and/or smallest element. largest and/or smallest element.

Each of these operations requires the ability to step through the Each of these operations requires the ability to step through the elements of the array. elements of the array.

Stepping-through the elements of an array is easily accomplished by Stepping-through the elements of an array is easily accomplished by a loop. a loop.

Page 8: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

88

Consider a list declared as an array of the SIZE 100Consider a list declared as an array of the SIZE 100

The following The following forfor loop steps-through each element of the loop steps-through each element of the array array listlist starting at the first element of starting at the first element of listlist..

forfor(i = 0; i < SIZE; i++)(i = 0; i < SIZE; i++) process list[i]process list[i]

If processing If processing listlist requires inputting data into requires inputting data into listlist, the , the statement in Line 2 takes the from of an input statement, statement in Line 2 takes the from of an input statement, such as the such as the cincin statement. statement.

forfor(i = 0; i < SIZE; i++)(i = 0; i < SIZE; i++) cin>>list[i];cin>>list[i];

Page 9: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

99

Finding the sum and average of an array:Finding the sum and average of an array:

sum = 0;sum = 0;

forfor(index = 0; index < SIZE; index ++)(index = 0; index < SIZE; index ++)

sum = sum + list[index];sum = sum + list[index];

average = sum / SIZE;average = sum / SIZE;

Page 10: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1010

Array ProblemsArray Problems

In C++, there is no guard against indices that are out of In C++, there is no guard against indices that are out of bounds. bounds.

Assignment, comparison of arrays, reading data into an Assignment, comparison of arrays, reading data into an array and printing the contents of an array must be done array and printing the contents of an array must be done component-wise. component-wise.

cin>>x; cin>>x; //illegal//illegal

if (x < y) …; if (x < y) …; //illegal//illegal

y = x; y = x; // illegal// illegal In order to copy one array into another array use a loop.In order to copy one array into another array use a loop.

forfor(j = 0; j < arraySize; j++)(j = 0; j < arraySize; j++)

y[j] = x[j];y[j] = x[j];

Page 11: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1111

Arrays as Parameters to FunctionsArrays as Parameters to Functions

By Reference Only:By Reference Only: In In C++,C++, arrays are passed by reference only. arrays are passed by reference only.

Since arrays are passed by reference only, we do not use the symbol Since arrays are passed by reference only, we do not use the symbol && when declaring an array as a formal parameter. when declaring an array as a formal parameter.

voidvoid initialize(datatype list, initialize(datatype list, intint SIZE) SIZE){{intint count; count;forfor(count = 0; count < SIZE; count++)(count = 0; count < SIZE; count++)

list[count] = 0;list[count] = 0;}}

Page 12: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1212

Example of 1D arrayExample of 1D array

To print the elements of a list in acsending orderTo print the elements of a list in acsending order#include <iostream.h>#include <iostream.h>#include <conio.h>#include <conio.h>void bubble_sort(int a[], int N)void bubble_sort(int a[], int N){{

int i, j, temp;int i, j, temp;for (i = 0; i < N; i++)for (i = 0; i < N; i++)

for (j = 0 ; j < N-1; j++)for (j = 0 ; j < N-1; j++)}}

Page 13: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1313

if (a[j] > a[j+1])if (a[j] > a[j+1])

{{

temp = a[j];temp = a[j];

a[j] = a[j+1];a[j] = a[j+1];

a[j+1] = temp;a[j+1] = temp;

}}

Page 14: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1414

void main()void main(){{

int A[10];int A[10];clrscr();clrscr();cout << "Enter the array elements : ";cout << "Enter the array elements : ";for (int i = 0; i<10; i++)for (int i = 0; i<10; i++)

cin >> A[i];cin >> A[i];bubble_sort(A, 10);bubble_sort(A, 10);cout << "Sorted elements are : ";cout << "Sorted elements are : ";for (i = 0; i<10; i++)for (i = 0; i<10; i++)

cout << A[i] << "\n";cout << A[i] << "\n";

}}

Page 15: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1515

Two-dimensional ArraysTwo-dimensional Arrays

A A two-dimensional arraytwo-dimensional array is a collection of a fixed number of is a collection of a fixed number of components arranged in two dimensions, and all components are of the components arranged in two dimensions, and all components are of the same type.The syntax for declaring a two-dimensional array is:same type.The syntax for declaring a two-dimensional array is:

DataType arrayName[intexp1][intexp2];DataType arrayName[intexp1][intexp2];

where where intexp1intexp1 and and intexp2intexp2 are expressions yielding positive are expressions yielding positive integer values. The two expressions integer values. The two expressions intexp1intexp1 and and intexp2intexp2 specify specify the number of rows and the number of columns, respectively, in the the number of rows and the number of columns, respectively, in the array. array.

doubledouble sales[10][5]; sales[10][5];

declares a two-dimensional array declares a two-dimensional array salessales of of 1010 rows and rows and 55 columns columns and every component is of the type and every component is of the type floatfloat. .

Page 16: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1616

Page 17: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1717

Accessing Array ComponentsAccessing Array ComponentsThe syntax to access a component of a two-dimensional array isThe syntax to access a component of a two-dimensional array is

arrayName[indexexp1][indexexp2]arrayName[indexexp1][indexexp2]

where where indexexp1indexexp1 and and indexexp2indexexp2 are expressions yielding are expressions yielding nonnegative integer values. nonnegative integer values. indexexp1indexexp1 specifies the row position specifies the row position and and indexexp2indexexp2 specifies the column position. specifies the column position.

The statementThe statement

sales[5][3] = 25.75;sales[5][3] = 25.75;

stores stores 25.7525.75 into row numbered into row numbered 55 and column numbered and column numbered 33 (that is, (that is, 6th6th row and row and 4th4th column) of the array column) of the array salessales. If. If

intint i = 5; i = 5;intint j = 3; j = 3;

then the previous statement is equivalent to then the previous statement is equivalent to

sales[i][j] = 25.75;sales[i][j] = 25.75;

Page 18: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1818

Page 19: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

1919

Processing Two-dimensional ArraysProcessing Two-dimensional Arrays

A two-dimensional array can be processed in three different ways.A two-dimensional array can be processed in three different ways.

1. Process the entire array.1. Process the entire array.

2. Process a particular row of the array, called 2. Process a particular row of the array, called row processingrow processing..

3. Process a particular column of the array, called 3. Process a particular column of the array, called column processingcolumn processing..

When processing a particular row or column of a two-dimensional When processing a particular row or column of a two-dimensional array we employ algorithms similar to when we process one-array we employ algorithms similar to when we process one-dimensional arrays.dimensional arrays.

Page 20: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2020

Suppose that we want to process row number, say Suppose that we want to process row number, say 55 of of matrixmatrix. The components of row number . The components of row number 55 of of matrixmatrix are: are:

matrix [0][1][2][3][4][5][0]

[1]

[2]

[3]

[4]

[5]

[6]

matrix[5][0]

matrix[5][1]

matrix[5][2]

matrix[5][3]

matrix[5][4]

matrix[5][5]

Page 21: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2121

const intconst int ROWS = 7; ROWS = 7; const intconst int COLUMNS = 6; COLUMNS = 6; intint matrix[ROWS][COLUMNS]; matrix[ROWS][COLUMNS];

intint row; row;intint col; col;intint sum; sum;intint largest; largest;intint temp; temp;

//In these components the first index is fixed at //In these components the first index is fixed at 55. . row = 5;row = 5;//The second index ranges from //The second index ranges from 00 to to 55. . forfor(col = 0; col < COLUMNS; col++)(col = 0; col < COLUMNS; col++)

process matrix[row][col]process matrix[row][col]

Page 22: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2222

Similarly, suppose that we want to process column number Similarly, suppose that we want to process column number 22 of of matrixmatrix, that is, the , that is, the thirdthird column of column of matrixmatrix. The components of . The components of this column are:this column are:

matrix [0][1][2][3][4][5][0]

[1]

[2]

[3]

[4]

[5]

[6]

matrix[3][2]

matrix[4][2]

matrix[5][2]

matrix[6][2]

matrix[0][2]

matrix[1][2]

matrix[2][2]

Page 23: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2323

The second index is fixed at The second index is fixed at 22 and the first index ranges from and the first index ranges from 00 to to 66. . The following The following forfor loop processes column loop processes column 22 of of matrixmatrix..

col = 2;col = 2;

forfor(row = 0; row < ROWS; row++)(row = 0; row < ROWS; row++)

process matrix[row][col]process matrix[row][col]

Page 24: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2424

PrintPrint

The following nested The following nested forfor loops print the components of matrix, one loops print the components of matrix, one row per line.row per line.

forfor(row = 0; row < ROWS; row++)(row = 0; row < ROWS; row++)

{{

forfor(col = 0; col < COLUMNS; col++)(col = 0; col < COLUMNS; col++)

cout<<setw(5)<<matrix[row][col]<<" ";cout<<setw(5)<<matrix[row][col]<<" ";

cout<<endl;cout<<endl;

}}

Page 25: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2525

Passing Two-dimensional Arrays as Parameters to Passing Two-dimensional Arrays as Parameters to FunctionsFunctions

• Two-dimensional arrays can be passed as parameters to a function Two-dimensional arrays can be passed as parameters to a function and they are passed by reference. and they are passed by reference.

• When storing a two-dimensional array in the computer’s memory, When storing a two-dimensional array in the computer’s memory, C++C++ uses the uses the row order form row order form. That is, first the first row is stored, . That is, first the first row is stored, followed by the second row, which is followed by the third row followed by the second row, which is followed by the third row and so on.and so on.

voidvoid initialize(datatype table) initialize(datatype table){{ intint row; row; intint col; col;

forfor(row = 0; row < ROWS; row++)(row = 0; row < ROWS; row++) forfor(col = 0; col < COLUMNS; col++)(col = 0; col < COLUMNS; col++)

table[row][col] = 0;table[row][col] = 0;}}

Page 26: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2626

Example of 2-D arrayExample of 2-D array

To print To print Sum of Column of a matrixSum of Column of a matrix

void Col_sum(int R[7][7])void Col_sum(int R[7][7]){{

int sum, i, j;int sum, i, j;for (i=0; i<7; i++)for (i=0; i<7; i++){{

sum = 0;sum = 0;for (j = 0; j<7; j++)for (j = 0; j<7; j++)

sum = sum + R[j][i];sum = sum + R[j][i];cout << "Sum of Column : " << i+1 << " is : " << sum << endl;cout << "Sum of Column : " << i+1 << " is : " << sum << endl;

}}}}

Page 27: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2727

void main()void main(){{

clrscr();clrscr();int ar[7][7];int ar[7][7];int i, j;int i, j;for (i=0; i<7; i++)for (i=0; i<7; i++)

for (j = 0; j<7; j++)for (j = 0; j<7; j++)cin >> ar[i][j];cin >> ar[i][j];

Col_sum(ar);Col_sum(ar);}}

Page 28: 1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data

2828

HomeworkHomework

Write a program in C++ to arrange the Write a program in C++ to arrange the elements of a list in descending order. (1-D elements of a list in descending order. (1-D array).array).

Write a program in C++ to enter elements inWrite a program in C++ to enter elements in

2-D matrix (3x3) and display the matrix on 2-D matrix (3x3) and display the matrix on the output screen.the output screen.