Write a C++ program that calculates the probabilities of four different scenario
ID: 3739996 • Letter: W
Question
Write a C++ program that calculates the probabilities of four different scenarios. You will get the input from a file named <somefilename>.txt that contains three different values on each line, separated by the ',' character. The values represent the number of colored balls in a box. The first value is the number of red balls, then the number of white balls, and lastly, the number of black balls. You should have at least four test cases in your text file.
Your program should read the command line to get the input filename - <somefilename>.txt - that follows the flag "-i". It should also check to see if there is a "-c" to output the results to the console, or a "-f" to output the results to a file of the name <somefilename>.out.txt .
You are determine the probabilities of the following four situations:
1. The probability of drawing two red balls in a row (without replacement)
2. The probability of drawing a red ball and then a black ball (without replacement)
3. The probability of drawing three red balls (without replacement)
4. The probability of drawing first a white ball, then a red ball, and then a black ball (assuming that all balls are replaced before the next draw).
Format your output data in readable format
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstring>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;
double probability1(int red, int white, int black);
double probability2(int red, int white, int black);
double probability3(int red, int white, int black);
double probability4(int red, int white, int black);
int main(int argc, char* argv[])
{
string srg;
ifstream filestream(argv[1]);
int i=strlen(argv[1]);
char output[i+5];
output[i+4] = '';
output[i+3] = 't';
output[i+2] = 'x';
output[i+1] = 't';
output[i] = '.';
output[i-1] = 't';
output[i-2] = 'u';
output[i-3] = 'o';
output[i-4] = '.';
int j = i-5;
while(j>=0){
output[j] = argv[1][j];
j--;
}
ofstream ofilestream(output);
int redBalls ;
int whiteBalls ;
int blackBalls ;
if(!strcmp(argv[3],"-f")){
if (filestream.is_open() && ofilestream.is_open()){
while(getline(filestream,srg)){
ofilestream << srg << endl;
vector <string> tokens;
stringstream check1(srg);
string temp;
while(getline(check1,temp,',')){
tokens.push_back(temp);
}
stringstream red(tokens[0]);
red >> redBalls;
stringstream white(tokens[1]);
white >> whiteBalls;
stringstream black(tokens[2]);
black >> blackBalls;
ofilestream << "The probability of drawing two red balls in a row is " << probability1(redBalls,whiteBalls,blackBalls) << endl;
ofilestream << "The probability of drawing a red ball and then a black ball is " << probability2(redBalls,whiteBalls,blackBalls) << endl;
ofilestream << "The probability of drawing three red balls " << probability3(redBalls,whiteBalls,blackBalls) << endl;
ofilestream << "The probability of drawing first a white ball, then a red ball, and then a black ball " << probability3(redBalls,whiteBalls,blackBalls) << endl;
}
filestream.close();
}
}else{
if (filestream.is_open()){
while(getline(filestream,srg)){
cout << srg << endl;
vector <string> tokens;
stringstream check1(srg);
string temp;
while(getline(check1,temp,',')){
tokens.push_back(temp);
}
stringstream red(tokens[0]);
red >> redBalls;
stringstream white(tokens[1]);
white >> whiteBalls;
stringstream black(tokens[2]);
black >> blackBalls;
cout << "The probability of drawing two red balls in a row is " << probability1(redBalls,whiteBalls,blackBalls) << endl;
cout << "The probability of drawing a red ball and then a black ball is " << probability2(redBalls,whiteBalls,blackBalls) << endl;
cout << "The probability of drawing three red balls " << probability3(redBalls,whiteBalls,blackBalls) << endl;
cout << "The probability of drawing first a white ball, then a red ball, and then a black ball " << probability3(redBalls,whiteBalls,blackBalls) << endl;
}
filestream.close();
}
}
return 0;
}
double probability1(int red, int white, int black){
int total = red+white+black;
int num = red*(red-1);
return (double)(num)/(total*(total-1));
}
double probability2(int red, int white, int black){
int total = red+white+black;
int num = red*(black);
return (double)(num)/(total*(total-1));
}
double probability3(int red, int white, int black){
int total = red+white+black;
int num = red*(red-1)*(red-2);
return (double)(num)/(total*(total-1)*(total-2));
}
double probability4(int red, int white, int black){
int total = red+white+black;
int num = red*white*black;
return (double)(num)/(total*(total)*(total));
}
Command Line arguments:
input.txt -i -f or input.txt -i -c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.