C++ programming question 2. For this problem, you will begin creating a program
ID: 3763756 • Letter: C
Question
C++ programming question
2. For this problem, you will begin creating a program that will allow a person to play Yahtzee. You will only do one small part of the program, however. Your job will be to:
Use the random number generator, seeded with time, to assign 5 random numbers(1-6) into an array
Print the five dice numbers to the screen
Ask the user to select which dice they’d like to keep
Then roll the non-selected dice again
Print the five dice numbers to the screen
Again, ask the user to select which dice they’d like to keep
Then take the final roll of the non-selected dice
Print the final dice rolls to the screen.
We’ll work on this more soon. Output should look like this:
You should use an integer array for the dice and an character array for whether you keep the dice or not. Everything can be done in main.
Explanation / Answer
#include<iostream>
#include<time.h>
using namespace std;
int main() {
int arr[5];
char flag[5];
int len=5;
srand(time(NULL));
int n=3;
for (int i=0; i<len; i++) {
flag[i]='y';
}
while (n>0) {
for (int i=0; i<len; i++) {
if (flag[i] == 'y') {
arr[i] = rand()%5+1;
}
}
for (int i=0; i<len; i++) {
cout<<arr[i]<<" ";
}
cout<<" ";
if (n>1) {
cout<<"Enter which dices to keep with y/n seperated by spaces: ";
for (int i=0; i<len; i++) {
cin>>arr[i];
}
cin.ignore();
}
n--;
}
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.