How to go: 0 to 100, real quick.

--Originally published at Ken's Disciple 01

Picture by Stokpic Stokpic

As you may imagine, this blog will be about WSQ03, in which we were asked to write a program that randomly picked a number from 1 to 100, ask the user to try to guess it, give him “too high” and “to low” hints and also showing the amount of guesses.

This Program is kind of challenging because in order to do this you have to use a “new” function that allows the program to choose the random number, this page may help you to learn the new functions you’ll need.

You don’t have to worry about this program, it’s actually easy once you have everything you need. All right so let’s get started:

  • Starting this program is just doing the same as usual, inside your main, you’ll have to declare your variables, you’ll need 3; one for the random number that the program will choose, one for the guessed number that the user will give and the last one is the one that will tell you the amount of times you tried to guess the number.
  • The next thing you’ll have to know is the function srand(time(0)); (for more info you can visit this page.srand() gives the random function a new seed, a starting point.

    time(0) with these you’re guaranteed your seed will be the same only once, unless you start your program multiple times within the same second.

  • then you’ll use your rand function and tell the program from which numbers you want to pick a random one, here are some examples:
    v1 = rand() % 100;         // v1 in the range 0 to 99
    v2 = rand() % 100 + 1;     // v2 in the range 1 to 100
    

    As you can see the %”…” gives you with how many numbers you want to

    Captura de pantalla 2017-01-26 a la(s) 15.27.06.png
    Captura de pantalla 2017-01-26 a la(s) 16.02.48.png
    Continue reading "How to go: 0 to 100, real quick."

Quiz 03

--Originally published at Ken's Disciple 01

Picture by Unsplash Unsplash

 

In this Quiz we had to make a main routine that asked for a number and showed the square and cubic roots of that number and printed them. We had to be careful, cause we had to give a message in our square root if the number given was negative.

This is my code: (also see it on GitHub)

Captura de pantalla 2017-01-30 a la(s) 21.50.36.png

There are several things we had to do in this quiz: (there are different ways we could do this, I made it this way)

  • We had to include our library cmath, for our functions square_root and cubic_root, and of course they had to return both sqrt(x) and cbrt(x).
  • I made if’s functions, if the number given was smaller than cero, it will give the advice that de square root couldn’t be done because the number was negative, it would give the cubic root and also gives the opportunity to give another number, a positive one (that was made with a do-while function. If the number was bigger than cero, we were good to go and it would give us all the answers and it would close.

If you have any questions feel free to ask.

 

L.out