array class 3

11
ARRAY

Upload: dinesh-kumar

Post on 05-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 1/11

ARRAY

Page 2: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 2/11

Array  –  About… 

• An array is a series of elements of the same

type placed in contiguous memory locations.

• Individually referenced by an index.

type name [size];

A subscript can be an integer expression too,• foo[17] foo[i+3] foo[a+b+c] 

Page 3: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 3/11

Types of Array

• One Dimensional Arrays

Two Dimensional Arrays

• Multi Dimensional Arrays3D Array, 4D Arrays etc.

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

A[0][0] A[0][1] A[0][2] A[0][3] A[0][4]

A[1][0] A[1][1] A[1][2] A[1][3] A[1][4

A[2][1] A[2][2] A[2][3] A[2][3] A[2][4]

Page 4: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 4/11

Declaration of Array

• One Dimensional Arrays: int A[10];

• Two Dimensional Arrays: char Y[5][25];

• Dynamic memory allocation:

int * bobby;

bobby = new int [5];

delete [] bobby; // Avoid memory Leak

Page 5: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 5/11

Initialization of the Array

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

int ARR[ ] = {1,2,3,4,5,6}; //Auto memory allocation

char B[20] = “Rudrapur ok”; // Like a String 

float C[5] = {2.4,3.5,1.5}; //Partial Initialization possible

int D[2][3] = {{1,2,3},{4,5,6}} //{{1,2},{3,4}} Remaining 0static int a[5]; // static declared to 0; auto =>Garbage..

• Memory Allocation with variable

const int x=5; // volatile variable not allowedint a[x]; // only const allowed

• In C++ first element of the array is always at zero position.

Page 6: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 6/11

Arrays and pass-by-reference

void print_array(int a[], int len)

{

int j;

for(int i=0;i<len;i++){

cout <<"["<<i<<“]=“<<a[i]<<“ a[i]+1”<<a[i]+1;}

}

• Inside a function any changes you make to array values arepermanent.

• You need to make sure that a function knows how big thearray is!!!

Page 7: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 7/11

Array Index Out of Bounds

• If either the index < 0 or the index >ARRAY_SIZE-1

 – then we say that theindex

is out of bounds.

• C++ does not check if the index value is within

range.

Assignment does not work with arrays» A = B // Not Works.. Use for loop instead

» cout<<A // Not Works.. Use for loop instead

Page 8: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 8/11

C Strings (Character Arrays)

• Character array - an array whose components are of typechar.

• String - a sequence of zero or more characters enclosed in

double quote marks. – Char a[25] = “Dinesh”; cout<<a // valid only for this

 – Contains 7 characters. ‘d’ ‘i’ ‘n’ ‘e’ ‘s’ ‘h’ ‘\0’ 

• c_str() // stringVariable.c_str()

 – Converts a value of the type string to a null-terminatedcharacter array 

Page 9: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 9/11

9

C++ strings

• A string is a null terminated array of 

characters.

 – null terminated means there is a character at the

end of the the array that has the value 0 (null).

• Pointers are often used with strings:char *msg = “RPI”; 

'R'

 msg

'P' 'I' 0

Page 10: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 10/11

Summary

• An array is a structured data type with a fixednumber of components

• Elements of a one-dimensional array are

arranged in the form of a list• An array index can be any expression that

evaluates to a non-negative integer

• The value of the index must always be lessthan the size of the array

• In C++, C-strings are null terminated and arestored in character arrays

Page 11: Array Class 3

7/31/2019 Array Class 3

http://slidepdf.com/reader/full/array-class-3 11/11

Summary (continued)

• In a function call statement, when passing an array asan actual parameter, you use only its name

• As parameters to functions, arrays are passed byreference only

• A function cannot return a value of the type array

• To access an element of a two-dimensional array, youneed a pair of indices: one for the row position andone for the column position

• In row/column processing, a two-dimensional array isprocessed one row/column at a time