C++ Coding HW Design your main program so that it has a while loop that will run
ID: 3591038 • Letter: C
Question
C++ Coding HW
Design your main program so that it has a while loop that will run until the user chooses to exit.
Inside this loop, you will also need to prompt the user to ask the user which function he/she would like to run. Use a switch statement to call the functions. The main method will take the form:
Prompt with a menu and read a code
while code != -1
test code using a switch statement
if code = 1
2.1.1.1 read in a number n
call Prime Number (n)
if code = 2
2.1.2.1 read in a number times
call Weighted Coins (times)
if code = 3
2.1.3.1 read in a number x
2.1.3.2 call EvenOdd (x)
default = print a message “Bad Code”
Prompt with a menu and read a code
3. Stop
Prime Numbers:
For the prime number function, have the method print out all primes up to a value n, which will be input from the keyboard by the user. You should ask for n inside the switch of the main function and then pass it into the prime number method/function. Ex: if n = 12 program would print 2, 3, 5, 7, 11. If n = 19, program would print 2, 3, 5, 7, 11, 13, 17, 19.
Weighted Coins:
For this function, you will be simulating a coin toss. However, you will not be doing just any coin toss. The coin you will be ‘simulating’ will be weighted such that it will land on heads 60 percent of the time and the coin will land on tails only 40 percent of the time. In main in the switch statement, you need to have the user input the number of coin tosses he/she wishes to perform. Make sure the method prints out:
1) The total number of times the coin landed on heads. 2) The total number of times the coin landed on tails. 3) The percentage of the time the coin landed on heads.
Even or Odd:
In this function, you have one parameter, an integer which you will input inside the switch statement. You need to be able to determine if the number is even or odd. Print out the appropriate message.
Hint: What operator (+ - * / %) would be helpful here?
Run:
Select the prime generator function - put in a number between 10-50 as your n. Once it finishes, select the coin toss function. Do the coin toss three times: 100, 500, and 1000. Then run EvenOdd function two times – once for a number that is even and once for a number that is odd. Show one “error message” for the switch using the default option (picking an invalid option from the menu – anything but 1-3 or -1). Make sure your code has “Coded by _________ “ and prints that message. (meaning you exited the program fully)
Explanation / Answer
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
void printprompt() {
cout << "Enter 1 for generating prime: " << endl;
cout << "Enter 2 for weighted coin toss: " << endl;
cout << "Enter 3 for chck even/odd: " << endl;
cout << "Enter -1 to exit:" << endl;
}
void printprime(int n) {
// create a boolean array for keeping whether that index is prime or not
bool primes[n+1];
// setting all number as prime initially
memset(primes, true, sizeof(primes));
// now loop over and find out numbers not prime and mark them as false
for (int i = 2; i*i <= n; i++)
{
if (primes[i] == true)
{
for (int j = i*2; j<= n; j += i)
{
primes[j] = false;
}
}
}
// now all index with true are orime, lets print them
for (int i = 2; i <= n; i++)
{
if (primes[i])
{
cout << i << " ";
}
}
cout << endl;
}
void weightToss(int n) {
int head = 0;
int tail = 0;
for(int i; i < n; i++) {
double r = ((double) rand() / (RAND_MAX));
if (r <= 0.6) {
head++;
}
else {
tail++;
}
}
cout << head << "number of coin landed on heads" << endl;
cout << tail << "number of coin landed on tails" << endl;
cout << ((head*100.0)/n) << " % coin landed on heads" << endl;
}
void EvenOdd (int n) {
switch(n%2) {
case 1:
cout << "Odd" << endl;
break;
default:
cout << "Even" << endl;
break;
}
}
int main()
{
int code;
printprompt();
cin >> code;
while(code != -1) {
int n;
switch(code) {
case 1:
cout << "Enter a number to print primes till that number: ";
cin >> n;
printprime(n);
break;
case 2:
cout << "Enter a number to do that many coin toss: ";
cin >> n;
weightToss(n);
break;
case 3:
cout << "Enter a number to test whether it is even or odd: ";
cin >> n;
EvenOdd (n);
break;
default:
cout << "bad code" << endl;
}
printprompt();
cin >> code;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.