0
Follow
3
View

Why does my code not run correct with arr10 but work fine with arr9?

creasy_wang 注册会员
2023-01-26 03:01

The reason your code doesn't work with the second array is because it will never set smallestVal when the smallest value is the last location in the array.

There is no need for your variable a here. Replace it with this:

int smallestVal = intArray[0]; //field
for (int i : intArray){
       if (i < smallestVal){
           smallestVal = i;
       }
   }
dousar 注册会员
2023-01-26 03:01
if (i > a) {
    smallestVal = a;
}
else {
    a = i;
}

This sets a to the value of i if i is smaller or equal a and sets smallestVal to the value of a if i is greater than a. In the case that i is never greater than a after a got updated smalledtVal will not be updated to the smallest value. I'd suggest getting rid of smallestVal and just returning a as a should always contain the smallest value. That being said a should probably be named smallestVal as it's a more descriptive name.

About the Author

Question Info

Publish Time
2023-01-26 03:01
Update Time
2023-01-26 03:01

Related Question

安卓怎么实现对mysql的增删改查啊悬赏可

MYSQL的问题解决

控制面板删除mysql组件

如何重定向用户的表单提交后,从mysite.com/services/到mysite.com/services/thank-you-page

在MySQL中ON DUPLICATE KEY UPDATE会导致额外的磁盘空间被占用吗?

Flask-security-too与wee + mysql不工作

How to use storybook with real data?

我怎么能级联删除与MySQL的递归关系

启动并登陆mysql shell

mysql distinct 之后使用limit 翻页失效