You are taking a geology class, and the professor wants you to write a program t
ID: 3633468 • Letter: Y
Question
You are taking a geology class, and the professor wants you to write a program to help students learn the periods of geologic time. The program should let the user enter a range of prehistoric dates (in millions of years), and then output the periods that are included in that range. Each time this output is done, the user is asked if he or she wants to continue. The goal of he exercise is for students to try to figure out when each period began, so that he or she can make a chart of geologic time.Within the program, represent the periods with an enumeration type made up of their names. You will probably want to create a function that determines the period corresponding to a date, and another function that returns the string corresponding to each identifier in the enumeration. Then you can use a For loop to output the series of periods in the range. The periods of geologic time are given here:
Period name Starting date (millions of years)
Neogene 23
Paleogene 65
Cretaceous 136
Jurassic 192
Triassic 225
Permian 280
Carboniferous 345
Devonian 395
Silurian 435
Ordovician 500
Cambrian 570
Precambrian 4500 or earlier
Use functional decomposition to solve this problem. Be sure to use good coding style and documenting comments.
Explanation / Answer
I think this code will help you. Please rate.
#include
#include
#include
using namespace std;
bool isInTimePeriod(int year, int guess);
// crap load of constants to make our life easier and our code cleaner
const int NEOGENE = 23;
const int PALEOGENE = 65;
const int CRETACEOUS = 136;
const int JURASSIC = 192;
const int TRIASSIC = 225;
const int PERMIAN = 280;
const int CARBONIFEROUS = 345;
const int DEVONIAN = 395;
const int SILURIAN = 435;
const int ORDOVICIAN = 500;
const int CAMBRIAN = 570;
const int PRECAMBRIAN = 4500;
const int timePeriods[] = { NEOGENE, PALEOGENE, CRETACEOUS, JURASSIC, TRIASSIC, PERMIAN, CARBONIFEROUS, DEVONIAN, SILURIAN, ORDOVICIAN, CAMBRIAN, PRECAMBRIAN };
const int numPeriods = 12;
void main ()
{
int guess = -1;
bool quit;
time_t seconds; //next few lines finds the randomized number
time(&seconds);
srand((unsigned int)seconds);
int max=PRECAMBRIAN,min=NEOGENE;
int num; // Don't generate the number just yet
quit = false;
do
{
while (!quit) //This section below is basically the user table for info
{
cout << "Period Name Starting Date (Millions of Years) ";
cout << "1. Neogene 23 ";
cout << "2. Paleogene 65 ";
cout << "3. Cretaceous 136 ";
cout << "4. Jurassic 192 ";
cout << "5. Triassic 225 ";
cout << "6. Permian 280 ";
cout << "7. Carboniferous 345 ";
cout << "8. Devonian 395 ";
cout << "9. Silurian 435 ";
cout << "10. Ordovician 500 ";
cout << "11. Cambrian 570 ";
cout << "12. Precambrian 4500 or earlier ";
// Get our random number.
num = rand()%(max-min)+min;
do
{
if(guess != -1)
cout << "INCORRECT! GUESS AGAIN! ";
cout << "The time period is (in millions of years): " << num << endl;
cout << "Please enter the correct Geological time period by number: " << endl;
cin >> guess;
if(guess <= 0 || guess > numPeriods)
{
cout << "Please enter a valid number! ";
guess = 0; //set it back to zero.
continue; // don't call the function, the number is invalid
}
}while(!isInTimePeriod(num, guess));
cout << "CORRECT ";
guess = -1; // reset it back so that it doesn't tell us that we were wrong :p
}
}
while (1); //I just use this command for my purpose of viewing it
}
bool isInTimePeriod(int year, int guess)
{
// If they guessed the final time period, we can't look to see if it is between anything.
if(guess == numPeriods)
return year >= timePeriods[guess-1]; // So they are right if the year is larger than the final time period.
// Otherwise, return true if the guess was inbetween the time periods.
return year >= timePeriods[guess-1] && year < timePeriods[guess];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.