This program that I am confused on is supposed to use a vector of integers to ke
ID: 3534404 • Letter: T
Question
This program that I am confused on is supposed to use a vector of integers to keep up with rolls of a five dice. It should roll the five dice and store the sum of the dice in the vector using the push_back() function. It should then continue rolling the dice until it rolls a 5 of a kind. Insert that final roll into the vector, and then use a loop to compute the sum and average of the elements in the vector. The output should include: the number of rolls it took to get 5 of a kind and the average value of a roll. NOTE: the program should have two functions: (1) a function that sums 5 integers. (Use it to compute the sum of the 5 dice you roll) and (2) a function that returns a bool to determine if the roll is 5 of a kind. Below are function prototypes for the functions:int sum5(int n1, int n2, int n3, int n4, int n5);
bool fiveOfAKind(int n1, int n2, int n3, int n4, int n5);
Please help THANKS
Explanation / Answer
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;
int sum5(int n1, int n2, int n3, int n4, int n5)
{
return n1+n2+n3+n4+n5;
}
bool fiveOfAKind(int n1, int n2, int n3, int n4, int n5)
{
return (n1==n2 && n1==n3 && n1==n4 && n1==n5);
}
int main()
{
srand(time(NULL));
vector<int> diceRoll;
int n1=1,n2=2,n3=3,n4=4,n5=5;
while(!fiveOfAKind(n1,n2,n3,n4,n5))
{
n1 = rand()%6 +1;
n2 = rand()%6 +1;
n3 = rand()%6 +1;
n4 = rand()%6 +1;
n5 = rand()%6 +1;
diceRoll.push_back(sum5(n1,n2,n3,n4,n5));
}
int sumRoll = 0;
float avgRoll;
for(int i=0;i<diceRoll.size();i++)
{
sumRoll += diceRoll[i];
}
avgRoll = sumRoll*1.0/diceRoll.size();
cout<<"Number of rolls : "<<diceRoll.size()<<endl;
cout<<"Average value per roll : "<<avgRoll<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.