+ 1
to find the smallest integer using while loop
4 Answers
+ 1
create a variable for the smallest number (int smallest = arrayThings[0];) assign it to the first thing in the list/array, then literate through the list/array and have an if statement comparing the the current smallest variable and the indexed one if the indexed is smaller then assign the smallest variable (if (smallest > arrayThings[current iteration]){smallest = arrayThings[current iteration];}) whatever the indexed one was until it is over and the smallest variable should contain the smallest item in the list p.s I'm pretty sure this would work better as a for loop and I am sorry for my messy comment
0
lol praveen u r hilarious
0
#include <iostream>
using namespace std;
int main() {
int arr[4] = {1,2,0,4};
int arr_len = 4;
int sm_nu = arr[arr_len];
while(arr_len) {
arr_len --;
sm_nu = arr[arr_len] > sm_nu ? sm_nu : arr[arr_len];
}
cout<<"smallest number: "<<sm_nu <<endl ;
return 0;
}
- 1
0