Arrays in C++ with programs, Study notes of Programming Languages

This lecture will cover your concepts on the topic of Arrays in C++.

Typology: Study notes

2016/2017

Uploaded on 01/30/2022

Ansari_01
Ansari_01 ๐Ÿ‡ต๐Ÿ‡ฐ

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter No#10
Arrays in C++
C++ provides a data structure,๎˜‘the array, which stores a fixed-size sequential
collection of elements of the same type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of the same
type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific element in
an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays
To declare an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows โˆ’
type arrayName [ arraySize ];
This is called a single-dimension array. The๎˜‘arraySize๎˜‘must be an integer constant
greater than zero and๎˜‘type๎˜‘can be any valid C++ data type. For example, to declare a
10-element array called balance of type double, use this statement โˆ’
double balance[10];
Initializing Arrays
You can initialize C++ array elements either one by one or using a single statement as
follows โˆ’
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } can not be larger than the number of
elements that we declare for the array between square brackets [ ]. Following is an
example to assign a single element of the array โˆ’
If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write โˆ’
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns element number 5th๎˜‘in the array a value of 50.0. Array
with 4th๎˜‘index will be 5th, i.e., last element because all arrays have 0 as the index of their
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Arrays in C++ with programs and more Study notes Programming Languages in PDF only on Docsity!

Chapter No#

Arrays in C++

C++ provides a data structure, the array , which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays

To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows โˆ’ type arrayName [ arraySize ]; This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement โˆ’ double balance[10];

Initializing Arrays

You can initialize C++ array elements either one by one or using a single statement as follows โˆ’ double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0}; The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array โˆ’ If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write โˆ’ double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0}; You will create exactly the same array as you did in the previous example. balance[4] = 50.0; The above statement assigns element number 5th^ in the array a value of 50.0. Array with 4th^ index will be 5th, i.e., last element because all arrays have 0 as the index of their

first element which is also called base index. Following is the pictorial representaion of the same array we discussed above โˆ’

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example โˆ’ double salary = balance[9]; The above statement will take 10th^ element from the array and assign the value to salary variable. Following is an example, which will use all the above-mentioned three concepts viz. declaration, assignment and accessing arrays โˆ’ #include using namespace std; #include using std::setw; int main () { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0 ; i < 10 ; i++ ) { n[ i ] = i + 100 ; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0 ; j < 10 ; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0 ; } This program makes use of setw() function to format the output. When the above code is compiled and executed, it produces the following result โˆ’ Element Value 0 100 1 101 2 102

Multi-dimensional Arrays

C++ allows multidimensional arrays. Here is the general form of a multidimensional array declaration โˆ’ type name[size1][size2]...[sizeN]; For example, the following declaration creates a three dimensional 5. 10. 4 integer array โˆ’ int threedim[5][10][4];

Two-Dimensional Arrays

The simplest form of the multidimensional array is the two-dimensional array. A two- dimensional array is, in essence, a list of one-dimensional arrays. To declare a two- dimensional integer array of size x,y, you would write something as follows โˆ’ type arrayName [ x ][ y ]; Where type can be any valid C++ data type and arrayName will be a valid C++ identifier. A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a , which contains three rows and four columns can be shown as below โˆ’ Thus, every element in array a is identified by an element name of the form a[ i ][ j ] , where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.

Initializing Two-Dimensional Arrays

Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns. int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 / {4, 5, 6, 7} , / initializers for row indexed by 1 / {8, 9, 10, 11} / initializers for row indexed by 2 */ };

The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example โˆ’ int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Accessing Two-Dimensional Array Elements

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example โˆ’ int val = a[2][3]; The above statement will take 4th^ element from the 3rd^ row of the array. You can verify it in the above digram. #include using namespace std; int main () { // an array with 5 rows and 2 columns. int a[ 5 ][ 2 ] = { { 0 , 0 }, { 1 , 2 }, { 2 , 4 }, { 3 , 6 },{ 4 , 8 }}; // output each array element's value for ( int i = 0 ; i < 5 ; i++ ) for ( int j = 0 ; j < 2 ; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout << a[i][j]<< endl; } return 0 ; } When the above code is compiled and executed, it produces the following result โˆ’ a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8 As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.

return 0 ; } When the above code is compiled and executed, it produces the following result โˆ’ Array values using pointer *(p + 0) : 1000 *(p + 1) : 2 *(p + 2) : 3. *(p + 3) : 17 *(p + 4) : 50 Array values using balance as address *(balance + 0) : 1000 *(balance + 1) : 2 *(balance + 2) : 3. *(balance + 3) : 17 *(balance + 4) : 50 In the above example, p is a pointer to double which means it can store address of a variable of double type. Once we have address in p, then *p will give us value available at the address stored in p, as we have shown in the above example.

Passing Arrays to Functions

C++ does not allow to pass an entire array as an argument to a function. However, You can pass a pointer to an array by specifying the array's name without an index. If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

Way-

Formal parameters as a pointer as follows โˆ’ void myFunction(int *param) { . . . }

Way-

Formal parameters as a sized array as follows โˆ’ void myFunction(int param[10]) { . . .

Way-

Formal parameters as an unsized array as follows โˆ’ void myFunction(int param[]) { . . . } Now, consider the following function, which will take an array as an argument along with another argument and based on the passed arguments, it will return average of the numbers passed through the array as follows โˆ’ double getAverage(int arr[], int size) { int i, sum = 0 ; double avg; for (i = 0 ; i < size; ++i) { sum += arr[i]; } avg = double(sum) / size; return avg; } Now, let us call the above function as follows โˆ’ #include using namespace std; // function declaration: double getAverage(int arr[], int size); int main () { // an int array with 5 elements. int balance[ 5 ] = { 1000 , 2 , 3 , 17 , 50 }; double avg; // pass pointer to the array as an argument. avg = getAverage( balance, 5 ) ; // output the returned value cout << "Average value is: " << avg << endl; return 0 ; }

// a pointer to an int. int p; p = getRandom(); for ( int i = 0 ; i < 10 ; i++ ) { cout << "(p + " << i << ") : "; cout << *(p + i) << endl; } return 0 ; } When the above code is compiled together and executed, it produces result something as follows โˆ’ 624723190 1468735695 807113585 976495677 613357504 1377296355 1530315259 1778906708 1820354158 667126415 *(p + 0) : 624723190 *(p + 1) : 1468735695 *(p + 2) : 807113585 *(p + 3) : 976495677 *(p + 4) : 613357504 *(p + 5) : 1377296355 *(p + 6) : 1530315259 *(p + 7) : 1778906708 *(p + 8) : 1820354158 *(p + 9) : 667126415