English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How Do I use while or do-while loop in c++ to figure out the largest and smallest number from list of numbers entered by the user?

Thanks for your help

2007-03-26 15:21:33 · 3 answers · asked by jkim972 3 in Computers & Internet Programming & Design

3 answers

/*
vitualpalab0i is mostly right but theres a few issues with his code:

- the counter "ctr" is never advanced
- the way he has it set up, smallest number will always be 0.
- Arrays use 0 as their first set, so must use numbers[ctr-1]
- other small errors
*/

// Try this:

#include

/*assume that numbers[~] array with size of num has pre-initialized values in it. */

using namespace std;

void main()
{ int numbers[num]; // list of numbers
int ctr=1;
int largestNum=0;
int lowestNum=0;

while(ctr != num)
{ if(numbers[ctr-1] > largestNum)
largestNum=numbers[ctr-1];
ctr++;
}

ctr = 0;
lowestNum = largestNum; /*this insures you start from the top down, to get the true lowest number*/

while(ctr != num)
{ if(numbers[ctr-1] < lowestNum)
lowestNum=numbers[ctr-1];
ctr++;
}

}

cout<<"largest number is: " << largestNum < cout<<"lowest number is: " << lowestNum <
// getchar(); I think this is compiler specific.
// on Windows? Use this:
system("pause");
}

2007-03-26 16:31:05 · answer #1 · answered by kristano.esperanta 1 · 0 0

shall we do a dry run: initially, ctr = one million, ctr1 = 2 and n =2. whilst (ctr1 <= (n-one million)) is corresponding to whilst(2 <= (2-one million)) which ability whilst (2 <= one million). for the reason that it extremely isn't genuine, the code interior the whilst() loop won't run. this suggests there'll be no execution of the if(n% ctr1 == 0) assertion nor of the respective else assertion. the subsequent if venture is now run. if( ctr1 == n) is corresponding to if( 2 == 2) that's genuine and subsequently the value of n would be printed. ctr is better as properly. n is likewise better. whilst( ctr <= one hundred) is corresponding to whilst( one million <= one hundred) that's genuine so the loop will iterate back. Now, the value of ctr = 2, ctr1 = one million (because of fact the ctr1++ interior the whilst() loop in no way got here approximately) and n = 3 back we enter the whilst() loop. Now whilst( ctr1 <= (n-one million)) is corresponding to whilst( one million <= 3) that's genuine. We enter the loop and the if assertion is administered. if(n % ctr1 == 0) is corresponding to if( 3 % one million == 0) that's genuine and the loop breaks. back there is not any boost in ctr1. if(ctr1 == n) is now corresponding to if(one million == 3) that's clearly no longer genuine and subsequently the value of n isn't printed neither is there a upward thrust in ctr. n is better. this might proceed till ctr is comparable to one hundred at which element the do-whilst loop will ruin and next code would be accomplished. with a bit of luck you may now visualize the source of errors. you may now strengthen a clean logic and enforce it.

2016-10-20 12:40:45 · answer #2 · answered by ? 4 · 0 0

#include
#include

/*assume that numbers[] array with size of num has pre-initialized values in it. */

using namespace std;

void main() {

int numbers[num]; //list of numbers
int ctr=1;
int largestNum=numbers[0];
int lowestNum=numbers[0];

while(ctr != num) {

if(numbers[ctr] > largestNum) {
largestNum=numbers[ctr];
}

if(numbers[ctr] < lowestNum) {
lowestNum=numbers[ctr];
}

}


cout<<"largest number is: " << largestNum < cout<<"lowest number is: " << lowestNum <
getchar();
}

2007-03-26 15:40:44 · answer #3 · answered by vitualpalab0i 1 · 0 0

fedest.com, questions and answers