Question

This program has an array of floating point numbers as a private data member of a...

This program has an array of floating point numbers as a private data member of a class. The data file contains floating point temperatures which are read by a member function of the class and stored in the array.

Exercise 1: Why does the member function printList have a const after its name but getList does not?

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

the code is as follows:

// This program reads floating point data from a data file and places those

// values into the private data member called values (a floating point array)

// of the FloatList class. Those values are then printed to the screen.

// The input is done by a member function called GetList. The output

// is done by a member function called PrintList. The amount of data read in

// is stored in the private data member called length. The member function

// GetList is called first so that length can be initialized to zero.

// PLACE YOUR NAME HERE

#include <iostream>

#include <fstream>

#include <iomanip>

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");

// Fill in the code that calls the getList function.

// Fill in the code that calls the printList function.

return 0;

}

FloatList::FloatList()

{

// Fill in the code to complete this constructor that

// sets the private data member length to 0

}

// Fill in the entire code for the getList function

// The getList function reads the data values from a data file

// into the values array of the class FloatList

// 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

// This program reads floating point data from a data file and places those
// values into the private data member called values (a floating point array)
// of the FloatList class. Those values are then printed to the screen.
// The input is done by a member function called GetList. The output
// is done by a member function called PrintList. The amount of data read in
// is stored in the private data member called length. The member function
// GetList is called first so that length can be initialized to zero.
// PLACE YOUR NAME HERE

#include <iostream>

#include <fstream>

#include <iomanip>

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.

float average() const; //Member function to get the average of data

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
FloatList list;

cout << fixed << showpoint;

cout << setprecision(2);

tempData.open("temperatures.txt");

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

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

return 0;

}

FloatList::FloatList()

{

// Fill in the code to complete this constructor that
// sets the private data member length to 0
length = 0;

}

// Fill in the entire code for the getList function
// The getList function reads the data values from a data file
// into the values array of the class FloatList

void FloatList::getList(std::ifstream &in)
{
if(!in.is_open()){
cout << "ERROR: could not read input file" << endl;
return;
}

while(in >> values[length])
length++;

in.close();
}

// 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
void FloatList::printList() const
{

for(int i = 0; i < length; i++){
cout << values[i] << endl;
}
cout << "The average temperature is " << average() << endl;
}

float FloatList::average() const{
float avg = 0;
for(int i = 0; i < length; i++){
avg += values[i];
}
return avg/length;
}

FloatList::~FloatList(){
}


input file: temperatures.txt
==============
78.90
87.40
60.80
70.40
75.60

output
====
78.90
87.40
60.80
70.40
75.60
The average temperature is 74.62

Add a comment
Know the answer?
Add Answer to:
This program has an array of floating point numbers as a private data member of a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • 1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify...

    1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify the initialize function. ESIGN PAGE LAYOUT REFERENCES an-E4_]Ka"!Aa.le' | =.=-'E- 恒栏1순↓ | T Font Paragraph // To declare a class for the Date ADT // This is the header file DateType.h class DateType public: void Initialize (int newMonth, int newDay, int newYear) int GetYear) const; int GetMonth() const int GetDay) const private: int year; int month; int day; search

  • 15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor init...

    15. Define a class called Point. It has two private data members: x and y with int type; and three public member functions: a constructor, setPoint(int, int) and printPoint0. The constructor initializes the data members to 0. You need to use the operator?: to test whether x and y are positive. If not, assign them to 0. The function printPoint prints the message "(X, Y)" where X and Y are two integers. In the main program, first input the number...

  • The 4th deliverable is to create the program the makes the buy recommendation. It uses the...

    The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

  • 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments

    9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in the main program that reads an integer N  (that is not more than 50) from standard input and then reads N  integers from a file named...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT