0
what is the problem in this code
# include <iostream> using namespace std; void min(float* array); int main() { float numbers[10] ={12.0, 23.2, 54.6, 84.0, 2.3, 24.2, 73.5, 7.0, 11.2, 4.5}; float min; min = min(&numbers); cout<<"the smallest number is: "<<min; return 0; } void min(float* array) { Â Â Â float *array; Â Â Â float min; for(int i=0; i<10; i++) if(*array<min) min= array[i]; return min; }
2 Answers
+ 1
- Same variable and function names (min)
- min has initial value of 0, so nothing is less than it with the given values
- If is always checking against first array element.
https://code.sololearn.com/cI6eQFer4qYp/?ref=app
Note: same adjusments are applied by rodwynnejones already, I'll just leave this as explanation.
+ 1
compare:-
#include <iostream>
using namespace std;
float minfltfunct(float *arr);
int main()
{
float numbers[10] = {12.0, 23.2, 54.6, 84.0, 2.3, 24.2, 73.5, 7.0, 11.2, 4.5};
float minfloat;
minfloat = minfltfunct(numbers);
cout << "the smallest number is: " << minfloat;
return 0;
}
float minfltfunct(float *arr){
float minfloat = arr[0];
for(int i = 1; i < 10; i++)
if(arr[i] < minfloat)
minfloat = arr[i];
return minfloat;
}