== Programming Assignment == For this assignment you will write a program that r
ID: 3854118 • Letter: #
Question
== Programming Assignment ==
For this assignment you will write a program that reads in climate data
(imagine it is coming from a weather station) and stores that data in a linked
list. After reading and storing all of the data your program will generate a
report from the data.
The objective of this assignment is to learn how to implement a linked list
and to gain familiarity with using a linked list in solving a problem. Since
one of the objectives is to learn how to implement a linked list you cannot
use the STL linked list.
Your program should use good design methodologies so you should have separate
classes for each of the following:
- datalogger -- This class represents the "business logic" of adding data to
the storage. This class also handles filtering out duplicate data items (see
the requirements listed below). This is the only class that weatherlog.cpp
knows about and it expects two methods: addData() and printReport().
- linkedlist -- This class should implement a linked list. This class should
be distinct from the datalogger class (although I would expect datalogger to
have a data member that is a linkedlist object).
- weatherdata -- This class encapsulates the weather data (time, temperature, windspeed).
You are welcome to create any additional classes that you need.
Below are the specific external and internal requirements for this
assignment. Your program will be evaluated based on these requirements.
== External Requirements ==
- The main driver (weatherlog.cpp) will provide a sequence of data
readings. The program needs to store these readings. After all of the data
is read in, the program must create a report identical to the one in the
expected.txt output file.
- Data with a duplicate timestamp indicates contradictory or inaccurate data
and should not be stored (all records with the same timestamp).
- The report includes:
- a line that states the timestamp range
- a line that states the number of data entries
- a temperature section that contains a detailed report (see below)
- a windspeed section that contains a detailed report (see below)
- detailed report data:
- the minumum value measured
- the maximum value measured
- a list of all of the readings >= the 99% reading (in order)
- a list of all of the readings <= the 1% reading (in order)
- see the provided "expected.txt" to see the report format
== Internal Requirements ==
- The program must use the supplied weatherlog.cpp file, unmodified, as the
main driver.
- All of the weather data must be stored in a linked list.
- The linked list must have three separate "chains". One that keeps the data
ordered by timestamp, one that keeps the data ordered by temperature, and
one that keeps the data ordered by windspeed.
- Strings must be stored using char* variables, not std::string.
- No memory leaks.
weatherlog.cpp file
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
Some example climate data values in climate.txt:
1480911409 -132 565
1480911412 -132 603
1480911416 -134 533
1480911418 -137 556
1480911418 -138 515
1480911425 -139 614
1480911430 -143 708
1480911435 -143 659
1480911443 -140 705
1480911443 -139 773
1480911452 -138 676
1480911452 -134 655
1480911453 -134 673
1480911454 -130 584
1480911459 -130 616
1480911463 -126 615
1480911468 -126 610
1480911469 -126 677
1480911472 -122 705
1480911473 -121 644
1480911482 -121 691
1480911487 -118 747
1480911493 -114 650
1480911497 -112 672
1480911503 -108 654
1480911503 -107 576
1480911504 -104 585
1480911512 -104 614
1480911518 -104 554
1480911522 -103 485
1480911527 -100 399
1480911529 -97 476
1480911529 -93 539
1480911532 -93 489
1480911537 -90 504
1480911537 -86 420
1480911541 -84 376
1480911544 -83 402
1480911546 -81 435
1480911551 -78 455
1480911557 -75 465
1480911557 -73 520
Code should be in C++
Explanation / Answer
Here is the completed code for the question. You have not given the expected.txt contents for report format. I am not clear about printing the >= 99% and <= 1%,. If provided the expected format and details , I can help you. Marked those sections in code with //TODO ... comment. Please post a comment with the report details / update the question , so I can help you.
linkedlist.h
#ifndef linkedlist_h
#define linkedlist_h
#include <iostream>
#include <fstream>
using namespace std;
// A node in the linked list
typedef struct llnode
{
int timestamp;
double temperature;
double windspeed;
struct llnode* next;
llnode(int time, double temp, double wind)
{
timestamp = time;
temperature = temp;
windspeed = wind;
next = NULL;
}
}node;
//a linked list holding 3 chains for storing data sorted on timestamp, windspeed, and temperature
class linkedlist
{
private:
node* time_chain_head; //list of nodes sorted on timestamp
node* temp_chain_head;
node* wind_chain_head;
//adds to the list by sorting based on the field number specified, if sortfield is 1, the list is sorted on timestamp,
//if 2, sorted on temperature and 3 means sort on windspeed
void add_to_list(node* &head, node *n, int sortfield)
{
if(head == NULL)
{
head = n;
return;
}
node *curr = head , *prev = NULL;
while(curr != NULL)
{
if((sortfield == 1 && curr->timestamp > n->timestamp) ||
(sortfield == 2 && curr->temperature > n->temperature) ||
(sortfield == 3 && curr->windspeed > n->windspeed))
break;
prev = curr;
curr = curr->next;
}
if(prev == NULL)
head = n;
else
{
n->next = curr;
prev->next = n;
}
}
//starting from the given node n, finds the last duplicate node in the sorted list and returns it. If no duplicates were found, the same
//node is returned
node* get_last_duplicate(node* n)
{
node *dup = n;
if(n == NULL) return NULL;
while(dup->next != NULL && dup->next->timestamp == n->timestamp)
dup = dup->next;
return dup;
}
//function to remove duplicate chain of nodes sorted by timestamps. This also cleans the other 2 chains (windspeed chain and
// temperature chain), whenever a timestamp is removed from the timestamp chain.
void remove_duplicate_timestamps()
{
node *curr = time_chain_head, *prev = NULL;
if(time_chain_head == NULL || time_chain_head->next == NULL ) //when list is empty or has 1 node, nothing to do
return;
while(curr != NULL)
{
node *lastdup = get_last_duplicate(curr);
node *saved = lastdup->next;
if(lastdup == curr) //this is the only node with the current timestamp
{
prev = curr;
curr = curr->next;
}
else
{
remove_nodes(wind_chain_head, curr->timestamp); //remove the same timestamp nodes from chain sorted on windspeed
remove_nodes(temp_chain_head, curr->timestamp); //
remove_duplicates(time_chain_head, prev, curr, lastdup);
curr = saved;
continue;
}
}
}
//remove nodes starting from firstdup till lastdup, and updating head if head node was removed.
void remove_duplicates(node *&head, node *prev, node* firstdup, node *lastdup)
{
std::cout << "removing duplicate timestamp " << firstdup->timestamp << std::endl;
if(prev == NULL)
head = lastdup->next;
else
prev->next = lastdup->next;
while(firstdup != lastdup)
{
node *p = firstdup->next;
delete firstdup;
firstdup = p;
}
delete lastdup;
}
//used to clean duplicats from the temperature sorted chain and windspeed sorted chain
void remove_nodes(node* &head, int timestamp)
{
node *curr = NULL, *prev = NULL;
while(curr != NULL)
{
if(curr->timestamp == timestamp) //found a matching node
{
node *save = curr->next;
if(prev == NULL)
head = curr->next;
else
prev->next = curr->next;
delete curr;
curr = save;
}
else
{
prev = curr;
curr = curr->next;
}
}
}
//used by destructor to clean up
void delete_list(node *head)
{
//deallocate all nodes
node *p = head, *n;
while(p != NULL)
{
n = p->next;
delete p;
p = n;
}
}
void get_temperature_range(double &min, double &max)
{
node *p = temp_chain_head;
if(p != NULL)
{
min = p->temperature; //temperature in 1st node
while(p->next != NULL)
{
p = p->next;
}
max = p->temperature; //temperature in last node of sorted temperature chain
}
}
void get_windspeed_range(double &min, double &max)
{
node *p = wind_chain_head;
if(p != NULL)
{
min = p->windspeed; //windspeed in 1st node
while(p->next != NULL)
{
p = p->next;
}
max = p->windspeed; //windspeed in last node of sorted windspeed chain
}
}
public:
//constructor
linkedlist()
{
time_chain_head = NULL;
temp_chain_head = NULL;
wind_chain_head = NULL;
}
//destructor
~linkedlist()
{
delete_list(time_chain_head);
delete_list(temp_chain_head);
delete_list(wind_chain_head);
}
void add(int timestamp, double temperature, double windspeed)
{
add_to_list(time_chain_head, new node(timestamp, temperature, windspeed), 1);
add_to_list(temp_chain_head, new node(timestamp, temperature, windspeed), 2);
add_to_list(wind_chain_head, new node(timestamp, temperature, windspeed), 3);
}
void cleanup_list()
{
remove_duplicate_timestamps();
}
void get_timestamp_range(int &first, int &last)
{
first = 0;
last = 0;
if(time_chain_head != NULL)
{
first = time_chain_head->timestamp;
node *p = time_chain_head;
while(p->next != NULL)
p = p->next;
last = p->timestamp;
}
}
int get_num_entries()
{
int count = 0;
node *p = time_chain_head;
while(p != NULL)
{
p = p->next;
count++;
}
return count;
}
void print_timestamp_section(ostream &out)
{
int min, max;
get_timestamp_range(min, max);
out << "Timestamp Range :" << min << " - " << max << endl ;
out << "Number of entries: " << get_num_entries() << endl << endl;
}
void print_temperature_section(ostream &out)
{
double min, max;
get_temperature_range(min, max);
out << "Temperature details:" <<endl;
out << "Minimum temperature: " << min << endl;
out << "Maximum temperature: " << max << endl << endl;
//TODO list all >= 99% and <= 1%
}
void print_windspeed_section(ostream &out)
{
double min, max;
get_windspeed_range(min, max);
out << "Windspeed details:" <<endl;
out << "Minimum windspeed: " << min << endl;
out << "Maximum windspeed: " << max << endl << endl;
//TODO list all >= 99% and <= 1%
}
void print()
{
node *p = time_chain_head;
while(p != NULL)
{
cout << p->timestamp << " " << p->temperature << " " << p->windspeed << endl;
p = p->next;
}
}
};
#endif /* linkedlist_h */
datalogger.h
#ifndef datalogger_h
#define datalogger_h
#include "linkedlist.h"
class datalogger
{
private:
linkedlist weatherdata;
public:
void addData(int time, double temp, double wind)
{
weatherdata.add(time, temp, wind);
}
void printReport()
{
weatherdata.cleanup_list();
cout << endl << endl;
printReport(cout);
}
//prints the report to specified output stream, this can be used to print to a file
void printReport(ostream &os)
{
weatherdata.print_timestamp_section(os);
weatherdata.print_temperature_section(os);
weatherdata.print_windspeed_section(os);
}
};
#endif /* datalogger_h */
weatherlog.cpp
#include <iostream>
#include <fstream>
#include "datalogger.h"
using namespace std;
int main(int argc, char** argv) {
datalogger dl;
if (argc != 2) {
cout << "Usage: " << argv[0] << " <datafile>" << endl;
exit(0);
}
// Read the data
char* datafile = argv[1];
ifstream infile(datafile);
int timestamp;
double temperature;
double windspeed;
while (!infile.eof()) {
infile >> timestamp;
infile >> temperature;
infile >> windspeed;
if (!infile.eof()) {
dl.addData(timestamp, temperature, windspeed);
}
}
// Output the report
dl.printReport();
return(0);
}
input file weatherdata.txt
1480911409 -132 565
1480911412 -132 603
1480911416 -134 533
1480911418 -137 556
1480911418 -138 515
1480911425 -139 614
1480911430 -143 708
1480911435 -143 659
1480911443 -140 705
1480911443 -139 773
1480911452 -138 676
1480911452 -134 655
1480911453 -134 673
1480911454 -130 584
1480911459 -130 616
1480911463 -126 615
1480911468 -126 610
1480911469 -126 677
1480911472 -122 705
1480911473 -121 644
1480911482 -121 691
1480911487 -118 747
1480911493 -114 650
1480911497 -112 672
1480911503 -108 654
1480911503 -107 576
1480911504 -104 585
1480911512 -104 614
1480911518 -104 554
1480911522 -103 485
1480911527 -100 399
1480911529 -97 476
1480911529 -93 539
1480911532 -93 489
1480911537 -90 504
1480911537 -86 420
1480911541 -84 376
1480911544 -83 402
1480911546 -81 435
1480911551 -78 455
1480911557 -75 465
1480911557 -73 520
output
$ g++ weatherlog.cpp
$ ./a.out weatherdata.txt
removing duplicate timestamp 1480911418
removing duplicate timestamp 1480911443
removing duplicate timestamp 1480911452
removing duplicate timestamp 1480911503
removing duplicate timestamp 1480911529
removing duplicate timestamp 1480911537
removing duplicate timestamp 1480911557
Timestamp Range :1480911409 - 1480911551
Number of entries: 28
Temperature details:
Minimum temperature: -143
Maximum temperature: -73
Windspeed details:
Minimum windspeed: 376
Maximum windspeed: 520
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.