Have a nice week.
flickr photo by Christopher Ludtke
https://www.flickr.com/photos/imhof89/22112428156/
Some Rights Reserved
Have a nice week.
flickr photo by Christopher Ludtke
https://www.flickr.com/photos/imhof89/22112428156/
Some Rights Reserved
Me tope con pared con este programa tuve que ir a asesorias para poder lograrlo
]]>So for this WSQ, I had to check the <ctime> library and why it worked for assigning random numbers, several times. It ended up being quite easy, as with C++ the same thing can be done in different ways (Thank you cplusplus.com!).
Here’s the GitHub link to WSQ 6.
]]>Link to my code:
]]>Here is my code:
import random
numCom = random.randrange(1, 101)
numUs = int( input( "Give me a number between 1 and 100: "))
attempt = int(1)
while numCom != numUs :
attempt = attempt + 1
if (numCom < numUs) :
print( "My number is smaller.")
numUs = int( input( "Give me a another number: "))
else :
print ( "My number is greater.")
numUs = int( input( "Give me a another number: "))
print(numCom,"was the right number!")
print("You needed",attempt,"attempts.")
]]>Here is my code, you have to guess a number that is randomly selected by the program. I did it with no help (except for the time/rand thing).
Hope this helps you!
using namespace std;
int main() {
srand(time(0)); //time changes, the number changes
int r=1+(rand() % 100);
int x;
int count = 0;
cout
cout
cin >>x;
count = count+1;
while (x!=r){
if(x
cout
cin >>x;
count = count + 1;
} else {
if (x>r){
cout
cin >>x;
count = count + 1;
}
}
}
cout
return 0;
}
]]>Here is my code, you have to guess a number that is randomly selected by the program. I did it with no help (except for the time/rand thing).
Hope this helps you!
using namespace std;
int main() {
srand(time(0)); //time changes, the number changes
int r=1+(rand() % 100);
int x;
int count = 0;
cout
cout
cin >>x;
count = count+1;
while (x!=r){
if(x
cout
cin >>x;
count = count + 1;
} else {
if (x>r){
cout
cin >>x;
count = count + 1;
}
}
}
cout
return 0;
}
]]>You can make this program using -while- or -for-, if you want it to be easy. I found -for- a nice way to make it.
To generate a random number and understand how that functions work, this video helped me a lot: https://www.youtube.com/watch?v=XfHI4ePUHc4
Also page 103 and 104 of the book.
Here is the link to GitHub: https://github.com/tonygarciavz/A01630861-TC1017.git
]]>Here the image with the program and the compiler running it:
Gaze at the program, stare at it as they are no other thing to observe.
I am going to explain it quickly, but if you still having doubts, search for yourself, and if it is not clear yet, ask me and then I am going to help you.
We include <stdlib> and <time> (for this example is cstdlib and ctime, but it depends on the compiler), because we need to use the functions rand(), srand() (from <stdlib>) and time() (from <time>).
Rand() is supposed to create a random number, but it is always the same value, and that is why we have to include sradn() (refers to seed random), because from that function the rand() takes like a reference for create a number. In this case, between the parenthesis of the srand() function, we put the time() function, and in this way, the random number will be generated based on a value given by the current time of the computer.
It is important to put the “%” after the rand(), because in this way you are telling the function to generate a number into a range. In this case we want to set the range between the number 1 and 100, after the “%” sign, we put the number of spaces that we are going to use, plus the initial number. For the first value, took the initial number too, that means that the final value that you want to put is the upper bound plus one, and then “+” before the first value.
There is an error in the program of the image, try to find it.
That’s all for this WSQ, here is the code (the correct one): https://github.com/hrglez/TC1017/blob/master/WSQ06%20-%20Random%20Number
]]>Here the image with the program and the compiler running it:
Gaze at the program, stare at it as they are no other thing to observe.
I am going to explain it quickly, but if you still having doubts, search for yourself, and if it is not clear yet, ask me and then I am going to help you.
We include <stdlib> and <time> (for this example is cstdlib and ctime, but it depends on the compiler), because we need to use the functions rand(), srand() (from <stdlib>) and time() (from <time>).
Rand() is supposed to create a random number, but it is always the same value, and that is why we have to include sradn() (refers to seed random), because from that function the rand() takes like a reference for create a number. In this case, between the parenthesis of the srand() function, we put the time() function, and in this way, the random number will be generated based on a value given by the current time of the computer.
It is important to put the “%” after the rand(), because in this way you are telling the function to generate a number into a range. In this case we want to set the range between the number 1 and 100, after the “%” sign, we put the number of spaces that we are going to use, plus the initial number. For the first value, took the initial number too, that means that the final value that you want to put is the upper bound plus one, and then “+” before the first value.
There is an error in the program of the image, try to find it.
That’s all for this WSQ, here is the code (the correct one): https://github.com/hrglez/TC1017/blob/master/WSQ06%20-%20Random%20Number
]]>