Hi all,
I recently got an extra credit assignment for my intro to c++ class and I'm having 2 slight problems. One is if I make the variable "cannleft" seen in the programming to an integer instead of a double the program loops. I tried static_cast<int>(cannleft); with no avail. Also my calculations based on day and night movement of tarzan seems to double print every bunch of numbers calculated even though its counting properly over all. Any help would be greatly appreciated. I am going to paste the assignment first and then the code. All the best
Assignment:
-----------
Extra Credit #1
Cannibals, Missionaries & Tarzan
Background
Thirty cannibals begin to eat ten missionaries. It takes an hour to consume the first missionary. After each HOUR, one cannibal steals away into the forest. Each cannibal eats at a constant rate. Therefore, since there are fewer consumers (cannibals), fewer missionaries will be consumed in each hour.
Fortunately for the missionaries, help is on the way. Tarzan is coming! However, he is 80 miles away. During the day Tarzan travels at seven miles per hour. During the darkness he reduces his speed to 4 miles per hour. Darkness lasts nine hours. When the cannibals start eating, there are only three hours of light left.
Your Task
Write a program to calculate and display how many cannibals will be caught and how many missionaries saved (if any!!). Display your results in the following form:
END OF HOUR MISSIONARIES LEFT CANNIBALS EATING TARZAN DISTANCE CURR MISS
BEING EATEN
----------- ---------------------------- ------------------------------------- -------------------- --------------------------
0 10 30 80 10
1 9 29 73 9
2 8 28 66 8
. . . . .
. . . . .
|
Consider that a missionary partially consumed is not a missionary saved.
Do the following:
1. Hand in the program.
2. Place the steps for the software development procedure in the top comment of your program.
3. Fully document (comments!!) your code.
4. Rerun your program with Tarzan only 30 miles away.
Extra Extra Credit Challenge
Change your program so that the program will read from the user the number of missionaries, the number of cannibals, and the distance Tarzan needs to travel. In other words your program should be general enough to handle any values for missionaries, cannibals, and the distance Tarzan needs to travel.
Code:
-----
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
//Extra Credit 2
//****************TO BE DETERMINED*************************
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//declare variables
int hours,missionaries,tarzan,eaten,day=0,cannibals;
double ratio=0,cannleft;
//ask for inputs
//asking for # of missionaries
cout<<" Hello! \n"<<endl
<<" Please enter the number of missionaries you are"<<endl
<<" calculating for (no spaces): ";
cin>>missionaries;
//asking for # of cannibals
cout<<"\n What are the current # of cannibals? ";
cin>>cannibals;
//asking for distance tarzan is away
cout<<"\n How Far away is Tarzan? ";
cin>>tarzan;
//output title
cout<<"\n CANNIBALS VS. MISSIONARIES: \n"<<endl;
//output columns
cout<<setw(15)<<"END OF HOUR"<<setw(12)<<"MISS LEFT"<<setw(13)<<"CANN EATING"<<
setw(17)<<"TARZAN DISTANCE"<<setw(21)<<"CURRENT BEING EATEN"<<endl;
cout<<setw(15)<<"-----------"<<setw(12)<<"---------"<<setw(13)<<"-----------"<<
setw(17)<<"---------------"<<setw(21)<<"-------------------"<<endl;
hours = 0; //start hours to count by
cannleft=cannibals; //setting cannibals left (starting with)
eaten=missionaries; //setting missionaries to start with being eaten
//while loop w/ calculations and outputing the results
for(eaten=missionaries; eaten>0; hours++)
{
//output status of all variables
cout<<setw(15)<<hours<<setw(12)<<eaten<<setw(13)<<cannleft<<setw(17)<<tarzan<<setw(21)<<eaten<<endl;
ratio=cannleft/cannibals; //calculating consumption ratio
cannleft=cannleft-ratio; //calculating cannibals left
eaten=eaten-ratio; //calculating missionaries left after being eaten
day++; //d+1
//if-else statements to determine speed of tarzan based on day or night
if (day<=3)
tarzan=tarzan-7;
else if (day<=9)
tarzan=tarzan-4;
else
day=1;
if (tarzan<=0)
tarzan=0;
}
//outputing maker of the program
cout<<"\n This program is written by Jarred Sutton.\n"<<endl;
return 0;
}
|
/*output
Hello!
Please enter the number of missionaries you are
calculating for (no spaces): 20
What are the current # of cannibals? 50
How Far away is Tarzan? 80
CANNIBALS VS. MISSIONARIES:
END OF HOUR MISS LEFT CANN EATING TARZAN DISTANCE CURRENT BEING EATEN
----------- --------- ----------- --------------- -------------------
0 20 50 80 20
1 19 49 73 19
2 18 48.02 66 18
3 17 47.0596 59 17
4 16 46.1184 55 16
5 15 45.196 51 15
6 14 44.2921 47 14
7 13 43.4063 43 13
8 12 42.5382 39 12
9 11 41.6874 35 11
10 10 40.8536 35 10
11 9 40.0366 28 9
12 8 39.2358 21 8
13 7 38.4511 17 7
14 6 37.6821 13 6
15 5 36.9285 9 5
16 4 36.1899 5 4
17 3 35.4661 1 3
18 2 34.7568 0 2
19 1 34.0616 0 1
This program is written by Jarred Sutton.
Press any key to continue . . .
Press any key to continue . . .
*/ |