Can you help me tell me what's wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

8th Jan 2021, 9:13 PM
Mkxgxju
Mkxgxju - avatar
2 Answers
+ 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
9th Jan 2021, 4:52 AM
Arsenic
Arsenic - avatar
+ 1
Wow, tanks
8th Jan 2021, 9:50 PM
Mkxgxju
Mkxgxju - avatar