help with my c++ example

V

vbmax

Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!! but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

help me out, please
 
J

John Harrison

vbmax said:
Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!! but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

help me out, please

No, all you need is two variables. One for the last number the user entered
and one for the LOWEST NUMBER FOUND SO FAR. Each time the user enters a new
number check if it is lower than the lowest number entered so far and if it
is update the lowest number entered so far.

When the user has entered all the numbers then the lowest number found so
far will be the lowest number of all the numbers entered.

john
 
C

Chris Theis

vbmax said:
Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!! but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

help me out, please

Start out and nail the problem down to the basic question. You have an input
(1 variable) and you should find the lowest number (another variable) that
the user entered. One way would be to store all variables and perform the
test for the lowest number afterwards. This can certainly be done with a
dynamic array or a standard container but is it really necessary?

No, you can simply solve this problem by testing for the lowest number each
time the user inputs another value and consequently you'll only need two
variables. However, there is a pitfall you should keep in mind - think about
the value you'll use to initialize your variables before the user starts
entering values and the comparison is performed for the first time.

BTW - the user will have a hard time finding 1000000 different three digit
numbers ;-)

HTH
Chris
 
M

Mike Wahler

vbmax said:
Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!!

Hint:

int i;
i = 134;
i = 256;
i = 425;
but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

Last time I checked, there were only 900 of them. But you don't
need 900 objects.

-Mike
 
S

Stuart Gerchick

Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!! but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

help me out, please

I would love to help you. But I do not want to tell you exactly how.
i will point you in the right direction. You are right, you need a
way to handle an arbitrary number of ints. In C you could dynamically
allocate memory for however many ints were asked for with malloc. in
c++ it is easier, there are classes that will do all the dirty work.
The one you want is vector. This will allow you to decide what you
want a vector of and on the fly increase to the size needed. Why dont
you look up vector and come back with more questions

Stuart Gerchick
 
P

puppet_sock

Hi to all!

I have to write the following program. [homework snipped]
help me out, please

Sure! Glad to help you. I'll just need your instructor's email address
in order to get a few clarifications on how this should be done.
Socks
 
M

Major_Small

Hi to all!

I have to write the following program.

1.User inputs a number (example: 5)
2.Then (because he entered 5) he enters 5 times, each time different,
3-placed number (example: 134, 256, 425, 253, 275)
3.then I have to find the lowest of those he entered and print it out

I have no idea how do I do that, because If the user enters a mubers
100 so he will input 100 different 3-placed numbers.you have to
declare each number (example: int a = 1;...)so that would mean that i
need to declare 100ints!! but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?

help me out, please

I'll help you figure it out, but I won't answer it for you. that will
get you nowhere...

for the input -> use a for loop
for the 1000000 different numbers -> do you really need that many to
find the lowest one if you're taking them in one at a time?

here's a hint on that second one - let's say I want to read you 100
numbers and have you tell me what the lowest one is... are you going
to memorize every number I read to you?
 
M

Mike Wahler

Stuart Gerchick said:
(e-mail address removed) (vbmax) wrote in message

I would love to help you. But I do not want to tell you exactly how.
i will point you in the right direction. You are right, you need a
way to handle an arbitrary number of ints.

No he doesn't.

In C you could dynamically
allocate memory for however many ints were asked for with malloc. in
c++ it is easier, there are classes that will do all the dirty work.
The one you want is vector. This will allow you to decide what you
want a vector of and on the fly increase to the size needed. Why dont
you look up vector and come back with more questions

No need to store all the input to solve this.

-Mike
 
S

Stuart Gerchick

Mike Wahler said:
No he doesn't.

In C you could dynamically

No need to store all the input to solve this.

-Mike

of course not, not to just get the average. but to do anything more,
you might need it. of course he can just take a sum and divide by the
number of inputs, that is not the point. he obviously is not aware of
a languages ability for anything other than single variables. We
should take this opportunity so he can see a little more and
understand a new concept.
 
C

Chris Theis

Stuart Gerchick said:
"Mike Wahler" <[email protected]> wrote in message

of course not, not to just get the average.

But that isn´t what the OP is trying to do.

[cite] 3.then I have to find the lowest of those he entered and print it out
[/cite]
but to do anything more,
you might need it. of course he can just take a sum and divide by the
number of inputs, that is not the point. he obviously is not aware of
a languages ability for anything other than single variables. We
should take this opportunity so he can see a little more and
understand a new concept.

That´s certainly true, but one step after the other might be more helpful
because I doubt that the OP will be happy reading about templates and
template syntax etc. if she/he is still stuck with such basic algorithmic
things.

Cheers
Chris
 
Z

zhou xiang

Hi to all!

I have to write the following program. [homework snipped]
help me out, please

Sure! Glad to help you. I'll just need your instructor's email address
in order to get a few clarifications on how this should be done.
Socks

It is my first time to enter into this Google Groups,and I'm so
excited that I write this message although it having nothing to do
with the topic.Thank puppet_socet for providing me a chance to
here,thanks again.
 
S

Stuart Gerchick

Chris Theis said:
Stuart Gerchick said:
"Mike Wahler" <[email protected]> wrote in message

of course not, not to just get the average.

But that isn´t what the OP is trying to do.

[cite] 3.then I have to find the lowest of those he entered and print it out
[/cite]
but to do anything more,
you might need it. of course he can just take a sum and divide by the
number of inputs, that is not the point. he obviously is not aware of
a languages ability for anything other than single variables. We
should take this opportunity so he can see a little more and
understand a new concept.

That´s certainly true, but one step after the other might be more helpful
because I doubt that the OP will be happy reading about templates and
template syntax etc. if she/he is still stuck with such basic algorithmic
things.

Cheers
Chris

Agreed. Though it seems even learning about arrays, circa 1960's
Fortran could be useful here
 
T

Thomas Matthews

Mike said:
news:[email protected]... [snip]
but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?


Last time I checked, there were only 900 of them. But you don't
need 900 objects.

-Mike
Wouldn't that be 1000?
0 ... 999
I believe that is 1000. Did you or I miss some?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Mike Wahler

Thomas Matthews said:
Mike said:
news:[email protected]... [snip]
but thats impossible, what if the user
decides he is going to input 1000000 different 3-placed numbers?


Last time I checked, there were only 900 of them. But you don't
need 900 objects.

-Mike
Wouldn't that be 1000?
0 ... 999
I believe that is 1000. Did you or I miss some?

I suppose OP would need to clarify what he meant by '3-placed'.
I took it to mean "three digits". No leading zeros.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top