Change the type TemperatureList given in Display 11.10 by adding a member function called get_temperature, which takes one int argument that is an integer greater than or equal to 0 and strictly less than MAX_LIST_SIZE. The function returns a value of type double, which is the temperature in that position on the list. So, with an argument of 0, get_temperature returns the first temperature; with an argument of 1, it returns the second temperature, and so forth. Assume that get_temperature will not be called with an argument that specifies a location on the list that does not currently contain a temperature.

Short Answer

Expert verified
Now, I will create a short answer based on the provided step-by-step solution. To add the `get_temperature` function to the `TemperatureList` class, follow these steps: 1. Add the `get_temperature` function declaration in the public section of the `TemperatureList` class: ```cpp class TemperatureList { public: // ... double get_temperature(int position); // New function added here // ... }; ``` 2. Implement the `get_temperature` function: ```cpp double TemperatureList::get_temperature(int position) { if (position >= 0 && position < MAX_LIST_SIZE) { return list[position]; } else { // Handle possible error scenario std::cerr << "Invalid position argument. Must be greater than or equal to 0 and strictly less than MAX_LIST_SIZE" << std::endl; return -1; } } ``` This function first checks if the provided integer position is within the valid range (0 to MAX_LIST_SIZE - 1). If it is within the range, it returns the temperature value at that position. Otherwise, an error message is printed, and an invalid value of -1 is returned.

Step by step solution

01

Add a new member function to the class

Add the get_temperature function declaration in the public section of the TemperatureList class: ```cpp class TemperatureList { public: TemperatureList(); void add_temperature(double new_temperature, int position); double get_temperature(int position); // New function added here void output(ostream& outs); private: double list[MAX_LIST_SIZE]; int temperatures_stored; }; ```
02

Implement the get_temperature function

Now we'll define the get_temperature function: ```cpp double TemperatureList::get_temperature(int position) { if (position >= 0 && position < MAX_LIST_SIZE) { return list[position]; } else { // Handle possible error scenario (not required as per the problem's // assumption, but can be added for better error handling) std::cerr << "Invalid position argument. Must be greater than or equal to 0 and strictly less than MAX_LIST_SIZE" << std::endl; return -1; } } ``` In this function, we first check if the provided integer position is within the valid range (0 to MAX_LIST_SIZE - 1). If it is within the range, we return the temperature value at that position. Otherwise, we can print an error message and return an invalid value, such as -1 (although this scenario is assumed to not happen according to the exercise, having some error handling is preferable). With the above steps, we have successfully added and implemented the get_temperature function in the TemperatureList class.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Following is the definition for a class called Percent. Objects of type Percent represent percentages such as 10% or 99%. Give the definitions of the overloaded operators >> and << so that they can be used for input and output with objects of the class Percent. Assume that input always consists of an integer followed by the character ‘%’, such as 25%. All per- centages are whole numbers and are stored in the int member variable named value. You do not need to define the other overloaded operators and do not need to define the constructor. You only have to define the overloaded operators >> and <<. #include using namespace std; class Percent { public: friend bool operator ==(const Percent& first, const Percent& second); friend bool operator <(const Percent& first, const Percent& second); Percent( ); Percent(int percent_value ); friend istream& operator >>(istream& ins, Percent& the_object); //Overloads the >> operator to input values of type //Percent. //Precondition: If ins is a file input stream, then ins //has already been connected to a file. friend ostream& operator <<(ostream& outs, const Percent& a_percent); //Overloads the << operator for output values of type //Percent. //Precondition: If outs is a file output stream, then //outs has already been connected to a file. private: int value; };

Write a function definition for a function called before that takes two arguments of the type DayofYear, which is defined in Display \(11.1 .\) The function returns a bool value and returns true if the first argument represents a date that comes before the date represented by the second argument; otherwise, the function returns false. For example, January 5 comes before February 2

What is the difference between a (binary) operator and a function?

a. Explain carefully why no overloaded assignment operator is needed when the only data consists of built-in types. b. Same as part (a) for a copy constructor c. Same as part (a) for a destructor.

Here is a definition of a class called Pairs. Objects of type Pairs can be used in any situation where ordered pairs are needed. Your task is to write implementations of the overloaded operator >> and the overloaded opera- tor << so that objects of class Pairs are to be input and output in the form (5,6) (5,-4) (-5,4) or (-5,-6). You need not implement any constructor or other member, and you need not do any input format checking. #include using namespace std; class Pairs { public: Pairs( ); Pairs(int first, int second); //other members and friends friend istream& operator>> (istream& ins, Pairs& second); friend ostream& operator<< (ostream& outs, const Pairs& second); private: int f; int s; };

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free