Please transform the code inside no_Of_Occ() function into other functions. #inc
ID: 3546005 • Letter: P
Question
Please transform the code inside no_Of_Occ() function into other functions.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice(); // Returns a random n&umber ranged from 1-6
void no_Of_Occu(); // Returns the conditions and the result of occurances
int main()
{
no_Of_Occu();
}
int dice_Rnd_Numb()
{
int rnd_no;
return rnd_no = (rand() % 6) + 1;
}
void no_Of_Occu()
{
srand (time(NULL));
int throws_number;
do {
cout << "How many times you wanna throw the dice ? (Please enter a number greater than 10) : ";
cin >> throws_number;
}
while(throws_number<10);
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
int result;
for (int i = 0; i < throws_number; i++)
{
result = dice_Rnd_Numb();
if (result == 1)
a++;
else if (result == 2)
b++;
else if (result == 3)
c++;
else if (result == 4)
d++;
else if (result == 5)
e++;
else if (result == 6)
f++;
}
cout << "Number of occurrences of 1 was " << a << endl;
cout << "Number of occurrences of 2 was " << b << endl;
cout << "Number of occurrences of 3 was " << c << endl;
cout << "Number of occurrences of 4 was " << d << endl;
cout << "Number of occurrences of 5 was " << e << endl;
cout << "Number of occurrences of 6 was " << f << endl;
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int throwDice(); // Returns a random n&umber ranged from 1-6
void no_Of_Occu(); // Returns the conditions and the result of occurances
int getThrowTimes();
void displayResult(int a,int b,int c,int d,int e,int f);
void throwAll(int times, int&a, int&b, int&c, int&d, int&e, int&f);
int main()
{
no_Of_Occu();
}
int dice_Rnd_Numb()
{
int rnd_no;
return rnd_no = (rand() % 6) + 1;
}
int getThrowTimes()
{
int throws_number;
do {
cout << "How many times you wanna throw the dice ? (Please enter a number greater than 10) : ";
cin >> throws_number;
}
while(throws_number<10);
return throws_number;
}
void no_Of_Occu()
{
srand (time(NULL));
int throws_number = getThrowTimes();
int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0;
throwAll(throws_number, a, b, c, d, e, f);
displayResult(a,b,c,d,e,f);
}
void throwAll(int times, int&a, int&b, int&c, int&d, int&e, int&f)
{
int result;
for (int i = 0; i < times; i++)
{
result = dice_Rnd_Numb();
if (result == 1)
a++;
else if (result == 2)
b++;
else if (result == 3)
c++;
else if (result == 4)
d++;
else if (result == 5)
e++;
else if (result == 6)
f++;
}
}
void displayResult(int a,int b,int c,int d,int e,int f)
{
cout << "Number of occurrences of 1 was " << a << endl;
cout << "Number of occurrences of 2 was " << b << endl;
cout << "Number of occurrences of 3 was " << c << endl;
cout << "Number of occurrences of 4 was " << d << endl;
cout << "Number of occurrences of 5 was " << e << endl;
cout << "Number of occurrences of 6 was " << f << endl;
}