Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include using namespace std; const int MAX_LENGTH = 50; // MAX_LENGTH contains the maximum length of our list class FloatList // Declares a

#include

#include

#include

using namespace std;

const int MAX_LENGTH = 50; // MAX_LENGTH contains the maximum length of our list

class FloatList // Declares a class that contains an array of

// floating point numbers

{

public:

void getList(ifstream&); // Member function that gets data from a file

void printList() const; // Member function that prints data from that

// file to the screen.

FloatList(); // constructor that sets length to 0.

~FloatList(); // destructor

private:

int length;

// Holds the number of elements in the array

float values[MAX_LENGTH];

// The array of values

};

int main()

{

ifstream tempData; // Defines a data file

// Fill in the code to define an object called list of the class FloatList

cout << fixed << showpoint;

cout << setprecision(2);

tempData.open("temperatures.txt");

List.getList(tempData);// Fill in the code that calls the getList function.

List.printList();// Fill in the code that calls the printList function.

return 0;

}

FloatList::FloatList()

{

Length=0;// Fill in the code to complete this constructor that

// sets the private data member length to 0

}

void FloatList::getList(ifstream& data)

{

float item;

data >> item;

while(data)

{

// Fill in the entire code to store item in the array values

length++;

data >> item;

}

}

void FloatList::printList() const

{

// Fill in the entire code for the printList function

// The printList function prints to the screen the data in

// the values array of the class FloatList

}

FloatList::~FloatList()

{

// Fill in the code for the implementation of the destructor

}

Exercise 2: Fill in the code so that the program reads in the data values from the temperature file and prints them to the screen with the following output:

78.90

87.40

60.80

70.40

75.60

Exercise 3: Add code (member function, call and function implementation) to print the average of the numbers to the screen so that the output will look like the output from Exercise 2 plus the following:

The average temperature is 74.62

float FloatList::findAverage() const

{

float sum = 0;

for (int count = 0; count < length; count++)

sum = sum + values[count];

return sum / length;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions