Design an abstract data type called Weather that keeps track

Design an abstract data type called \"Weather\" that keeps track of the temperature of the last 7 days and the average. Assume that the days are numbered 0 to 6 (you can declare an array such as day[7J). When an object of Weather is instantiated, the temperature value of the last 6 days should be automatically initialized to 50.75. Once the program starts running, the user should be able to input the temperature values one day at a time. After each input adjust your array index and values, and update the average. At any time, the user may want to see the temperature of last 7 days and the average. The user may also want to see the temperature of a specific day in the last 7 days. Use constructor and destructor. Be user-friendly. Guide the user by specifying what input is expected. You should ALSO support the following features: Use const keyword everywhere it is appropriate Provide multiple versions of constructors Support a copy constructor Support an overloaded assignment operator (=) Support a feature to find number of current Weather objects. You should use the static keyword where appropriate. Support a reset function that will reset the weather values of all the days to the default value of 50.75. Support a function to compute the median of the last 7 days. Support a function to compute the mode of each weather value in the last 7 days.

Solution

#include<iostream>
using namespace std;
//Declaration of Abstract data type
class Weather
{
double days[7], average;
int c;
public:
//Parameterized constructor and use of constant
Weather(const double);
//Default constructor
Weather();
//Copy constructor
Weather(Weather &);
//Destructor
~Weather();
void accept();
void avg();
void displayAll();
void displaySpecial(int);
//Use of constant
void reset (Weather, const double = 50.75);
void median();
void mode();
void sortData();
};

//Find out the median
void Weather::median()
{
//Sorts data
sortData();
cout<<\"\ Sorted Array \";
for(c = 0; c < 7; c++)
cout<<days[c]<<\" \";
cout<<\"\ Median = \"<<days[3];
}

//Find out the mode
void Weather::mode()
{
int number = days[0];
int mode = number;
int count = 1;
int countMode = 1;

for (int i=1; i<7; i++)
{
if (days[i] == number)
{
// count occurrences of the current number
countMode++;
}
else
{
// now this is a different number
if (count > countMode)
{
countMode = count; // mode is the biggest ocurrences
mode = number;
}
count = 1; // reset count for the new number
number = days[i];
}
}
cout<<\"\ Mode value = \"<<days[countMode3]<<\"\ Position = \"<<countMode;
}

//Sorting operation
void Weather::sortData()
{
int x, y, Temp;
for(x = 0; x < 7; x++)
{
for(y = 0; y < 7 - x - 1; y++)
{
if(days[y] > days[y + 1])
{
//Swapping operations
Temp = days[y];
days[y] = days[y + 1];
days[y + 1] = Temp;
}
}
}
}

//Resets the temperature
void Weather::reset(Weather w, double d )
{
for(c = 0; c < 7; c++)
days[c] = d;
}

//Display specified day temperature
void Weather::displaySpecial(int n)
{
cout<<\"\ Day \"<<n<<\" Temperature = \"<<days[n-1];
}

//Display all temperature
void Weather::displayAll()
{
avg();
for(c = 0; c < 7; c++)
{
cout<<\"\ Day \"<<c+1<<\" Temperature = \"<<days[c];
}
cout<<\"\ Average Temperature of Last 7 days = \"<<average;
}

//Calculates the average temperature
void Weather::avg()
{
double tot = 0;
for(c = 0; c < 7; c++)
tot = tot + days[c];
average = tot / 7.0;
}

//Accept temperature
void Weather::accept()
{
cout<<\"\ Enter 7 days temperature \";
for(c = 0; c < 7; c++)
{
cout<<\"\ Enter day \"<<c+1<<\" temperature: \";
cin>>days[c];
}
}

//Parameterized constructor
Weather :: Weather(double t)
{
for(c = 0; c < 6; c++)
days[c] = 50.75;
days[c] = 0.0;
}

//Destructor
Weather :: ~Weather()
{
cout<<\"\ Destructor invoked \";
}

//Default constructor
Weather :: Weather()
{
for(c = 0; c < 7; c++)
{
days[c] = 0.0;
}
}

//Copy constructor
Weather :: Weather(Weather &ww)
{
for(c = 0; c < 7; c++)
{
days[c] = ww.days[c];
}
}

int main()
{
int no;
//parameterized constructor
Weather w(50.75);
//Accept data
w.accept();
//Copy constructors
Weather w1(w);
//Display all the 7 days temperature
w.displayAll();

cout<<\"\ Enter the day number to see the Temperature: \";
cin>>no;
//Display the specified day temperature
w.displaySpecial(no);
//Display the median
w1.median();
//Display the mode
w1.mode();
}

Output 1:

Enter 7 days temperature
Enter day 1 temperature: 10

Enter day 2 temperature: 40

Enter day 3 temperature: 20

Enter day 4 temperature: 30

Enter day 5 temperature: 40

Enter day 6 temperature: 30

Enter day 7 temperature: 40

Total = 210
Day 1 Temperature = 10
Day 2 Temperature = 40
Day 3 Temperature = 20
Day 4 Temperature = 30
Day 5 Temperature = 40
Day 6 Temperature = 30
Day 7 Temperature = 40
Average Temperature of Last 7 days = 30
Enter the day number to see the Temperature: 3

Day 3 Temperature = 20
Sorted Array 10 20 30 30 40 40 40
Median = 30
Mode value = 40
Position = 4
Destructor invoked
Destructor invoked

Output 2:

Enter 7 days temperature
Enter day 1 temperature: 30

Enter day 2 temperature: 20

Enter day 3 temperature: 10

Enter day 4 temperature: 30

Enter day 5 temperature: 50

Enter day 6 temperature: 30

Enter day 7 temperature: 60

Total = 230
Day 1 Temperature = 30
Day 2 Temperature = 20
Day 3 Temperature = 10
Day 4 Temperature = 30
Day 5 Temperature = 50
Day 6 Temperature = 30
Day 7 Temperature = 60
Average Temperature of Last 7 days = 32.8571
Enter the day number to see the Temperature: 4

Day 4 Temperature = 30
Sorted Array 10 20 30 30 30 50 60
Median = 30
Mode value = 30
Position = 3
Destructor invoked
Destructor invoked

 Design an abstract data type called \
 Design an abstract data type called \
 Design an abstract data type called \
 Design an abstract data type called \
 Design an abstract data type called \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site