0

I don't understand the elements of array. Can you help me

7th Jan 2017, 11:37 AM
Abdi Malik Abukar Yusuf
Abdi Malik Abukar Yusuf - avatar
3 Antworten
+ 6
If you have an array of ints, elements of that array are the ints you have there, and you can access those elements by their indexes. The first elements of an array is with the index number 0, and so on... So here's the example: int a[5]={12, 25, 10, 5, 7}; cout<<a[2]; That prints the 3rd element - number 10
7th Jan 2017, 12:04 PM
Filip
Filip - avatar
+ 2
Array is collection of data same data type. and we can call matrix of data. 1-D Array int arr[] = {1,2,3} In this array if we want to use 1 then we write like this arr[0]. because 1 is at 0th index of array arr. If we want to access 3 from arr then we write like this arr[2]. if we check length of array that time we use arr.length and length of arr is 3. 2-D Array int arr[][] = {{1,2,3},{4,5,6}} In this array if we want to use 1 then we write like this arr[0][0]. because 1 is at 0th row and 0th column of array arr. If we want to access 3 from arr then we write like this arr[0][2]. because 3 is available at 0th row and 2nd column.if you want to use 6 then use arr[1][2]. 1 is row and 2 is column. if we check length of array that time we use arr.length and length of arr is 6.if we use arr[0].length then we can see output is 3 because we only count 0th row length. If you have more question please reply me. I will help you
7th Jan 2017, 12:02 PM
Avanish Kumar
Avanish Kumar - avatar
+ 2
The great thing about arrays in c++ is that each element is contiguous with its neighbors. More modern languages prefer a container abstraction where insertion is less expensive but in c/c++ all that is needed is to reallocate and copy both halves (memcpy/strcpy) around the inserted portion. In most languages this is expensive but the two mentioned functions are highly optimised. Array addressing is supported by many CPU instruction sets. Most other modern languages impact on this either by bounds checking and/or indirection (the illusion of contiguousness).
7th Jan 2017, 12:13 PM
Leon
Leon - avatar