This document discusses arrays in C++. It begins by introducing arrays and their need, then describes the different types of arrays including single, two, and multi-dimensional arrays. It explains how arrays are stored in contiguous memory locations and indexed starting from zero. The document also covers array initialization, unsized array initialization, and using strings as arrays in C++.
Introduction toArray Need of Array Types of Array Single dimensional Two dimensional Multi dimensional Array initialization Unsized Array initialization String as an array Contents
3.
Array is acollection of variables that can hold value of same type and reference by common name .Its is a derived data Structure Array are always stored in a continuous memory locations. An Array either be a integer ,character, or float base type (Data type of array) . Array indexing is always start from zero and highest address corresponds to last element Introduction
To store processedlarge number of variables of same data type and reference/name Easy understanding of program Example: Marks 0 1 2 3 4 5 Need ofArrray
A one dimensionalarray is one in which one subscript /indices specification is needed to specify a particular element of array Declaration : Data_type array_name [size of array ]; Eg: Int num[10]; 1-D Array
8.
num[0] num[1] num[2]num[3] num[4] num[5] num[6] num[7] num[8] num[9] 2000 2002 2004 2006 2008 2010 2012 2014 2016 2018 starting Address of location Total memory in bytes that an array is occupied : Size of array=size of array*size of(base type) Hear, 10*2=20 39 56 23 98 6 56 09 2 54 67 Memory representation:
9.
A 2-d arrayis an array in which each element is itself an array i.e int num[4][3] 0 1 2 0 1 2 3 2-D Array No of rows No of columns Num [2][1] No of element in 2-D array =M*N
10.
Total bytes= noof rows*no of columns*size of(base type) Memory reprsentation in 2-D array: char A [2][3] A[0][0] A[0][1] A[0][2] A[1][0] A[1][1] A[1][2] 5001 5002 5003 5004 5005 5006 size of 2-D array
11.
An array withdimensions more than two .The maximum limit of array is compiler dependent Declration: Data_type name [a][b][c][d][e][f]…….[n]; Array of 3 or more dimensional are not often use because of huge memory requirement and complexity involved Multi dimensionalarray
12.
C++ provides thefacility of array initialization at the time of declaration .General form of array initialization is as: Type array_name[size 1]…..[size N] ={vale list}; Eg: Int days_month[12]={31,25,29,03,31,19,20,31,18,20,31,29}; Char string[6]={‘a’,’r’,’g’,’y’,’d’,’0’}; 2-D array are also initialized in same way as linear array Int cube[4][2]={ 1,3, 4,6, 9,37, 5,78 }; Arrayinitialization
13.
C++ allowed youto skip the size of array in an array initialization statement C++ automatically create an array big enough to hold all the initializers present Char S1[] =“ first string ”; you skip the size, you must give list of initializers so that C++ can calculate the size of array Int val []={3,5,6,2,8,9,6,4}; Int cube [] [2] ={ 1,3, 67,7, 6,87, }; Unsizedarrayinitializations
14.
C++ does nothave a String data type ,it impairments string as 1-D character Arrray .A string as a character array is terminate by a null character ‘0’ Char str 1 [11]; Char square [][]={ ‘first string’, ‘second string’, ‘third string’ }; String as array