0
What is the best way to calculate the no of elements that array has? Any formula.
10 Answers
+ 3
I think you need to:
#include <array>
http://www.cpluplus.com/reference/array/array/size/
+ 2
Use built-in function/method ^^
+ 2
In Python: len(myArray)
In Javascript: myArray.length;
In C++: myArray.size();
( because the three languages were initialy tagged in question ^^ )
0
which builtin function?
One way of doing it in c++ is :
cout<< sizeof(array)/sizeof(array[firstelement] );
But the problem with this is that; it is limited to the function, it is defined in.
0
I think that you are looking for the len function
0
I wanna know how many elements an array contains without using sizeof()
0
#include <iostream>
using namespace std;
int main() {
int array[10];
cout<<array.length;
return 0;
}
producing error
0
in cpp I am executing the above code.
#include <iostream>
using namespace std;
int main() {
int array[50];
cout<<array.size();
return 0;
}
it is producing error.
@visph
0
#include <iostream>
using namespace std;
int main() {
int array[50];
cout<<array.size();
return 0;
}
it is producing error.
This code above will not compile. If you want to get the size of array, you use the built in function with your variable attached.
array.size(). The reason for the compile error is because you have to declare the array for it to get the size.
int array[2] = {2, 4, 6}
The reason is in order to get the number of elements, the compiler first gets int which equals 4. Then it calculate it by 3 to get the bits it takes up which is 12. For elements it divides by three 12/3. It doesn't do it for 50 because you may not use 50. It is just the max you set aside for it. I hope that helped a bit.
- 1
09