Size of Array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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