+ 2

Array size determination within a function

In the 'main' part of the programm I declared an array of N elements. I would like to send it to the function which make some statistical calculation on it. How is it possible to detemine the size of this array (N) within the function without sending the number explicitely?

24th Jan 2018, 7:24 AM
Vladimir Filippov
Vladimir Filippov - avatar
8 Respostas
+ 4
You can use templates, the compiler will automatically deduce the size for you. #include <iostream> template<class T, std::size_t sz> void PrintArray(T (&arr)[sz]) { for (auto& i : arr) std::cout << i << "\n"; } int main() { int arr[] = {4, 3, 1, 5, 8}; PrintArray(arr); } https://code.sololearn.com/c1wqPSpEJnuT
24th Jan 2018, 11:21 AM
aklex
aklex - avatar
24th Jan 2018, 7:30 AM
jay
jay - avatar
+ 7
Note: I believe the generic method will not work with pointers. (just in case this is what you were planning)
24th Jan 2018, 7:36 AM
jay
jay - avatar
+ 3
Thanks a lot! It works well: https://code.sololearn.com/c6nxF46PmV76/#cpp #include <iostream> using namespace std; template<class T, std::size_t sz> void PrintArray(T (&arr)[sz]) { std::cout << "Size of an element: " << sizeof(arr[0]) << "\n"; std::cout << "Size of the whole array: " << sizeof(arr) << "\n"; std::cout << "Number of elements: " << sizeof(arr)/sizeof(arr[0]) << "\n"; std::cout << "Elements of the array: "; for (auto& i : arr) std::cout << i << " "; std::cout << "\n " << "\n"; } int main() { int arr[] = {4, 3, 1, 5, 8}; PrintArray(arr); float farr[] = {1.2, 3.5, 8.1, 9.0, 3.9, 6.1}; PrintArray(farr); double darr[] = {1.22, 3.51, 8.17, 9.05, 9.00, 3.94, 6.18, 5.283}; PrintArray(darr); char carr[] = {'a', 'd', 'f', 's'}; PrintArray(carr); char sarr[] = "just an example"; // string as an array PrintArray(sarr); std::cout << sarr << "\n"; } Output ========================================== Size of an element: 4 Size of the whole array: 20 Number of elements: 5 Elements of the array: 4 3 1 5 8 Size of an element: 4 Size of the whole array: 24 Number of elements: 6 Elements of the array: 1.2 3.5 8.1 9 3.9 6.1 Size of an element: 8 Size of the whole array: 64 Number of elements: 8 Elements of the array: 1.22 3.51 8.17 9.05 9 3.94 6.18 5.283 Size of an element: 1 Size of the whole array: 4 Number of elements: 4 Elements of the array: a d f s Size of an element: 1 Size of the whole array: 16 Number of elements: 16 Elements of the array: j u s t a n e x a m p l e just an example =====================================================
25th Jan 2018, 8:28 AM
Vladimir Filippov
Vladimir Filippov - avatar
+ 2
Unfortunately such a construction sizeof(arr)/sizeof(arr[0]) doesn't work within a function. In the example below the sizeof(arr) operator returns just a size of the pointer itself and the value of aSize is always 1. So it is necessary to pass the size of the array explicitely, The question is how to avoid this? float rootMeanSquer(int *arr, int arrSize, float aver) { int i = 0; float rootMeanSquer = 0; int aSize; aSize = sizeof(arr)/sizeof(arr[0]); // A try to determine the number of elements cout << aSize << endl; for( int i = 0; i < arrSize; i++) { rootMeanSquer += sq(arr[i] - aver); }; rootMeanSquer /= (arrSize - 1); return sqrt(rootMeanSquer); } // End rootMeanSquer
25th Jan 2018, 7:54 AM
Vladimir Filippov
Vladimir Filippov - avatar