Using C++ Problem B: Predicting Life Expectancy (20 points) The Social Security
ID: 3595753 • Letter: U
Question
Using C++
Problem B: Predicting Life Expectancy (20 points) The Social Security Administration maintains an actuarial life table that contains the probability that a citizen of the United States will die Chttp://www.ssa govOACT/STATS /table4c6 html) tp/CTSTAebleember the espesso Remember: the expression above (on the previous page) returns a double (real number) with a value ranging from 0.0 to 1.0. You should create a fiunction named myRand to compute the random number and return it, and call the fiunction myRand whenever you need to compute a random number. Note, you should prompt the user for a seed value for the rand function, and call the seed function srand once and only once at the beginning of your program. t ahonld rte a finction named mtthe The death probabilities from this table for 2014 are stored in the file LeTableMF2014.txt available on the class Moodle site in the Homework 5 folder. There are three values for each row: the age, the death probability for a male of that age, and the death probability for a female of th at age. For example, the first five lines are 0.006322 0.005313 0.000396 0.000346 0.000282 0.000221 0.000212 0.000162 0.000186 0.000131 0 You should also include the logic to ensure the file LeTableMF2014.txt opens without errors, and logic to ensure the use inputs legal values for age and gender. An example nun of the program is as follows Please enter a seed for the random number generator: 35 For example, the fourth line in the listing above says that a 3 year old female has a 0.000162 chance of dying. Enter your age (must be >- 0 and 120) 45 Enter your gender (M or F M We predict you will live to age: 57 Write a program that reads the data from the file LeTableME2014.txt into a multidimensional array Would you like to continue: (y or n): y Next your program should prompt the user to enter their sex (M or F- in upper or lower case) and age (an integer value). Your program should check to make sure legal values for age and gender are entered, and if not, print an error Enter your age (must be >= 0 andExplanation / Answer
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
fin.open("LeTableMF2014.txt");
if(!fin.is_open())
{
cout << "Unable to open the file." << endl;
return 0;
}
double deathProbability[120][2];
int temp, seed, age;
for(int i = 0; i < 120; i++)
fin >> temp >> deathProbability[i][0] >> deathProbability[i][1];
cout << "Please enter a seed for the random number generator: ";
cin >> seed;
srand(seed);
char again = 'y', gender;
while(again != 'n' && again != 'N')
{
cout << "Enter your age (must be >= 0 and <= 120): ";
cin >> age;
cout << "Enter your gender (M or F): ";
cin >> gender;
int column = gender == 'M' ? 0 : 1;
if(age < 0 || age > 120)
cout << endl << "Illegal input: please enter the data requested at the prompts" << endl;
else
{
for(int i = age; i < 120; i++)
{
double randValue = rand() / double(RAND_MAX);
if(randValue <= deathProbability[i][column])
{
cout << "We predict you will live to age: " << i << endl;
break;
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.