0

I need help

write the code to display the smallest number stored in one - dimensional int array named orders . the array has five elements (10 , 20 , 18 , 25 , 9 ) . use the while statment

21st Dec 2016, 5:30 AM
Ady Jeko
Ady Jeko - avatar
3 Answers
+ 6
Assuming you know the basics as in how to declare the arrays - What you need to do now is to compare all arrays to find the smallest array: E.g. int smallest = 1000; // or any integer larger than all if (smallest < array[0]) { smallest = array[0]; } The above segment of the code would only compare the first array to smallest, though. Hence, you may use a loop and an integer variable to store and manipulate the corresponding array index so that all the arrays are compared. E.g. int smallest = 1000; for (int i = 0; i < 5; i++) { // conditional statement here // where smallest is compared to array[i] } However, you have stated that you need to use while loop. So I would leave it to you to figure that out, since this is probably an assignment question. :>
21st Dec 2016, 6:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
#include <iostream> using namespace std; int main() { int orders[5]={10,20,18,9,25}; int small = orders[0]; int i =0; while (i<5){ if (small>orders[i]){ small=orders[i]; } i++; } cout<<small; return 0; } this is the answer by while loop
21st Dec 2016, 7:55 AM
Ady Jeko
Ady Jeko - avatar
0
first thx for help second its not assessment
21st Dec 2016, 6:53 AM
Ady Jeko
Ady Jeko - avatar