Create a new file in C++ and save it as lab12_XYZ.cpp
- Consider rolling two six-sided dice. While the result of each throw is independent of all rolls that have come before, we can still calculate the odds of rolling any given number between 2 and 12. This
program will calculate those odds by generating a large number (100,000) of random values (i.e. "rolling the dice") and storing the results in an array. When all values have been rolled, we will display the results (counts and odds to 2 decimal places) to the user.
In Lab 5, sample code was provided for generating a random number. In this lab, I've provided a header file for you to include, getRandomLib.h, which lets you get all kinds of random things.
We need some functions:- void displayMessage(); //hello, goodbye, etc.
- int rollDice(); //returns a number between 2 and 12 (by rolling two dice and summing the result)
- void displayResults(int[]); //displays results in the array
HINT: Make sure to initialize counters before starting loop
HINT: To calculate odds: odds = count * 100 / NUM_ROLLS;
- Header comments must be present
- Prototypes must be present if functions are used
- Hello and goodbye messages must be shown
- Array(s) must be used for implementation
- Results should be formatted neatly (2 decimal places for odds, decimals lined up, etc.)
- Use comments and good style practices
Make sure to #include "getRandomLib.h". Download the attached header file and place in same location as your program.
SAMPLE OUTPUT: Welcome! This program will calculate the odds of rolling 2 - 12 with two 6-sided dice.Number of 2s rolled: 270 ( 2.70%) Number of 3s rolled: 556 ( 5.56%) Number of 4s rolled: 814 ( 8.14%) Number of 5s rolled: 1136 (11.36%) Number of 6s rolled: 1379 (13.79%) Number of 7s rolled: 1705 (17.05%) Number of 8s rolled: 1397 (13.97%) Number of 9s rolled: 1140 (11.40%) Number of 10s rolled: 818 ( 8.18%) Number of 11s rolled: 532 ( 5.32%) Number of 12s rolled: 253 ( 2.53%)
Exiting program. Goodbye!
- getRandomLib.h (3.468 KB):
#ifndef GET_RANDOM_LIB_H
#define GET_RANDOM_LIB_H
#include <cstdlib>
#include <random>
#include <ctime>
using namespace std;
const int MIN_UPPER = 65;
const int MAX_UPPER = 90;
const int MIN_LOWER = 97;
const int MAX_LOWER = 122;
const int MIN_DIGIT = 48;
const int MAX_DIGIT = 57;
const int MIN_CHAR = 33;
const int MAX_CHAR = 126;
//unsigned seed = time(0);
int getRandomInt(int min, int max) {
//seed random engine
static default_random_engine gen((unsigned int)time(0));
// if max < min, then swap
if (min > max) {
int temp = min;
min = max;
max = temp;
}
//set up random generator
uniform_int_distribution<int> dis(min, max); //range
//need to throw away the first call, as it is always max...
static int firstCall = dis(gen);
return dis(gen);
} //end getRandomInt()
float getRandomFraction() {
//seed random engine
static unsigned seed = time(0);
return rand() / static_cast<float>(RAND_MAX);
} //end getRandomFraction()
float getRandomFloat(float min, float max) {
if (min > max) {
float temp = min;
min = max;
max = temp;
}
return (getRandomFraction() * (max - min)) + min;
} //end getRandomFloat()
double getRandomDouble(double min, double max) {
if (min > max) {
double temp = min;
min = max;
max = temp;
}
return (static_cast<double>(getRandomFraction()) * (max - min)) + min;
} //end getRandomDouble()
bool getRandomBool() {
if (getRandomInt(0,1) == 0)
return false;
else
return true;
} //end getRandomBool
char getRandomUpper() {
return static_cast<char>(getRandomInt(MIN_UPPER, MAX_UPPER));
} //end getRandomUpper()
char getRandomUpper(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomUpper()
char getRandomLower() {
return static_cast<char>(getRandomInt(MIN_LOWER, MAX_LOWER));
} //end getRandomLower()
char getRandomLower(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomLower()
char getRandomAlpha() {
if (getRandomBool())
return getRandomUpper();
else
return getRandomLower();
} //end getRandomAlpha()
char getRandomDigit() {
return static_cast<char>(getRandomInt(MIN_DIGIT, MAX_DIGIT));
} //end getRandomDigit()
char getRandomDigit(char min, char max) {
return static_cast<char>(getRandomInt(static_cast<int>(min), static_cast<int>(max)));
} //end getRandomDigit()
char getRandomChar() {
return static_cast<char>(getRandomInt(MIN_CHAR, MAX_CHAR));
} //end getRandomChar()
#endif
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 2 images
- JAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. double_input1.txt double_input2.txt WHEN I UPLOAD THE PROGRAM. TO HYPERGRADE IT DOES NOT PASS THE TEST CASES. PLEASE MODIFY THIS CODE, SO WHEN I…arrow_forwardan int variable k, an int array currentMembers that has been declared and initialized, an int variable nMembers that contains the number of elements in the array, an int variable memberID that has been initialized, and a bool variable isAMember, Write code that assigns true to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMemberotherwise. Use only k, currentMembers, nMembers, and isAMember.arrow_forwardJAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. PLEASE MOFDIFY THIS PROGRAM SO THERE ARE ONLY THREE DIGITS AFTER THE DECIMAL POINT FOR LOWEST, HIGHTEST, AVERAGE AND TOTAL. BECAUSE WHEN I UPLOAD IT TO…arrow_forward
- In Java please,arrow_forwardThis is needed in JAVAarrow_forwardJAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. This part of the program is not correct. There should be no Scanner. You should read the file name from the command line.Scanner scanner = new…arrow_forward
- Create in Java (1) Create an array of String objects and populate it with the days of the week (7 days)(i.e., Monday - Sunday)(2) Create an array parallel to the one created in (1) to store the entrées. However, you do not have this information, soyou will need to write a loop that prompts the user to enter the entrée served on that day. This prompt shouldinclude the day for which you are asking for the information (e.g., "What entrée is being served on Monday?")(3) Create another parallel array that stores the prices. You will also need to collect this information from the user. Thistime, your prompt should include the name of the entrée for which you are asking for the price(4) Prompt the user to choose an entrée from a list that you display numbered 1 through 7 (have them input a number1 to 7) and search the array and output the day on which the entrée will be served(5) Search through the array for the highest price entrée and output the name of this entrée for the user including…arrow_forwardA file USPopulation.txt contains the population of the US starting in year 1950 and then each subsequent record has the population for the following year. USPopulation.txtDownload USPopulation.txt Write a program that uses an array with the file that displays these in a menu and then produces the results. This is not an Object Oriented Program but should be a procedural program, calling methods to do the following: 1: Displays the year and the population during that year 2. The average population during that time period (Add up the populations of all records and divide by the number of years). 3. The year with the greatest increase in population - print the year and that population and that amount. To figure this out, compare the difference in population before of say year 1950 and 1951, store that difference somewhere. Compare 1951 with 1952, find that difference. Is that difference greater than the stored difference? If so, move that the the maximum place. 4.…arrow_forwardStep 1: Read your files! You should now have 3 files. Read each of these files, in the order listed below. staticarray.h -- contains the class definition for StaticArray, which makes arrays behave a little more like Python lists O In particular, pay attention to the private variables. Note that the array has been defined with a MAX capacity but that is not the same as having MAX elements. Which variable indicates the actual number of elements in the array? staticarray.cpp -- contains function definitions for StaticArray. Right now, that's just the print() member function. 0 Pay attention to the print() function. How many elements is it printing? main.cpp -- client code to test your staticarray class. Note that the multi-line comment format (/* ... */) has been used to comment out everything in main below the first print statement. As you proceed through the lab, you will need to move the starting comment (/*) to a different location. Other than that, no changes ever need to be made to…arrow_forward
- Alert dont submit AI generated answer.arrow_forwardJAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. This part of the program is not correct. There should be no Scanner. You should read the file name from the command line.Scanner scanner = new…arrow_forwardJAVA PROGRAM Lab #2. Chapter 7. PC #11. Array Operations (Page 491) Write a program that accepts a file name from command line, then initializes an array with test data using that text file as an input. The file should contain floating point numbers (use double data type). The program should also have the following methods: * getTotal. This method should accept a one-dimensional array as its argument and return the total of the values in the array. * getAverage. This method should accept a one-dimensional array as its argument and return the average of the values in the array. * getHighest. This method should accept a one-dimensional array as its argument and return the highest value in the array. * getLowest. This method should accept a one-dimensional array as its argument and return the lowest value in the array. PLEASE FIX AND MODIFY THIS JAVA PROGRAM. THIS PROGRAM DOES NOT WORK IN HYPERGERADE. IT ONLY PASSSES 2 OUT OF 4 TEST CASES. I NEED IT TO PASS 4 OUT OF 4…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY