+ 1
Can you help me tell me what's wrong?
Hi, I have a problem with this code, I can't get it to print on the screen the smallest number #include <iostream> using namespace std; int main() { int tam; cin>>tam; int arr[tam]; int may=0; int men=0; for (int i=0; i<tam; i++) { cin>>arr[i]; for (int j=0; j<=i; j++) { if (arr[i] >= may){ may= arr[j]; } else { men=arr[i]; } } } cout<<men<<" "; cout<<may; return 0; }
2 Respuestas
+ 6
Although the method suggested by Martin Taylor is better but still if you want to know what are the problems in your perticular code then here you go:-
1) initial value of "may" and "men" are 0 ( evaluation will fail in many cases for example when the input doesn't have any zero value in it )
2) "men" is assigned a new value without even checking if it's minimum or not.
Here is the fixed code 👇
https://code.sololearn.com/ci7AH3a2Km14/?ref=app
+ 1
Wow, tanks