Replace each switch statement with two or more function calls. #include <iostrea
ID: 3631146 • Letter: R
Question
Replace each switch statement with two or more function calls.#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//random number generator prototypes
void randomize(void);
void randomize(int seed);
int random(void);
int random(int upper_bound);
int random(int upper_bound, int lower_bound);
int main()
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
switch(digit_1)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_2 = random_number % 10; random_number = random_number / 10;
switch(digit_2)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
int digit_3 = random_number % 10; random_number = random_number / 10;
switch(digit_3)
{
case 0:
counter_0++;
break;
case 1:
counter_1++;
break;
case 2:
counter_2++;
break;
case 3:
counter_3++;
break;
case 4:
counter_4++;
break;
case 5:
counter_5++;
break;
case 6:
counter_6++;
break;
case 7:
counter_7++;
break;
case 8:
counter_8++;
break;
case 9:
counter_9++;
break;
}
}
cout << "0 occurs " << counter_0 << " times" << endl;
cout << "1 occurs " << counter_1 << " times" << endl;
cout << "2 occurs " << counter_2 << " times" << endl;
cout << "3 occurs " << counter_3 << " times" << endl;
cout << "4 occurs " << counter_4 << " times" << endl;
cout << "5 occurs " << counter_5 << " times" << endl;
cout << "6 occurs " << counter_6 << " times" << endl;
cout << "7 occurs " << counter_7 << " times" << endl;
cout << "8 occurs " << counter_8 << " times" << endl;
cout << "9 occurs " << counter_9 << " times" << endl;
system("pause");
return 0;
}
//random number generators
void randomize(void)
{
srand(unsigned(time(NULL)));
}
void randomize(int seed)
{
srand(unsigned(seed));
}
int random(void)
{
return rand();
}
int random(int upper_bound)
{
return rand() % (upper_bound + 1);
}
int random(int upper_bound, int lower_bound)
{
if(upper_bound < lower_bound)
{
int t = upper_bound;
upper_bound = lower_bound;
lower_bound = t;
}
int range = upper_bound - lower_bound + 1;
int number = rand() % range + lower_bound;
return number;
}
Explanation / Answer
please rate - thanks
like this?
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//random number generator prototypes
void randomize(void);
void randomize(int seed);
int random(void);
int random(int upper_bound);
int random(int upper_bound, int lower_bound);
int count(int,int);
int main()
{
int upper_bound = 999;
int lower_bound = 100;
int n_random_numbers = 1000;
randomize();
int counter_0 = 0;
int counter_1 = 0;
int counter_2 = 0;
int counter_3 = 0;
int counter_4 = 0;
int counter_5 = 0;
int counter_6 = 0;
int counter_7 = 0;
int counter_8 = 0;
int counter_9 = 0;
for(int counter = 1; counter <= n_random_numbers; counter++)
{
int random_number = random(upper_bound, lower_bound);
int digit_1 = random_number % 10; random_number = random_number / 10;
counter_0+=count(digit_1,0);
counter_1+=count(digit_1,1);
counter_2+=count(digit_1,2);
counter_3+=count(digit_1,3);
counter_4+=count(digit_1,4);
counter_5+=count(digit_1,5);
counter_6+=count(digit_1,6);
counter_7+=count(digit_1,7);
counter_8+=count(digit_1,8);
counter_9+=count(digit_1,9);
int digit_2 = random_number % 10; random_number = random_number / 10;
counter_0+=count(digit_2,0);
counter_1+=count(digit_2,1);
counter_2+=count(digit_2,2);
counter_3+=count(digit_2,3);
counter_4+=count(digit_2,4);
counter_5+=count(digit_2,5);
counter_6+=count(digit_2,6);
counter_7+=count(digit_2,7);
counter_8+=count(digit_2,8);
counter_9+=count(digit_2,9);
int digit_3 = random_number % 10; random_number = random_number / 10;
counter_0+=count(digit_3,0);
counter_1+=count(digit_3,1);
counter_2+=count(digit_3,2);
counter_3+=count(digit_3,3);
counter_4+=count(digit_3,4);
counter_5+=count(digit_3,5);
counter_6+=count(digit_3,6);
counter_7+=count(digit_3,7);
counter_8+=count(digit_3,8);
counter_9+=count(digit_3,9);
}
cout << "0 occurs " << counter_0 << " times" << endl;
cout << "1 occurs " << counter_1 << " times" << endl;
cout << "2 occurs " << counter_2 << " times" << endl;
cout << "3 occurs " << counter_3 << " times" << endl;
cout << "4 occurs " << counter_4 << " times" << endl;
cout << "5 occurs " << counter_5 << " times" << endl;
cout << "6 occurs " << counter_6 << " times" << endl;
cout << "7 occurs " << counter_7 << " times" << endl;
cout << "8 occurs " << counter_8 << " times" << endl;
cout << "9 occurs " << counter_9 << " times" << endl;
system("pause");
return 0;
}
int count(int i,int j)
{if(i==j)
return 1;
return 0;
}
//random number generators
void randomize(void)
{
srand(unsigned(time(NULL)));
}
void randomize(int seed)
{
srand(unsigned(seed));
}
int random(void)
{
return rand();
}
int random(int upper_bound)
{
return rand() % (upper_bound + 1);
}
int random(int upper_bound, int lower_bound)
{
if(upper_bound < lower_bound)
{
int t = upper_bound;
upper_bound = lower_bound;
lower_bound = t;
}
int range = upper_bound - lower_bound + 1;
int number = rand() % range + lower_bound;
return number;
}