I'm trying to make method has an array as parameter the purpose of this method is to get the min element in the array. I'm using C++.
when I pass the array size using (sizeof(arr) / sizeof(arr[0])
to Loop it only has 2 as it's size
Here is my program :
int main(int argc, char** argv) {
int lenghtSticks;
cout << "Enter [ N ] For length of sticks :\n";
cin >> lenghtSticks;
int sticksItems[lenghtSticks];
for (int i = 0; i < sizeof(sticksItems) / sizeof(sticksItems[0]); i++)
cin >> sticksItems[i];
cout << minStick(sticksItems) << endl;
return 0;
}
Here is my method
int minStick(int arr[]) {
int lowest = arr[0];
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
if (lowest > arr[i])
lowest = arr[i];
}
return lowest;
}
Input as size of array [6] Input as Elements = {5, 4, 4, 2, 2, 6} I should get out put 2 but I get 4 any help