Using C++ An automatic animal monitoring device sits in the african savannah mon
ID: 3834811 • Letter: U
Question
Using C++
An automatic animal monitoring device sits in the african savannah monitoring all the animals that pass within range. Throughout the day it creates a large text file that records all of its observations. At the end of the day that file is uploaded to a research base and analysed.
The file has one line for each animal observed. Each line has the same format: first the time (in 24 hour form), then the species of the animal (a single word, using only lower case letters), then the direction it was heading (N, S, E, or W), then an estimate of its weight (in pounds), and finally an estimate of its speed (in miles per hour). Here is a sample from a typical file...
0530 gorilla E 441.3 2.9
0531 gorilla E 338.0 2.8
0531 gorilla E 127.5 2.8
0540 zebra W 1107.0 11.0
0542 lion W 510.3 15.0
0551 penguin S 5.1 45.0
0551 gorilla W 338.0 2.1
It seems to show a family of gorillas strolling towards the rising sun, then shortly later a zebra chased by a lion going in the opposite direction, and then a small penguin flying south and one of the gorillas coming back. The name of the file is animals.txt. Write a function that processes this file and does the following things:
+ It must create a new file called gorillas.txt containing all the records of gorilla observations.
+ It must calculate the average weight of all the lions observed.
+ It must note the speed of the slowest gorilla seen heading east in the morning (time between 0000 and 1200).
So if the sample data given above were the whole file, your function would create a gorillas.txt file containing:
0530 gorilla E 441.3 2.9
0531 gorilla E 338.0 2.8
0531 gorilla E 127.5 2.8
0551 gorilla W 338.0 2.1
And it would print for the user to see: Average lion weight 510.3 pounds Slowest east-bound morning gorilla 2.8 mph
Explanation / Answer
#include<iostream.h>
#include<string>
using namespace std;
void main(){
ifstream in1;
ofstream out;
string name, time;
float weight, speed;
char direction;
float min_speed = 100;
float sum = 0;
int lion_count = 0;
in1.open("animals.txt");
out.open("gorillas.txt");
while (in1 >> time >> name >> direction >> weight >> speed){
if (name == "gorilla"){
out << time << " " << name << " " << direction << " " << weight << " " << speed << endl;
if (time > 0000 && time < 1200 && direction == 'E' && speed < min_speed)
min_speed = speed;
}
if ( name == "lion"){
lion_count++;
sum = sum + weight;
}
}
cout << "Average weight of lion " << sum/lion_count << " " << "pounds ";
cout << "Slowest east-bound morning gorilla " << min_speed << end;
in1.close();
out.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.