Pick a number improved

--Originally published at Hensel

In the last week, I wrote the code for this program but I don´t use the srand command and I had a problems with it. Because when I ran my code the programm couldn´t run. but reviewing daga2017´s blog, I could see my mistake that it was a lowercase letter problem of the word null. This had to be written in capital letters. Then I can run my code and here I leave it.

code-pickcywing-pick


WSQ03

--Originally published at blog de Horacio

What to do:

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.

Progress:

First we looked for the way to create the random number, we found a good post that helped us a lot. Then we started reading a little more about counters and the while function that was the one that made me more suitable for my program. After a bit of analysis finally came out the program but I have a fault in the counter that I did not know how to remove it, I hope Ken can explain to me.

captura-de-pantalla-2017-02-01-a-las-23-39-51

Sources of research:

http://blog.martincruz.me/2012/09/obtener-numeros-aleatorios-en-c-rand.html

http://www.aprenderaprogramar.com/index.php?option=com_content&view=article&id=931:bucles-en-lenguaje-c-estructura-de-repeticion-condicion-contador-ejemplos-tabla-de-multiplicar-cu00533f&catid=82:curso-basico-programacion-lenguaje-c-desde-cero&Itemid=210

Code:

#include <stdlib.h>
#include <time.h>
#include<iostream>
using namespace std;
int main() {
int num,resp,intentos;
srand(time(NULL));
num=1+rand()%(101-1);
do {
std::cout << “ya estoy listo para jugar” << ‘\n’;
std::cout << “¿cual numero crees que tengo para ti?” << ‘\n’;
std::cin >> resp;
intentos++;
if (num<resp) {
std::cout << “lo siento, pero tu numero es muy alto” << ‘\n’;
}
if (num>resp) {
std::cout << “lo siento, pero tu numero es muy bajo” << ‘\n’;
}
}

Continue reading "WSQ03"

cout<< “Hit the #” <<endl;

--Originally published at Alvaro_246

WSQ03 = PICK A NUMBER

Esta semana hice un programa que se encarga de preguntarle al usuario un numero del 1 al 100 y tiene que adivinar el numero de un cierto rango intermedio, en el caso de mi programa el rango correcto es (num<=46 || num>=54). Para ello utilice una función llamada “while” la cual no conocía, pero sirve para hacer un Loop infinito hasta que se rompa la condición dentro del while. En varias ocasiones no me funcionaba mi código, le pedí ayuda a uno de mis amigos que estudia ISC (Ingeniería en sistemas comunicacionales) Hector Chavez Morales y me ayudo a encontrar el error.(estaba mal el rango).

pick-a-number

 


Guess the number

--Originally published at my programming blog

The assignment is to create a program in which  you need to guess the number the computer was thinking, and if you get it wrong the program tells you if its too high or to small and when you get it right it tells the number of times it took you to get it right.

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main (){
int x, secret,y=0;
srand(time(NULL));
secret=(rand()%100+1);
cout<< “I chose  a number between 1-100, can you guess it?: “<<endl;

do {
cout<<“Write your guess:”<<endl;
cin>>x;
y=y + 1;
if(x>secret){
cout<<“Wrong! ” <<x<< ” is too high, try again.”<<endl;
} else {
if (x<secret){
cout<<“Wrong! “<<x<< ” is too low, try again.”<<endl;
}
}
}while(x!=secret);
cout<<“Congratulations you got it right!!”<<endl;
cout<<“It took you “<<y<<” times to get it right.”<<endl;
return 0;
}

screen-shot-2017-01-31-at-7-40-53-amscreen-shot-2017-01-31-at-7-42-10-am

  1. I included the libraries “cstdlib” and “ctime” so that I could use the srandom function and the time function.
  2. Then I named my variables (the integers “x”, “y” and “secret”)
  3. I wrote the function of srand and inside the parenthesis I wrote time(NULL) so that the random number is always different.
  4. Then I equaled the integer “secret” to the random function “rand” and put %100 +1 to specify that I want a number between 1 and a 100.
  5. I added the cout to ask the user for a number.
  6. I made a loop called do-while, this means that while a condition is true this loop will repeat, so I did it with the condition that x!=secret, so while the user doesn’t guess the secret number this will repeat itself.
  7. Inside the loop I added an if so if x>secret the program tells you your number is too high, and another if so if
    Continue reading "Guess the number"

pick a number

--Originally published at My life @ TEC

This program actually gave me a hard time, because I didn’t know how to declare a random number, so I read the book, I looked for answers online, I asked my dad and I also asked a friend, so when I finally understood how to do it, everything was easy from there until I tried to give the user chances to guess the number. I had to erase all the code to start again, it’s just that I wasn’t concentrated at all so it was hard, at the end I relaxed and it was easier that way.

Here’s my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int num1,num2,cont;
std::string resp;
do{
srand(time(NULL));
num1=1+rand()%(101);
cont=9;
cout<<“Hey! I’m thinking of a number between 0 and 100, guess wich one it is, you would have 10 chances to guess.”<<endl;
cin>>num2;
while (num2!=num1 & cont>0){
cont=cont-1;
if (num2>num1){
cout<<“sorry but “<<num2<<” is too high, try again.”<<endl;
cin>>num2;
}
else{
cout<<“sorry but “<<num2<<” is too low, try again.”<<endl;
cin>>num2;
}
}
if(num2!=num1){
cout<<“You couldn’t do it ? the number I was thinking was “<<num1<<endl;
}
else{
cout<<“¡Congratulations! “<<num2<<” is actually the number I was thinking”<<endl;
}
cout<<“Would you like to play again? (yes/no)”<<endl;
cin>>resp;
}while(resp!=”no”);
return 0;
}

and here’s how it works:

img_1483


pick a number

--Originally published at My life @ TEC

This program actually gave me a hard time, because I didn’t know how to declare a random number, so I read the book, I looked for answers online, I asked my dad and I also asked a friend, so when I finally understood how to do it, everything was easy from there until I tried to give the user chances to guess the number. I had to erase all the code to start again, it’s just that I wasn’t concentrated at all so it was hard, at the end I relaxed and it was easier that way.

Here’s my code:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
int num1,num2,cont;
std::string resp;
do{
srand(time(NULL));
num1=1+rand()%(101);
cont=9;
cout<<“Hey! I’m thinking of a number between 0 and 100, guess wich one it is, you would have 10 chances to guess.”<<endl;
cin>>num2;
while (num2!=num1 & cont>0){
cont=cont-1;
if (num2>num1){
cout<<“sorry but “<<num2<<” is too high, try again.”<<endl;
cin>>num2;
}
else{
cout<<“sorry but “<<num2<<” is too low, try again.”<<endl;
cin>>num2;
}
}
if(num2!=num1){
cout<<“You couldn’t do it ? the number I was thinking was “<<num1<<endl;
}
else{
cout<<“¡Congratulations! “<<num2<<” is actually the number I was thinking”<<endl;
}
cout<<“Would you like to play again? (yes/no)”<<endl;
cin>>resp;
}while(resp!=”no”);
return 0;
}

and here’s how it works:

img_1483


#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp

--Originally published at Solving Problems with Programming

Link of the picture: Link of the picture

So in this sixth class that I had on last Monday I started how to make a program with loops in C++.#Mastery13

What I did for this numeric program is solving the problem to the user by writing a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen , I chose one method that best works for me.

After that, inside  the program 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. I did something extra here including telling the user how many guesses the user had to make to get the right answer.

I checked that my program doesn’t always use the same random number chosen by splitting my problem solving into parts.

At first I only generated the random number and printed that as a first step.

 

The following photograph shows the solution to this problem:

wsq3

Picture of the author.

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp and #WSQ02 Post Temperature 23/01/17 and WSQ02.cpp where i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library <iostream> to call all the fuctions of inputs and outputs of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08

wsq3-v2
wsq3v3
wsq3v4
wsq3v5
wsq3v6
wsq3
Continue reading "#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp"

Your chance to make the first impression

--Originally published at Programming Path

Hello! The task, #WSQ03, that I’m publishing covers the #Mastery12 and #Mastery13.

I’m not that inspired, but I will try to do the best.

The activity was to do a program that choose a random number and the user tries to guess it. To do this, I needed to insert a new library <cstdlib> because this one has the code to generate random numbers. Also, I used do…if…while because I had to put conditions.

Here is picture of the code:

random

Here is just the code:

#include <iostream>
#include <cstdlib>
using namespace std;

int main () {
int a, b, c=0;
srand(time(NULL));
cout << “I picked a number between 1 and 100.” << endl;
cout << “Guess the number I chose: ” << endl;
a = rand()%101;
do {
c = c + 1;
cin >> b;
if (b > a) {
cout << “Wrong! Your number is too high, try again: ” << endl;
}
else if (b < a) {
cout << “That’s not the one. Your number is too low, guess again: ” << endl;
}
else if (b = a) {
cout << “Excellent! You did it! That’s the right number!” << endl;
}
} while (a != b);
cout << “Your total of guesses: ” << c;
return 0;
}

Thanks for reading.


I have a number chosen between 1 and 100.

--Originally published at Loading…

The activity was:

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

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.

So the first step was to remember how to generate random numbers, and as always I searched it in St.Google, and I found a very good page that helped my a lot. I add the <ctime> and <cstdlib> libraries to make the program works. Then I established my 2 variables with int, I wrote srand(time(NULL)), for make the number always random, and the equation.

To make that the program continue until the user guesses I add a do/while function, that includes an if function to set the parameters and give the hints of ‘too high’, ‘too low’ or BRAVO, if you guess. Before to making something more pro, I wanted to be sure that it works correctly, I compiled and guess what… It didn’t do it, you will never guess the number, i don’t know why but it change it every time, so I looked for help and I found it in a classmate’s blog, he has a char command with a condition, so I try it in my program and it works, so that was the solution. At the final I add an extra variable to count the opportunities, and an if to show if your a loser or a winner. This is my code at the final:

randrand02