Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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

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