+ 1

Size of Array

https://code.sololearn.com/c8mf0UjIjVTU/?ref=app read the code give and can someone tell why we need to know the size of array and where we need that and how this array size is 28 ?

25th Dec 2017, 2:29 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
2 odpowiedzi
+ 1
To get number of elements contained by array , you must know size of array. Here, int n = sizeof(arr)/sizeof(arr[0]); n is number of elements which you get 7. n = sizeof array 28 / size of one element 4 = 7 This n you can use to set condition while you are iterating on array using loop. see below code where I used n to iterate over arr using for loop to print each element of an array. #include <iostream> using namespace std; int main() { int arr[] = {5, 2, 42, 6, 1, 3, 2}; int n = sizeof(arr)/sizeof(arr[0]); for (int i = 0; i < n ; i++) cout <<" "<<arr[i]; cout <<endl; return 0; } This just an example but in real you can do any operation on array elements like above.
26th Dec 2017, 1:38 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
0
hey i got it the array has 7 elements and each elements are integer and one integer sizeof 4 so 4×7=28; but still haven't gotten where this used
25th Dec 2017, 2:32 PM
Shyamal Bhatt
Shyamal Bhatt - avatar