Pick a Number

Hey! Do you want to play? Yes? You’re lucky because here’s a gaming code!

Intructions:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

Yeah! A guess game! And this is the code:

pick

pick1

We’re using the same things as the past codes but now there’s something different. We need to use CYCLES with a DO-WHILE command.

What is DO-WHILE? The do-while loop use the test condition at the end of the loop.  Having the test condition at the end, guarantees that the body of the loop always executes at least one time (http://mathbits.com/MathBits/CompSci/looping/dowhile.htm).


This command will repeat the cycle until the condition that we gave becomes true. Then the loop is over. In our code we combine IF and DO to create the game. 

Another important thing is that the number has to be random. So, we use the function SRAND() and declare the variable number as the random number. To make it functional we have to introduce the TIME function too in the SRAND funtion.

SRAND() ->  (Library: <stdlib.h>)

TIME ->  (Library: <time.h>

Combining this

Continue reading “Pick a Number”

Random number generator

Our next task was to build a random number generator wth a C++ program. This was the first time I ever did something like that. I tried unsuccesfully to do it by myself at first, but I just couldn´t figure out what to do. So I decided to check what the Oracle of Google had for me. I came up with this information, basically the first webpage on the list. I also checked out a video from my  Yankee tutor on Youtube, thenewboston. 

The program basically works with a do while loop, so you can keep guessing until you hit the correct number. Also, I you miss, it is programmed to tell you wether you gave a higher number or a lower number. I did this with an If cycle.

  • About the random number, it works with the function rand, contained in the header #include <stdlib.h>
  • In order fot the numbers to never be predictive, I used the function time, as a paramater of srand. As a paramater of time  I used 0, as thenewboston explained, this would make the time to use he number of seconds since it was developed, probably in the 70´s. PEACE.
  • So, now the numbers should be really hard to predict.
  • In order to only get random numbers between 0 and 100, we had to use the modulus 100 + 1 of the function rand()

So no more talking, the code can tell a thousend words

“Dead men tell no tales” ARGGGHH

Sorry Blackbeard, go back to the XVII century bro.

Have a look at my code:
#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;

int main ()
{
int x, y;
srand (time(0));

x = rand() % 100 + 1;

do {
cout << “Guess a

Continue reading “Random number generator”

Creating a game

#TC1017 #WSQ06

This time we had to create a simple game which basically generates a random number between 1 and 100 and it’s up to the user to try and guess what number the computer came up with. For this I created some if statements inside a do-while loop that keeps checking for an inequality between the random number and the user’s guess.

If the user doesn’t guess correctly, the programm checks whether the number guessed is inferior or superiror to the generated number, and thus informs the user. The code also keeps track of every guess and when guessed correctly displays the user how many tries he made.

wsq06.proof.PNG

This problem has been the most complex so far, since it requieres the implementation of some logic, but the tricky part was finding out how to generate a random number and keen the program running until the user guesses correctly.

I checked online how to generate a random number first, and it turned out you need to include some extra “files” into the code, as well as a specific function to generate te number and keep it between the given range.

For keeping the program running as along as the number isn’t guesses I asked Bauer for help and he instructed me to write an inequality as an argument for the while statement.

Once this was done all that was left was correcting the number of tires that were displayed to the user, since I didn’t count the very first try, so my programm was only showing one try. In the end, I decided to cheat a little and just add a +1 to the number os tries displayed. It works just fine 😛

Source code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main (void){

srand (time(NULL)); //truly

Continue reading “Creating a game”

WSQ06 – Pick a Number

código:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
int num1,num2,i=1;
char des;
cout <<“nI have a number chosen between 1 and 100.”<<endl;
num1=rand()%100 + 1;
//cout<<“n”<<num1;
cout <<“nPlease guess a number between 1 and 100:”;
cin>>num2;
while (num1!=num2)
{
if (num1 < num2)
{
cout <<“nI am sorry but “<< num2 << ” is too high, try again:”;
cin>>num2;
}
else
{
cout <<“nI am sorry but “<< num2 << ” is too low, try again:”;
cin>>num2;
}
i++;
}
cout <<“nYou got it! The right answer is indeed”<<num1;
cout <<“nYou made “<< i << ” guesses to get the right number.”<<endl;
getch();
return 0;
}

Pick a Number

This program picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

Note:

For random integer you need use the libraries

NumRand librerias

You need use the second library because, if you don’t use the program pick the same number.

WSQ06-1WSQ06-2

#WSQ06

This is my code for this assignment:

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(){
int num, c, usuario;
srand(time(NULL));

for(c = 1; c <= 100; c++)
{
num = 1 + rand() % (101 – 1);
}
cout<< num << endl;

do{
cout << “Tengo un número escogido entre el 1 y el 100, trata de adivinarlo”<< endl;
cin>>usuario;

if (usuario<num){
cout << “El numero es muy bajo, intenta otra vez” << endl;
}
else
if(usuario>num){
cout << “El numero es muy alto, intenta otra vez” << endl;
}
} while(usuario!=num);

if(usuario==num){
cout<<“Correcto! El número sí es “<< num << endl;
}

return 0;
}

 

PICK A NUMBER

Sobre el srand(time(NULL))

Genera números aleatorios, se usa en el encabezado, antes de la declaración de variables.

Este programa lo que realiza es seleccionar un número aleatorio del upickno al cien, en función a esto el usuario inserta el número, si es menor el programa lo indicara tanto como si es mayor al número elegido por el sistema.

 

IMG_2693

WSQ06 Pick a number

Now, in this program, you will try to guess the number I´m thinking in.

First, I will assign a value to the number. Then you´re going to ingress a number and the program will tell you if the number is smaller or bigger than my number.

And here is the code:

Captura de pantalla 2016-01-26 a las 10.44.49 a.m.

And here is when it runs:

Captura de pantalla 2016-01-26 a las 10.45.34 a.m.

The link to the code on GitHub is:

https://github.com/Chema1807/TC101/blob/master/Pick%20a%20number

dD88anO

TC101

WSQ06

Pick a Number #WSQ06

Hello there, for making this program right I did run into many problems, but now is done. I did need to search for the condition of loops that C++ uses and I found out that FOR was the one that works works for me. Here is the link FOR LOOP.

The images of the final program:

Click to view slideshow.

Here is my code link to github:

https://gist.github.com/batmantec/af9e2bff8a77495a3445