Is there some way to write a condition within a while loop that creates output if the user guesses a number that is within 10 units (plus or minus) from a random number generated by the program (integers)?
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand ( time(NULL) );
bool valid;
int randNum;
int sum = 0;
int userNum;
for (int x = 1; x < 11; x++)
{
randNum = rand() % (71) + 7;
cout << "Random number " << x << ": " << randNum << endl;
sum = sum + randNum;
}
cout << "\nThe total of all the random numbers is " << sum << "\n\n";
cout << "Guess a number between 70 and 770: ";
do
{
cin >> userNum;
while(userNum >= 70 && userNum <= 770)
{
while(userNum == sum)
{
cout << "You win";
break;
}
while(/* the number given by the user is within 10 units from the random number generated by the program*/)
{
cout << "You almost won";
break;
}
break;
}
while(userNum < 70 || userNum > 770)
{
cout << "Try again.";
valid = false;
break;
}
}
while (!valid);
return 0;
}
You will have to #include <stdlib.h> then for the condition in your while loop you write abs(userNum - randNum) <= 10. This will give the magnitude of the difference between the userNum and randNum, which you want to be less than or equal to 10.
Related
I'm attempting to list a set of prime numbers from a lower bound to an upper bound limiting the number of prime numbers in a row to 8. Though I have done the first part, I can't get them to list in rows with only 8 prime numbers per row.
#include <iostream>
enter code here
int main()
{
int low, high, i, flag, j;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
while (low < high)
{
flag = 0;
for (i = 2, j = 1; i <=low/2; +ii, ++j)
{
if (j == 8)
{
cout << "\n";
j = j - 7;
}
else if (low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
cout << low << " ";
++low;
}
return 0;
}
It works for the first row, then everything else seems to start listing rather than being in a row.
Output: Enter two numbers(intervals): 1
200
Prime numbers between 1 and 200 are: 1 2 3 5 7 11 13 17
19
23
29
31 ...
It's a little late, but I said I'd do it. My suggestions:
#include <iostream>
//enter code here
using std::cin;
using std::cout;
int main()
{
int low, high, count, i;
bool flag; // bools are more suited to being flags
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
count = 1; // I replaced j with this for ease of reading
while (low < high)
{
flag = true;
// using break in loops is not recommended, and you already have a flag
for (i = 2; i <= low / 2 && flag; ++i)
{
if (low % i == 0)
{
flag = false;
}
}
if (flag)
{
cout << low;
if (count == 8)
{
cout << std::endl;
count = 1;
}
else
{
cout << " ";
++count;
}
}
++low;
}
return 0;
}
Your code divide not every 8 prime numbers, but every 8 attempts of dividing a number during search for prime number. So any prime that is 8 or more values away from previous will generate a line break. Consider following fix:
#include <iostream>
using namespace std;
int main()
{
int low, high, i, flag, j;
cout << "Enter two numbers(intervals): ";
cin >> low >> high;
cout << "Prime numbers between " << low << " and " << high << " are: ";
j = 0;
while (low < high)
{
flag = 0;
for (i = 2; i <=low/2; ++i)
{
// Removed here
if (low % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
++j; // Added here
cout << low << " ";
}
if (j == 8) // and here
{
cout << "\n";
j = j - 8;
}
++low;
}
return 0;
}
By the way, you should end a search when reaching square root of low, not low / 2. The loop will be much faster.
/*Instead of increment 1 or decrease 1 the random number in the for loop, I would like the code to generate a random integer higher than the previous random generated if guess is higher and to generate a random integer lower than the previous random generated if the guess is lower./
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
int main(){
int counter = 0;
unsigned int guess;
srand(time(NULL));
unsigned int random_number = (rand() % 100)+ 1;
std::string higher = "higher";
std::string lower = "lower";
std::string value = "";
cout << "Please enter a number between 1 and 100, so the computer can guess it!" << endl;
cin >> guess;
cout << random_number << endl;
while (guess != random_number)
{
cout << "Is it a lower or higher value?" << endl;
cin >> value;
if (value == higher)
{
for(int i = 1; i< 2; i++)
{
random_number = random_number + 1;
cout << random_number << endl;
}
}
else if (value == lower)
{
for(int i =1; i < 2; i++)
{
random_number = random_number - 1;
cout << random_number << endl;
}
}
}
cout << "This PC is fabulous" << endl;
return 0;
}
You just need to call random with your new limits.
To make things easier let us write a function that generates integers between any two limits we specify, say low_limit and low_limit.
int random(int low_limit, int up_limit){
return rand() % ((up_limit - low_limit) + 1) + low_limit;
}
Now generate the first number by calling the function and save the result into a temp variable.
temp = random (0, 100);
Now upon user input you can use temp to call random like this:
if (value == higher)
temp = random (temp, 100);
else if (value == lower)
temp = random (0, temp);
For different random numbers each run you can use srand function with a parameter that can give a different seed each time like system time.
srand(time(NULL));
note: Use the proper headers.
UPDATE
Based on your comments, you can use extra two variables say: minguess and maxguess to narrow your random generation range. check the following piece of code:
srand(time(NULL));
int choice, minguess = 0, maxguess = 100, current;
do{
choice = 0;
current = random(minguess,maxguess);
cout << "\n My guess is: " << current << "\n";
while (choice < 1 || choice > 3){
cout << "\n Enter you choice: \n";
cout << " 1- higher. \n";
cout << " 2- lower. \n";
cout << " 3- quit. \n\n";
cin >> choice;
}
if (choice == 1)
minguess = current;
else if (choice == 2)
maxguess = current;
} while (choice != 3);
I know I'm missing something real simple but I can't seem to get the numbers to print out in rows of just odd or just even numbers using a while loop or loops. Also It keeps printing out "the even numbers are:"/ "the odd numbers are:" for every number.
#include<stdio.h>
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
while (n <= 100) //loop only if n equals 100 or less
{
for(number = n; number <= n; number++) //for loop to increment int value
{
if(number % 2 !=0) //determines if odd
{
cout << "The odd numbers are:" <<number << endl; //prints odd values
}
}
for(number = n;number <= n; number++) // for loop to increment int value
{
if(number % 2 ==0) //determines if even
{
cout <<"The even numbers are:" <<number <<endl; //prints even values
}
}
n++;
}
return 0; //end of program
}
You may want this:
#include <iostream>
using namespace std;
int main()
{
//declare variables
int number;
int n;
cout << "Enter value less than 100: ";
cin >> n; //take user input
// print odd values
cout << "The odd numbers are:";
for (number = n + 1 - (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
// print even values
cout << "The even numbers are:";
for (number = n + (n % 2); number <= 100; number += 2)
{
cout << " " << number;
}
cout << endl;
return 0; //end of program
}
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 20: 20 10 5 2 1
#include <iostream>
using namespace std;
int main()
{
int userNum = 0;
userNum = 20;
cout << userNum << " ";
while (userNum > 1)
{
userNum = userNum/2;
cout << userNum << " ";
}
cout << endl;
return 0;
}
It divides properly until I get to 0, where it gives me 0 instead of terminating. What am I doing wrong?
Thanks!
Your posted code behaves just like you expect it to.
I am going to suggest changing code a little bit as a matter of good practice.
Instead of:
cout << userNum << " ";
while (userNum > 0)
{
userNum = userNum/2;
cout << userNum << " ";
}
Use:
while (userNum > 1)
{
cout << userNum << " ";
userNum = userNum/2;
}
The general principle is:
while ( <conditional> )
{
// Use the data
// Change the data as the last operation in the loop.
}
A for loop provides natural placeholders for these.
for ( <initialize data>; <conditional>; <update data for next iteration> )
{
// Use the data
}
If you were to switch to using a for loop, which I recommend, your code would be:
for ( userNum = 20; userNum > 0; userNum /= 2 )
{
cout << userNum << " ";
}
I tried all of these options but none of them worked with the other tests therefore I made some modifications. This works perfect for zybooks.
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
userNum = 40;
cin>>userNum;
while(userNum>1)
{
userNum=userNum/2;
cout<<userNum<<" ";
}
cout << endl;
return 0;
}
http://ideone.com/SvU8z3
#include <iostream>
using namespace std;
int main()
{
int userNum = 20;
while (userNum > 0)
{
cout << userNum << " ";
userNum = userNum/2;
}
cout << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int userNum;
cin >> userNum;
while (userNum >= 4) {
userNum /= 4;
cout<<userNum<<" ";
}
cout << endl;
return 0;
}
This program runs great, but like a true casino owner. I want the payout to be as minimal as possible without adding more possibilities. The way I want to do that is to add probabilities to each number. there 8 possibilities (2,3,4,5,6,7,8,9) I want 7 to occur the least. So I want to set probabilities as 2-6 at 75% (that's 15% each) 8-9 at 20% (that's 10% each) and 7 at 5%. Can someone help me?
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int Random(int low, int high);
int Result(int a, int b, int c, int chips, int bet);
int main()
{
srand( time(0) );
int low1(2), low2(2), low3(2);
int high1(9), high2(9), high3(9);
int menu=0;
int bet =0;
bool quit=0;
cout << "Player's chips: $1000" << endl;
int chips=1000;
while (!quit)
{
cout << "1) Play slot. 2) Exit.";
cin >> menu;
switch (menu)
{
case 1:
{
cout << "Enter your bet: ";
cin >> bet;
if (bet<=0 || bet>chips)
{
cout << "You did not enter a valid bet." << endl;
menu=1;
}
else
{
int a = Random(low1,high1);
int b = Random(low2,high2);
int c = Random(low3,high3);
cout << a << " " << b << " " << c << endl;
chips = Result(a,b,c,chips,bet);
cout << "Player's chips: $" << Result(a,b,c,chips,bet) << endl;
}
break;
}
case 2:
{
cout << "Exiting..." << endl;
quit=1;
break;
}
default:
{
cout << "Not a valid menu !"<<endl;
}
}
}
if (chips <=0)
{
cout << "You have $0 ! Game Over !" << endl;
quit=1;
}
}
int Random(int low, int high)
{
int random;
random = low + rand() % ((high+1)-low);
return random;
}
int Result(int a, int b, int c, int chips, int bet)
{
if ((a==7 && b==7 && c==7))
{
cout << "You won 10 times your bet !($" << (bet*10) << ")" << endl;
chips=chips+(bet*10);
}
if ((a==b==c) && !(a==7 && b==7 && c==7))
{
cout << "You won 5 times your bet !($" << (bet*5) << ")" << endl;
chips=chips+(bet*5);
}
if ((a==b || a==c || b==c) && !(a==b==c) && !(a==7 && b==7 && c==7))
{
chips=chips+(bet*3);
cout << "You won 3 times your bet ! ($" << (bet*3) << ")" << endl;
}
else
{
cout << "You lost your bet ! ($-" << bet << ")" << endl;
chips=chips-bet;
}
return chips;
}
Introduce an array of values [0.05, 0.15, 0.25, 0.4, 0.55, 0.7, 0.85] corresponding to values 7, 8, 9, 2, 3, 4, 5. 6 is if a generated random number > 0.85.
Then get a random number r from 0 to 1.
if r <=0.05 then it is 7
else if r <= 0.15 then it is 8
else if r <= 0.1 then it is 9
...
else if r <= 0.85 then it is 5
else it is 6
You can figure out how to write it down as a loop if you want.
I would re-write the function as follows (it could be reworked into something better, but it is a start that should work):
// Generate a random number 2-9, with weighted probabilities
int Random()
{
int number; // number 1-9 to generate
int prob; // probability
// Generate probability value 0-99
prob = rand() % 100;
// Calculate number with desired probability "additive"
if (prob < 15) // 15% probability for 2
number = 2;
else if (prob < 30) // 15% probability for 3
number = 3;
else if (prob < 45) // 15% probability for 4
number = 4;
else if (prob < 60) // 15% probability for 5
number = 5;
else if (prob < 75) // 15% probability for 6
number = 6;
else if (prob < 80) // 5% probability for 7
number = 7;
else if (prob < 90) // 10% probability for 8
number = 8;
else // 10% probability for 9
number = 9;
return number;
}
Don't forget to add the following code to seed the random number generator:
srand (time(NULL));
I hope this isn't for a real slot machine by the way, there could be legal issues if it were :-)