In: Computer Science
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24.
You are not allowed to use arrays. Declare four variables to hold the card values.
Required program in c++ -->
#include <iostream>
#include <random>
int main()
{
int sum=0,count=0;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr(1, 13); //range of
random number 1-13
int a=distr(gen); //generates a random number &
stores the value in a
int b=distr(gen); //generates a random number &
stores the value in b
int c=distr(gen); //generates a random number &
stores the value in c
int d=distr(gen); //generates a random number &
stores the value in d
std::cout<<"Random card selected is: "<<a<<"\n";
//print random number of a
sum=sum+a; //value of a is added in sum
if(sum<=24){ //if sum is smaller or equal to 24
count++; //then count increases by 1
}
std::cout<<"Random card selected is: "<<b<<"\n";
//print value of b
sum=sum+b; //add b in sum
if(sum<=24){ //if sum is smaller or equal to 24
count++; //count increases by 1
std::cout<<"Random card selected is: "<<c<<"\n";
//print value of c
sum=sum+c; //add c in sum
if(sum<=24){ //if sum is smaller or equal to 24
count++; //count is increased by 1
std::cout<<"Random card selected is: "<<d<<"\n";
//print value of d
sum=sum+d; //d is added in sum
if(sum<=24){ //if sum is smaller or equal to 24
count++; //value of count increases by 1
std::cout<<"It takes
"<<count<<"counts"<<"\n"; //print count
}
else{
std::cout<<"It takes
"<<count<<"counts"<<"\n"; //print count
}
}
else{
std::cout<<"It takes
"<<count<<"counts"<<"\n"; //print count
}
}
else{
std::cout<<"number of picks that yields the sum of 24 or less
is : "<<count<<"\n"; //print count
}
}