0

Why isn't my code working if the smallest number is the first number in array?

https://code.sololearn.com/cCLD7tJFLzws/#c for input: 1 3 2 3 4 the answer is coming 4 with small1=2 and small2=2 expected output is 5 with small1=2 and small2=3 the code finds the smallest and second smallest number in the array and adds them the sum is shown as output

1st Mar 2018, 5:00 PM
srihitha reddy
srihitha reddy - avatar
2 Answers
+ 4
What is the expected output? And what is the code supposed to do? Kindly elaborate.
1st Mar 2018, 4:56 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
First of all: Use better variable names. It's better if the name explains what the variable is used for. I edited your code a bit to show the difference: https://code.sololearn.com/c2nNujk9SILz/?ref=app And now the bug. You've set the smallest values to the first value of the list when declaring them and then check if another value is smaller. Since there is no smaller value in your example the value stays at 2. You need to give it a high value. That's why I included limits. I set the values to INT_MAX so that no value exists that could be higher. Just to add something: you need to check if a is smaller than b. You could've declare a function for this. If you name it "isSmaller" you can do stuff like: if(isSmaller(a, b)), which adds even more readability in my opinion. But that's up to you.
1st Mar 2018, 5:42 PM
Alex
Alex - avatar