+ 1

to find the smallest integer using while loop

8th Aug 2016, 1:14 PM
maria
4 Antworten
+ 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
8th Aug 2016, 3:19 PM
jamie mctaggart
jamie mctaggart - avatar
0
lol praveen u r hilarious
8th Aug 2016, 6:09 PM
Suhail Pappu
Suhail Pappu - avatar
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; }
8th Aug 2016, 6:12 PM
Abhishek Kumar
Abhishek Kumar - avatar
- 1
0
8th Aug 2016, 5:48 PM
parveen
parveen - avatar