5.x — Chapter 5 comprehensive quiz

Quick review

If statements allow us to execute a statement based on whether some condition is true. Else statements execute if the associated if statement is false. You can chain together multiple if and else statements.

Switch statements provide a cleaner and faster method for selecting between a number of discrete items. Switch statements pair great with enumerations.

Goto statements allow the program to jump to somewhere else in the code. Don’t use these.

While loops allow the program to loop as long as a given condition is true. The condition is evaluated before the loop executes.

Do while loops are the same as while loops, but the condition is evaluated after the loop execution. They’re great for menus or things that need to execute at least once.

For loops are the most used loop, and are perfect when you need to loop a specific number of times.

Break statements allow us to break out of a switch, while, do while, or for loop. Or a for each loop, which we haven’t covered yet.

Continue statements allow us to move immediately to the next loop iteration. Be careful when using these with while and do while loops, as your loop counter may not get incremented properly.

And finally, random numbers give us a way to make our programs behave different each time they are run. We’ll see an example of this in the quiz below!

Quiz time!

Warning: The quizzes start getting harder from this point forward, but you can do it. Let’s rock these quizzes!

1) In the chapter 2 comprehensive quiz, we wrote a program to simulate a ball falling off of a tower. Because we didn’t have loops yet, the ball could only fall for 5 seconds.

Take the program below and modify it so that the ball falls for as many seconds as needed until it reaches the ground.

In constants.h:

In your main code file:

Show Solution

2) Implement a game of hi-lo. First, your program should pick a random integer between 1 and 100. The user is given 7 tries to guess the number. If the user does not guess the correct number, the program should tell them whether they guessed too high or too low. If the user guesses the right number, the program should tell them they won. If they run out of guesses, the program should tell them they lost, and what the correct number is. At the end of the game, the user should be asked if they want to play again. If the user doesn’t enter ‘y’ or ‘n’, ask them again.

Here’s what your output should look like:

Let's play a game.  I'm thinking of a number.  You have 7 tries to guess what it is.
Guess #1: 64
Your guess is too high.
Guess #2: 32
Your guess is too low.
Guess #3: 54
Your guess is too high.
Guess #4: 51
Correct! You win!
Would you like to play again (y/n)? y
Let's play a game.  I'm thinking of a number.  You have 7 tries to guess what it is.
Guess #1: 64
Your guess is too high.
Guess #2: 32
Your guess is too low.
Guess #3: 54
Your guess is too high.
Guess #4: 51
Your guess is too high.
Guess #5: 36
Your guess is too low.
Guess #6: 45
Your guess is too low.
Guess #7: 48
Your guess is too low.
Sorry, you lose.  The correct number was 49.
Would you like to play again (y/n)? q
Would you like to play again (y/n)? f
Would you like to play again (y/n)? n
Thank you for playing.

* Seed your random number generator with time(0).
* Visual Studio users: Due to a flaw in the Visual Studio implementation of rand(), call rand() once after seeding to discard the first result.
* Use the getRandomNumber() function from lesson 5.9 -- Random number generation to pick a random number.
* Write a function that allows the user to play a single game of hi-lo.
* Write a function that asks the user if they want to play again and handles the looping logic for an incorrect input.

Show Solution

6.1 -- Arrays (Part I)
5.11 -- Introduction to testing your code

140 comments to 5.x — Chapter 5 comprehensive quiz

  • Marcel S

    Hey Alex,

    the second if is useless and gives out a compiler error
    anyways I love your tutorials thank you for making them!

  • JJ

    There seems to be an issue with this random number generator. It always gives 10 when you first run it.

    • Alex

      It sounds like maybe seeding the random number generator is failing for some reason.

      Can you tell me the following:
      * What compiler/OS are you running?
      * sizeof(time_t) on your system
      * sizeof(unsigned int) on your system
      * Whether static_cast<unsigned int>(time(0)) always comes out the same number?

      • JJ

        Let me first say thank you very much for writing and maintaining such a fantastic tutorial on C++. I am new to C++ and am learning a lot for your work.

        Here are the information you asked for:

        * What compiler/OS are you running?
          I am using the latest version of Xcode on the latest version of Mac OS X.

        * sizeof(time_t) on your system

        * sizeof(unsigned int) on your system

        * Whether static_cast<unsigned int>(time(0)) always comes out the same number?
          No. It changes every time I run it.

        By the way, I am not getting 10 anymore. I was consistently getting 11 about 20 minutes (maybe) after I posted my post. Today as of right now I am consistently getting 75 every time. Could it be that the seed changes rather slowly so the random number generated changes even slower?

        • Alex

          If static_cast<unsigned int>(time(0))) is coming out a different number every time you run your application, then your random number generator should generate a different sequence every time you run your program. However, from the symptoms you’re seeing, it sounds like your seed value is only being changed periodically

          Are you sure static_cast<unsigned int>(time(0))) changes _every_time_ you run your program, and not just once every 20 minutes or so?

          Normally when casting a larger int to a smaller one, compilers truncate, keeping the low bits (which are the ones that change every second, in this case). Maybe your system is doing something else, since technically casting a larger int to a smaller one results in undefined behavior.

          Try this instead:

          and see what happens?

          • Viv

            I am having the same problem (in Visual Studio) and I tried what you suggested here. It still produces the same number every time it starts. I waited ten minutes, then tried it again, and the same problem occurred except that the repeated number was different (I just tried it again after typing up this comment, and the repeated number has changed again. Both times it went up by two). So I suppose it doesn’t actually change every time the program is run, just every so often.

            • Alex

              It took a while, but I discovered the root cause of the issue.

              In Visual Studio, the first result of rand() is heavily dependent upon the seed value. Since the seed value from time() isn’t changing much, the first result from rand() isn’t changing much either. getRandomNumber() is then compressing that into the same number.

              After the first result, the sequence of numbers generated from rand() appears to be properly randomized.

              The easy solution is to discard the first result from rand(). I’ll update the examples accordingly.

  • Viv

    Hi! This tutorial is great and very useful. I am running into something odd in my solution to the first problem. It runs almost fine, except that it prints a seemingly random (but not infinite) number of "At (time) seconds, the ball is at height: 0" messages after the ball has hit the ground.

    This is my main function (everything else is the same as the original program):

    I agree that your solutions are quite a bit better, but I am curious as to why this is happening.

    • Alex

      You’re calling calculateAndPrintHeight() with argument height instead of argument initialHeight.

      This is pretty straightforward to discover using the debugger.

  • The following is my solution:

    Could it be better?

  • Oops…I forgot to #include constants.h

  • Can command prompt only print 296 lines? When I tried my solution (above posted) with a large input (1236549), it prints 296 lines of output and rest are not printed. I also noted that the only last 296 lines  were printed. For example: if my input is 1236549, the first line the program prints is "At 207 seconds, the ball is at height: 1.02659e+006" and the last line my program printed was "The ball is at height 0 meters now". I first thought that it’s an overflow problem. I changed the double variables to long double but the result remained unaffected.  I then wrote this:

    Result was counting of numbers from 4 to 299 where each number was printed in a new line. Does all command prompts are limited to print maximum 296 lines or this is a machine specific problem? Whatever this is, because the whole tutorial seems to use non-gui programs, you should tell the readers about this limitation of CMD somewhere in this tutorial 🙂

    • Alex

      The limitations of the console vary depending on operating system. In all likelihood, your program printed all of the lines, but the buffer on your console was set to only retain the last 296 lines. There’s likely a way to change how large the console buffer is, but that’s outside the scope of this tutorial. Ask Google. 🙂

  • Hello Alex, how are you

    I was recently trying to solve #2 and wrote this solution:

    Your solution is undoubtly the best one. I just want to ask you, is my program acceptable or I am doing something that shouldn’t be done for #2 problem?

    • Alex

      Your program has a few issues:
      1) You should only call srand() once, not every time game() is run. Move it to the top of main().
      2) Avoid using goto. Use a loop instead.
      3) You call game() in two different places. Consolidate this into a do-while loop.
      4) I’m not sure why game() returns anything since you always return 0.
      5) You should avoid using system() calls.
      6) The if statement to determine whether the user has lost isn’t needed. The user lost if the for loop terminated. So you can move the “you lost” string underneath the for loop and make it unconditional.

  • You can actually solve the equation for "seconds" of the "Ball falling from a tower" problem, and let the height == 0.
    Then you can post the exact time-elapsed when the ball hits the ground.(Using Visual Studio 2015 Community)

    // quiz2ques4.cpp : Defines the entry point for the console application.

    #include "stdafx.h"
    #include <iostream>
    #include "constants.h"
    #include "math.h"

    double getInitialHeight() {
        using namespace std;
        cout << "How high is the tower, in meters? ";
        double height(0.0);
        cin >> height;
        return height;

    void printHeightChart(double height) {
        using namespace std;
        using namespace myConstants;
        double instantHeight = height;
        for (int seconds = 0; instantHeight >= 0 && seconds <= 10; seconds++) {
            instantHeight = height - myConstants::gravity * seconds * seconds / 2;
            if (instantHeight < 0.0) {
                double lastSeconds = (sqrt((2 * (height) / myConstants::gravity)));
                cout << "At " << lastSeconds << " seconds, the ball has landed."  << endl;
                cout << "At " << seconds << " seconds, the ball is at " << (instantHeight > 0 ? instantHeight : 0) << " meters." << endl;

    int main()
        double height = getInitialHeight();
        return 0;

  • Call me "mat"

    Heyho, Alex!

    i’ve solved the 2nd problem and this is my solution :

    like what you can see there, i got some different algorithma from yours.
    so, i wanna ask you what are the bad things about my solution? except the use of "goto" of course.. i use it because im a little bit stuck on how to ask player to play again.. haha.

    thx before.. your tutorials are great :3

    • Alex

      I see a couple of things:
      1) You should only call srand() once. Your srand() gets called every game.
      2) Your main() function is a bit long -- if you broke it up into more functions, then I think you’d have an easier time using a loop to replay instead of goto.

      • Chad22

        Hi Alex,
        Is this good practice (to use the static variable) or should I just move the first two statements to the main function?

  • !coder

    This is my solution, without following all the hints. Please tell me guys, how bad is it?

    • Alex

      srand() should only be called once at the start of your program, so it needs to be outside the loop.

      Your code is hard to follow because you have loops inside of loops. Functions would be good here.

      I also think the continue at the end of main() may be unnecessary.

  • Josh

    Hey Alex, I need your opinion. I have been using your tutorials to learn C++ and I can’t quite seem to do the two quiz questions above. I have been able to do all the quizzes before these. Do you think I should keep reading on or not proceed until I can complete these quizzes? Some things I asked myself after attempting to write those programs and failing, and after checking the correct answers was: how did you know that some of those functions needed to be of the bool type? When I was attempting both of them, I didn’t even think of doing that. Is there a certain way to think when I read the questions, that will tell me what kinds of functions and what not to use? I feel like if I had a teacher for this, I could learn it no problem but it is kind of hard learning it on my own. I think my main problem is converting what you want me to do in the quiz question into the actual code but I have gotten better! So, thank you very much for your tutorials!

    • Alex

      The quizzes in this section are harder than the ones that you’ve seen previously.

      That’s intentional: they are designed to challenge you and force you to stretch a bit.

      Even if you don’t get it right, as you learn from your mistakes, and can understand why I did something the way I did, you can move on.

      If you don’t understand why I did something the way I did, ask.

      > Is there a certain way to think when I read the questions, that will tell me what kinds of functions and what not to use?

      Figuring out how to structure your programs is something that takes practice. For me, I usually ask: “what are the main things this program needs to do?” and those become functions. If any of those things are complicated or need to be done more than once, they get broken down into smaller functions. From there, it’s just a matter of understanding how to use parameters and return values effectively.

  • cesare

    Hi Alex, first of all thanks so much for your work.

    I have a question about the 2nd quiz (high low game), particularly for my function ‘playAgain’.
    I preferred to implement it using std::string instead of char but I got an unexpected result.
    When the function is called it prints "Would you like to play again (y/n)? " twice, and I really don’t understand why. Can you help me please.

    This is my complete program, anyway I think the issue is inside ‘playAgain’ as the rest works as expected.

    • cesare

      Another little question.
      How can I change ‘getNumber’ function to be sure the user typed a number instead of a character.
      I tried typing a character and the program seems to get into an infinite loop inside ‘playAgain’ function.
      Any hint will be appreciated.

      • Alex

        After the user enters a number, you can check to see whether it succeeded:

        Put this inside a loop to keep asking the user for input until cin doesn’t fail.

        • cesare

          Ok, I got it. I understand that I should be careful when using std::cin, and have to be aware of it’s behavior with the buffer.
          About the std::cin buffer, is 32767 its maximum size?
          This is my new implementation and it seems to work properly.
          If there’s a better way please let me know.
          Thx again.

          • Alex

            The maximum stream size can be accessed like this: std::numeric_limits::max(), which lives in the limits header.

            So technically, you should do this:

            You’ll only need to use this if you think you’ll have more data in your buffer than 32767 chars.

    • Alex

      The problem is that when you enter a number previously, the ‘\n’ character that the user entered is still in the input stream. So the first time you read the stream to get the user input into your string, it reads the ‘\n’ into your string and continues. This doesn’t match your conditional, so it loops one more time. Now the stream is empty, so it waits for user input.

      You can fix this by removing the ‘\n’ from the stream before asking the user if they want to play again, like this:

  • Jazz

    Hi, Alex.

    It seems to me, that "do-while statement", is more appropriate for quiz number 2, because a statement checks the condition   after it has been executed at least once.

    Please tell me, if there are any mistakes in this code:
    *I’m using C::B for Windows*

    • Alex

      Yes, on reflection, I agree with you. I’ve updated the example.

      • Jazz

        Alex, thank you for your answer, and I have the last question related to quiz number two, and to programing generally. Is there some kind of "gold" code, in case if needed to choose between 3 conditions? Is there a shorthand method for doing a particular type of "if/else if/else" statement? It seems to me, that there are a lot of "more"-"less"-"equal" cases in programing.

        • Alex

          Not really. Most common thing to do is:

          I’d advise against using the conditional operator to do these tri-state conditionals, as they’re harder to follow and easier to inadvertently mess up.

  • Annibale

    Just a comment from a mathematician … nice game: you always win 😀

  • bio

    something is wrong with the RNG ,it just keeps showing the same number for two or three intervals

  • Lukas

  • Sean Kelly

    Hello Alex, for the first question I took another route from what you put as the answer and I wanted to see if this was a bad choice. Also, just so you know, I am currently using as my compiler due to the fact I am unable to get to my main computer at this time and the one I am using is a little weird when it comes to visual studio, so there is no header for this program.

    • Alex

      This looks fine. The use of a different variable for the loop counter and loop condition is clever.

    • Shiva

      Yeah, the logic behind that for-loop is really nice. Elegant thought.

      One thing that occurred to me though is, you don’t need to call the function twice inside the loop. Instead,

      can optimise the code. IMHO this looks a little bit better logically as well.

      Hope that helps. 🙂

  • Victor

    I find the solution to 1st question a bit too large. I did it that way: (Back in chapter 2)



    PS: With two additions, printout the exact hit time at the end and restart the whole program at it’s end.

  • Makar

    paragraph 3.5 says this is bad, but it is still used in reference solution to 1)

    • Alex

      In general, doing equality/inequality comparisons with floating point numbers is dangerous.

      However, this is one of the few cases where it works, since:
      * 0.0 can always be represented exactly in floating point.
      * We’re not doing any math operations on currentHeight between the point where we assign 0.0 to it, and the point where we check if it’s set to 0.0 (so there’s no chance to accumulate floating point errors).

  • Chris

    this is my solution for number 2.  I just wanna ask you what are the bad things about my solution? it is fine use void instead bool like yours on play() function? and my main() is  so short because few statement placed in other function, it is good or bad? Thank you.

  • Nortski

    Haha, well I got a solution to question 2. Not very elegant though :p

    • Nortski

      Oops, disregard

      in the playGame function.
      That was just to tell me what the random number was, for testing purposes.

  • Mistorcha

    Well, thanks a lot for this C++ tutorial, it’s far better than the one in french I found so far.
    After looking for the correction, it look’s like I’m close to what you did, I have just a question about the getRandomNumber.
    When you first spoke about having a random number between a mini and a maxi, in my mind it looked like :

    Is something wrong with this fonction?

    Thanks a lot for your time.
    Btw, sorry for the "far to be" fluent english.

    • Alex

      Yes, if maxi-mini isn’t a divisor of the largest number generated by rand(), you get lopsided results due to the modulo (where some numbers are slightly more likely to occur than others).

  • Nathan

    No idea if you still post in here, but this was my solution. I just finished first year of computer science and I’m just going through your
    guides to touch up on my skills and make sure I understand most things.

    I still don’t feel like I’m as good as I should be after one year of university so I’m still looking to be ahead for next year.

    If you are still replying, some feedback on the layout & design of my code would be nice.

    Thanks =)

  • Aymen

    Hi, Alex. big supporter. My bro actually introduced me to this website 2 months ago and slowly but surely going through it, you might remember he says he used to talk to you quite a lot back in the day (Shemseddine) anyways, I tried accomplishing this task using a for loop and was stuck for a long time and then thought to use a while loop. Why is this

    not acceptable - its an infinite loop but i cant seem to see why.
    Anyways thanks for the help and looking forward to finishing this series and learning so much 🙂

    • Alex

      You never set heightNow, so if heightNow is ever >0, it will always be >0.

      • Aymen

        I did, maybe I should of posted the full code but everything is the same apart from my calculateAndPrintHeight function.

        Again much thanks for your help, I got so frustrated with myself of not being able to do it I’v started this chapter again to refresh myself and hopefully have a better crack at it at the end.

        EDIT: PS how do assign expressions to an if statement
        for example in free pascal I do


        and if the if statement returns true it executes everything withing the begin and end.

        With c++ to my understanding is that this

        would give me an output of 5, so what can I do to avoid this and organize my code.

        • Aymen

          I managed to figure it out , but I’m absolutely certain that its bad programming.

          • Alex

            There are a few things that are wrong with this:
            1) You’re using the comma operator in a while statement, which is very unconventional. Comma also evaluates to the rightmost operand, so your while statement essentially evaluates to while (time++).
            2) You shouldn’t need the if (heightNow == 0) statement if you fix the loop condition
            3) since heightNow is a double, you shouldn’t use the equality operator. Use <= instead in case of floating point error.

        • Alex

          Regarding if statements, operator= is used for assignment. Operator== is used for comparison. So you’d want your if statement to look like this:

          • Aymen

            Ahh I get it now, much thanks!, I’v seen that alot of people in the comments could not complete the random number generator problem. I tried accomplishing the task but not using the techniques learnt in your lessons (well one, mersenne). I got an error on two different occasions so I did this instead - is it still valid or bad code again?

            Anyway’s if anyone cant seem to get the mersenne function to work, I think my solution works fairly well.

            much thanks again!

            • Alex

              srand() should only be called once at the start of the program. Yours is inside the do loop.
              Also, the way you’re doing your random numbers will result in a slightly uneven distribution (low numbers will come up slightly more often than higher numbers) but that shouldn’t matter for this exercise since it’s not important to have an even distribution.

  • Sandro

    "* Visual Studio users: Due to a flaw in the Visual Studio implementation of rand(), call rand() once after seeding to discard the first result."

    I don’t think this is only a VS problem. I use Code::Blocks and the rand() function returns the same number as before, plus something.

    I also need to call two times to recieve a more randon number.

  • Lokesh

    For Quiz Q1., I think this is more modular:

  • Dyablo

    For Q1, I went with a for loop but I probably broke a "rule" about functions being too complicated, but at least it works haha:

  • saeid

    in quiz 2 i replaced this code :

    whit mine code :

    and my program work correctly . is it ane need to declare

    and make if statement work with that ???

  • Alex

    So I mostly used the same code for question 1 as above, however, I found that using || on my conditionals in the while loop meant that my loop would run until both height and time had been satisfied, whereas && would terminate when only one of the conditions was met. Surely the behaviour should be the other way round?

  • Shiva

    > Warning: The quizzes start getting harder from this point forward, but you can do it. Let’s rock these quizzes!
    Awesome! 😀

    Quiz #1 felt pretty straightforward, but Quiz #2 was the real fun! I tried to make use of almost everything I learned so far from Alex, and here’s the result:

    - gameFunctions.h -

    - gameFunctions.cpp -

    - gameHiLo.cpp -

    It looks pretty similar to Alex’s own solution, as well many others’s above.

    Question: is the default statement in the switch inside my playAgain() function totally pointless? As I’ve marked in the comments, I know it is superfluous since the loop iterates without it as indented if the control reaches there. But I felt including that made the logic clearer. Or am I being too fussy?

    Any other suggestions welcome too. 🙂

    • Alex

      Yes, the default case is superfluous because your switch is already at the end of the loop. However, it doesn’t really do any harm either, and if being able to document that case makes you feel better, then go with it.

      • Shiva

        That’s the expert’s opinion we all need. Thanks Alex!

        And I hope the rest of the solution is overall okay. That’s almost everything I’ve learned from here so far. 🙂

  • Shiva

    Alex, I also spotted a small mistake in the solution to Quiz #2:

    The second else if inside the playGame() function is unnecessary. Only else is needed, as what the if statement checks is the only remaining possibility.

  • sandy

    For the second quiz hi-lo, when the game asks for guess, if the user enters dd or any characters instead of integers the console went crazy!!! Do you know why? and how to prevent it since its a run time error.

    • Alex

      Yes, this happens because the extraction to an integer fails when you enter a non-integer input, and then all further input stops working until the problem is resolved. You can fix this by detecting the error and resolving it like so:

  • ImDaW

    Hi Alex!
    I just want to tell you that your guide is the best!
    That’s my solution for the second quiz!
    Thanks for all!

  • Domenic D

    I am amazed at how far I have come so far and I’m not saying it was easy either but I am still amazed and very appreciative for this tutorial.Seems like this is how I am going to learn C++. I have enrolled in on-line video based courses but for some reason it won’t stick.I do watch them for refreshing purposes.

    Thanks Alex.

  • orbit

    Got a question. Might be I’m missing something but I can’t figure it out.
    It’s the function bool playAgain()

    How can a bool return a char? 😮
    How would the compiler know (ch == ‘y’) to be true?

    • Alex

      A bool doesn’t return a char.

      First, the function returns a boolean value.

      Second, the return expression is (ch == ‘y’). The job of operator== in this function is to compare ch and ‘y’ and see if they are equal. If so, this operator evaluates to true, otherwise it evaluates to false. And this boolean value is then returned by the function.

  • Indorfin

    Is there any reason not to combine a PRNG with the remainder symbol %? For example, if I wanted to get a random number between 1-100, couldn’t I just let it generate its random number between 0 and 37,xxx, then take the remainder after dividing by 100 to be my "random selection"?

  • Veer

    Hey Alex, I just wanted to point out that in the first question, the headers are included two times.

  • Chris

    I don’t know how to gauge the readability of my program since it just makes sense to me but I want to know if this is too jumpy or too complicated. I realize some things can be condensed after looking at your solution but I was wondering if my attempt was passable. There are sooo many different ways to solve something it feels like the more you look at your code the more efficient you can make it almost like an artist spending too much time on their painting.

    • Alex

      The overall structure looks pretty good. The logic in playGame() is a little convoluted in that it handles both individual guesses and new games. Seems like this could be separated a bit for ease of comprehension.

      • Chris

        Ah, I see thank you very much! I know you said in one of your earlier lessons that one function should serve one purpose, so should function calling be as limited as possible in other functions and be better reserved for main()? In other words, if I can help it, always call from main? (I know it’s inevitable that functions will need to call other functions to get values and such).

        • Alex

          A well-written function should only serve one purpose, but that purpose could be to call a few other functions to do something useful.

          For example, a function named runGame() could look like this:

          This function calls three other functions to do its job, but it still has one goal: to run the game until the player dies.

          There’s nothing special about calling a function from main() as opposed to some other function.

          • Chris

            Thank you for the replies! I was thinking more about for ease of comprehension rather than special-ness but I think I understand better now. I thought you meant that calling playAgain() in my program inside my playGame() function made it convoluted. Rather, it was the logic statements which handled more than one purpose. Thank you so much for these tutorials, that’s awesome of you! Have a great day!

  • Ola Sh

    Your solution is really good especially in terms of performance and understandability. I would like to know what you think about my solution for future references. I understand I should have called the function for the hi-Lo game preferably once. Thanks ahead.

  • J3ANP3T3R

    on test 1 :

    bool calculateAndPrintHeight(double initialHeight, int seconds)  // Changed from void to bool
        double height = calculateHeight(initialHeight, seconds);
        printHeight(height, seconds);

        if (height <= 0.0)      // Added condition
            return 1;    // on the ground
            return 0;       // Still flying

    int main()
        const double initialHeight = getInitialHeight();

        int sec{ 0 };                                             // Added variable for the loop
        while (!calculateAndPrintHeight(initialHeight, sec))      // print till ball hits the ground
        return 0;

    is the result the same ?

    • J3ANP3T3R

      Weird … i get the same number 80% of the time.

      by the way my visual studio 2015 community edition is going to expire in 14 days i thought it was free :3

      • Alex

        Visual Studio is free beyond 30 days if you register an account with Microsoft. There should be some way to do that through the application.

    • Alex

      Looks like it would work the same. You really should return true and false instead of 1 and 2.

      It’s also not intuitive what the meaning of the return value of your calculateAndPrintHeight function is.

  • Hussain

    Thanks Alex for the amazing tutorials!

    I have a question with regard to Q1 if I may:

    I changed the manual seconds inputs (in the main function) with the following loop:

    I didn’t change anything else. Is that an acceptable approach?!

    • Alex

      Depends on what you mean by acceptable. It may produce the right answer, but it’s a bit inefficient since calculateAndPrintHeight() is calculating the height to do its job, and then you’re calling calculateHeight() again. That’s a bit redundant.

  • Nyap

    insult my work plz

    question 1

    question 2

    I feel like i’ve rushed this chapter, so I know that there has to be at least 1 thing wrong with them xD
    I’ll go over this chapter again tommorow

  • joe

    I found that if you change calculateAndPrintHeight to a double, and return height, you can run this in main and it works just fine.

    int main()
        const double initialHeight = getInitialHeight();

        int seconds(0);
        while (calculateAndPrintHeight(initialHeight, seconds) >= 0)
        return 0;

  • Vlad

    Hello again

    …and sorry for the persistence. So far, I have read your tutorial page by page, never going forward until the current lesson has sunked in. There may be some vague feelings but I suspect they’ll be gone with enough practise.

    For every page that had a quiz, I made sure I solved it without looking back in the lessons (one or two exceptions where the compiler complained about my syntax…), or in the q&a, or cheat by peeking under the hood. Again, I didn’t move forward until I have solved the problem.

    But these were fairly simple, the only reward was getting to the next chapter. This quiz, however, proved more challenging, because there already is quite a bit of knowledge to juggle with -- this coming from someone who only knew a few miligrams of programming, and not even those well enough. But I kept to my stubborn way and, well, I did it. I admit it took me a day (on-off, as time allowed), and towards the later part there was more hammering than logic, but I did it.

    Why am I saying this? Because, at this point, while I am comparing myself with the official result and others from the q&a, I am left wondering about how well I actually did. So, Alex, if I post the code, could you please be [extra-]critic about my results? I could really use some form of feedback, good or bad.

    Quiz 1 (additional printing of time@touchdown):



    Quiz 2 (additional choice after executing the program and some exceptions):


    I apologize if this turns out to be a very lengthy post.

    • Alex

      Without commenting on specific items, I’ll say that for where you are in the tutorials, I think you’re doing a reasonably good job. I like that you’ve clearly labeled your “hacks” or things that you’re uncertain about. I don’t see anything egregious here, just a lot of room for optimization.

      • Vlad

        Thank you, I have noone to turn to for answers here so even a "no" would have meant a feedback. Fortunately, it’s more than that. 🙂 I know I am far from having the current chapter level, but, maybe with enough practise…

        I wanted to try a Newton-Raphson solver, but I don’t know yet how to parse a string and extract the terms into an array or some sort. I don’t see a 20th order equation entyered, term by term, with enter after each one, only to realize you made a mistake at term 19. Stackexchange has solutions, but they go over my head now, and I took a peak at the index only to reveal that I probably won’t be having any solver until chapter 13. Ah well, I have patience.

        Thank you for making this *understandable*.

        • Alex

          You’re welcome. Extracting stuff from a string is harder in practice that it seems like it should be, especially if the format of the string can vary.

          • Vlad

            I thought it might be so judging by the amount of answers, each pulling its own way and everyone having very different arguments to each. OTOH, it’s tempting to think that people using the program would not have the intent to crash it, instead of finding out the roots. But Murphy did have some excellent points…

  • Nicolas

    Hey, after first creating a very complex but functional program for question 1, I came up with this final solution. I was just wondering what you thought about it, and if you would have change anything (I know i should have put the constant in a header file but I didn’t for simplicity)

    • Alex

      My suggestions for improvement:
      1) Give your variables better names
      2) A few comments explaining what your functions are doing would go a long way
      3) You have redundant calls to calculateHeight(). This isn’t necessarily a problem, but is inefficient.

  • Arkadiusz Kupracz

    How can my code be improved?

    • Alex

      You could use a do-while loop instead of a while loop to remove the redundant call to playGame(). You could use demorgan’s law to distribute the ! sign inside playAgain(). srand() should only be called once at the start of the program, not every time a random number is picked.

  • Bw793

    Hi Alex! I just want to thank you for your great work with the lessons, which are clear and well based. Thanks also for your dedication to clarify doubts!

  • hussein

    Hey Alex, can you look at this code and tell me what you think

    #include "stdafx.h"
    #include <iostream>
    #include "constants.h"

    [using namespace std;
    using namespace constants;
    double distanceFallen(double initialHeight, int t)

        return initialHeight - (gravity*t*t) / 2;
    int main()

        cout << " Enter initial height of the ball: ";
        double initialHeight;
        cin >> initialHeight;
        int count = 0;
        for (t = 0; distanceFallen(initialHeight, t) >= 0; t++)
            cout << " At " << t << " seconds the ball is at height " << distanceFallen(initialHeight, t) << endl;
        cout << " …And finally the all hits the ground in " << t << " seconds " << endl;


  • hussein

    actually that was my question, the code compiles and i don’t know why it doesn’t give me error!

    • Alex

      Did you declare a variable named t somewhere in constants.h? If you did, then it would be getting copied into your main file since you #include constants.h.

      That’s the only thing I can think of.

  • Damien

    Hi Alex,

    If you have time, would you please be able to give some critique on my solution for question 2. I didn’t see the hints before i did it so my answer is a bit different. My main concern is that my program is hard to follow, with functions being inside the loops and it causing the path to jump around a bit. Still, this exercise was fun.

    Thanks again!




    • Alex

      Your program actually looks pretty good, except for one thing. playGame() calls anotherGame(), which calls playGame(), which calls anotherGame()… If a user were to play this game enough times, the program would eventually run out of stack space and crash.

      It would be better to add a while loop into main() to continually call playGame() until anotherGame() returns false. That way, anotherGame() doesn’t explicitly have to call playGame().

  • Logan

    Hey Alex. Is this okay for the ball drop? I modified it a little so it can handle initial velocity.