# Random Addition

Discussion in 'C++' started by snow.carriers@gmail.com, Oct 8, 2005.

1. ### Guest

(This is personal work by the way, I took the questions from sites to
enhance my c++ at start before I go indepth)

A program will ask 10 questions in addition. If they get it wrong, they
get three tries to answer it, if they fail on all of them then it goes
to the next question. If they any correct, they get a point.

I'm moderately sure how to get started. I can see many possibilities
with this one. I'm thinking of using a random function to generate
numbers. Then make an expression if they're guess is true (boolean),
and that the next part of the loop will go. When they get another try,
and when attempt = 3, the next loop will go.

Any other ways that will make the program more efficient, use less
code? I'm trying to make it the best it can be. Any code examples of
how to start off will also help me out.

Thanks

, Oct 8, 2005

2. ### John HarrisonGuest

wrote:
> (This is personal work by the way, I took the questions from sites to
> enhance my c++ at start before I go indepth)
>
> A program will ask 10 questions in addition. If they get it wrong, they
> get three tries to answer it, if they fail on all of them then it goes
> to the next question. If they any correct, they get a point.
>
> I'm moderately sure how to get started. I can see many possibilities
> with this one. I'm thinking of using a random function to generate
> numbers. Then make an expression if they're guess is true (boolean),
> and that the next part of the loop will go. When they get another try,
> and when attempt = 3, the next loop will go.

It all seems reasonable enough, you have a loop within a loop.

>
> Any other ways that will make the program more efficient, use less
> code?

Ammount of code is not an issue, nor is efficiency. You are barking up
the wrong tree if you try to achieve either of those. The key things to
go for, the things that seperate good programmers from bad, is clarity
of code and logical design of code. Write code that is easy and natural
to understand, that solves the problem at hand but is also easily
extendible to other similar problems. Forget about efficiency and
smallness of code, those are typical newbie concerns and bogus concerns
most of the time.

I'm trying to make it the best it can be. Any code examples of
> how to start off will also help me out.
>

The issue you have not addressed is how in your random choice of
questions you will avoid asking the same question twice. That is a
problem which has an obvious but slightly messy answer, and a much
cleaner but slightly less obvious answer. Here's a hint, 'shuffle'.

> Thanks
>

john

John Harrison, Oct 8, 2005

3. ### GregGuest

wrote:
> (This is personal work by the way, I took the questions from sites to
> enhance my c++ at start before I go indepth)
>
> A program will ask 10 questions in addition. If they get it wrong, they
> get three tries to answer it, if they fail on all of them then it goes
> to the next question. If they any correct, they get a point.
>
> I'm moderately sure how to get started. I can see many possibilities
> with this one. I'm thinking of using a random function to generate
> numbers. Then make an expression if they're guess is true (boolean),
> and that the next part of the loop will go. When they get another try,
> and when attempt = 3, the next loop will go.
>
> Any other ways that will make the program more efficient, use less
> code? I'm trying to make it the best it can be. Any code examples of
> how to start off will also help me out.
>
> Thanks

You should also write the program with some degree of flexibility -
since the first version of a program is rarely the last version. For
instance: making the number of wrong answers allowed or the total
number of questions easily configurable. Or to have the program adjust
the difficulty of the problems depending on the user's current score.
Or to add a timer, or..

Greg

Greg, Oct 8, 2005
4. ### Karl Heinz BucheggerGuest

wrote:
>
> (This is personal work by the way, I took the questions from sites to
> enhance my c++ at start before I go indepth)
>
> A program will ask 10 questions in addition. If they get it wrong, they
> get three tries to answer it, if they fail on all of them then it goes
> to the next question. If they any correct, they get a point.
>
> I'm moderately sure how to get started. I can see many possibilities
> with this one. I'm thinking of using a random function to generate
> numbers. Then make an expression if they're guess is true (boolean),
> and that the next part of the loop will go. When they get another try,
> and when attempt = 3, the next loop will go.
>
> Any other ways that will make the program more efficient, use less
> code? I'm trying to make it the best it can be. Any code examples of
> how to start off will also help me out.
>
> Thanks

--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5

Karl Heinz Buchegger, Oct 10, 2005
5. ### Karl Heinz BucheggerGuest

wrote:
>
> I'm moderately sure how to get started. I can see many possibilities
> with this one. I'm thinking of using a random function to generate
> numbers. Then make an expression if they're guess is true (boolean),
> and that the next part of the loop will go. When they get another try,
> and when attempt = 3, the next loop will go.
>
> Any other ways that will make the program more efficient, use less
> code? I'm trying to make it the best it can be.

You are heading in the wrong direction.
First make it work, then sit back, look at your solution and
figure out ways how you can enhance the solution. Start with thinking
about the design and see how your current design influenced your coding.
Then think about what other designs are possible and try to implement
them.

That actually will teach you something. Asking somebody for what you
should do or shouldn't do, won't teach you much. There is just one
exception: if you neither have a solution nor have an idea how
a solution should look like, then ask. But if you have an idea, forumlate
it in a programming language. If it works, fine. There may be better ways,
shorter ways, ways that require less code, but they all have one disadvantage:
They were not 'invented' by our brain.

Making mistakes in programming which means throwing away everything
and start afresh is perfectly allowed in programming (although your boss
will not like it) and *is* a good way of learning.

--
Karl Heinz Buchegger

Karl Heinz Buchegger, Oct 10, 2005
6. ### Karl Heinz BucheggerGuest

Karl Heinz Buchegger wrote:
>
> wrote:
> >
> > I'm moderately sure how to get started. I can see many possibilities
> > with this one. I'm thinking of using a random function to generate
> > numbers. Then make an expression if they're guess is true (boolean),
> > and that the next part of the loop will go. When they get another try,
> > and when attempt = 3, the next loop will go.
> >
> > Any other ways that will make the program more efficient, use less
> > code? I'm trying to make it the best it can be.

>
> You are heading in the wrong direction.
> First make it work, then sit back, look at your solution and
> figure out ways how you can enhance the solution. Start with thinking
> about the design and see how your current design influenced your coding.
> Then think about what other designs are possible and try to implement
> them.
>
> That actually will teach you something. Asking somebody for what you
> should do or shouldn't do, won't teach you much. There is just one
> exception: if you neither have a solution nor have an idea how
> a solution should look like, then ask. But if you have an idea, forumlate
> it in a programming language. If it works, fine. There may be better ways,
> shorter ways, ways that require less code, but they all have one disadvantage:
> They were not 'invented' by our brain.

***
Sorry. Replace 'our' with 'your'.

--
Karl Heinz Buchegger

Karl Heinz Buchegger, Oct 10, 2005

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.