Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,094 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,596 people online right now. Registration is fast and FREE... Join Now!




While Structure Maximum Number

 
Reply to this topicStart new topic

While Structure Maximum Number, Enter multiple numbers -> Displays the greatest

fXp17
2 Oct, 2007 - 03:40 AM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2007
Posts: 1


My Contributions
I am trying to write a code where a user can enter 10 different numbers 1 through 200 and the greatest number and the smallest number are displayed after. A while structure must be used.

For example:
Please enter 10 random numbers:

32
194
5
69
100
69
113
17
90
111

The largest number you entered is: 194
The smallest number you entered is: 5

I am a beginner and don't even know where to start. I've created programs that use do simple equations. I've used if-else statements. Now I'm moving onto a while structure. Any help is appreciated to get me started.

Thanks a lot. Jason.

**Try to make it easy to understand. I'm a quick learner but don't want to get lost and confused. rolleyes.gif
User is offlineProfile CardPM
+Quote Post

Smarf
RE: While Structure Maximum Number
2 Oct, 2007 - 06:23 AM
Post #2

D.I.C Head
**

Joined: 21 Sep, 2007
Posts: 80



Thanked: 2 times
My Contributions
QUOTE(fXp17 @ 2 Oct, 2007 - 04:40 AM) *

I am trying to write a code where a user can enter 10 different numbers 1 through 200 and the greatest number and the smallest number are displayed after. A while structure must be used.


I am a beginner and don't even know where to start. I've created programs that use do simple equations. I've used if-else statements. Now I'm moving onto a while structure. Any help is appreciated to get me started.


**Try to make it easy to understand. I'm a quick learner but don't want to get lost and confused. rolleyes.gif


A while statement works like this:

CODE
while (this stuff is true)
{
   do this stuff
}



Since you have a set number of inputs (10), you can do a counter (let's call it counter) that the while checks each time.

CODE
int counter = 0; // initialize counter as an integer equal to zero
int greatest = 0; // this variable will hold the biggest
int least = 201; // this variable will hold the smallest ( needs to be bigger than max number)

while (counter < 10) // while counter is less than 10 will run this ten times
{
   get a number (called num) from the user
   if the number is bigger than greatest then set greatest to the number
   if the number is smaller than least then set least to the number
   increment your counter by adding one to it
}


I left this as psuedocode so you'll learn it instead of me just giving you the answer. Does it make sense?

For the sake of simplicity, I left out checking to see if the user enters a number in the valid range of 1 - 200.


** EDIT: Fixed the While to while **

This post has been edited by Smarf: 2 Oct, 2007 - 06:46 AM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: While Structure Maximum Number
2 Oct, 2007 - 06:42 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE(Smarf @ 2 Oct, 2007 - 07:23 AM) *

A while statement works like this:

CODE
While (this stuff is true)
{
   do this stuff
}


just a little correction here:
C and C++ are case sensitive languages so that While in the code I quoted is not correct, the correct keyword is while. I know it's a pseudo code, just don't want to confuse the beginners wink2.gif
User is offlineProfile CardPM
+Quote Post

Smarf
RE: While Structure Maximum Number
2 Oct, 2007 - 06:47 AM
Post #4

D.I.C Head
**

Joined: 21 Sep, 2007
Posts: 80



Thanked: 2 times
My Contributions
QUOTE(PennyBoki @ 2 Oct, 2007 - 07:42 AM) *

QUOTE(Smarf @ 2 Oct, 2007 - 07:23 AM) *

A while statement works like this:

CODE
While (this stuff is true)
{
   do this stuff
}


just a little correction here:
C and C++ are case sensitive languages so that While in the code I quoted is not correct, the correct keyword is while. I know it's a pseudo code, just don't want to confuse the beginners wink2.gif



Thanks for the input, I'm a beginner myself and didn't realise that. I edited my original reply.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:26PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month