// This program uses a private Boolean function to determine if
// a new value sent to it is the largest value received so far
#include <iostream>
usingnamespace std;
class SimpleStat
{
private:
int largest; // The largest number received so far
int sum; // The sum of the numbers received
int count; // How many numbers have been received
bool isNewLargest(int);
public:
SimpleStat(); // Default constructor
bool addNumber(int);
double getAverage();
int getLargest()
{ return largest; }
int getCount()
{ return count; }
};
// SimpleStat Class Implementation Code
/*
===================
SimpleStat Default Constructor
Initialize the three variables to zero
===================
*/
SimpleStat::SimpleStat()
{
largest = sum = count = 0;
}
/*
===================
SimpleStat::addNumber
===================
*/
bool SimpleStat::addNumber(int num) // function for adding the numbers
{ bool goodNum = true;
if (num >= 0) // If num is valid
{
sum += num; // Add it to the sum
count++; // Count it
if(isNewLargest(num)) // Find out if it is
largest = num; // the new largest
}
else // num is invalid
goodNum = false;
return goodNum;
}
/*
===================
SimpleStat::isNewLargest
===================
*/
bool SimpleStat::isNewLargest(int num)
{
if (num > largest)
returntrue;
elsereturnfalse;
}
/*
===================
SimpleStat::getAverage
===================
*/
double SimpleStat::getAverage()
{
if (count > 0)
returnstatic_cast<double>(sum) / count;
elsereturn 0;
}
// Client Program
/*
===================
Main
===================
*/
int main()
{
int num;
SimpleStat statHelper;
cout << "Please enter the set of non-negative integer \n";
cout << "values you want to average. Separate them with \n";
cout << "spaces and enter -1 after the last value. \n\n";
cin >> num;
while (num >= 0)
{
statHelper.addNumber(num);
cin >> num;
}
cout << "\nYou entered " << statHelper.getCount() << " values. \n";
cout << "The largest value was " << statHelper.getLargest() << endl;
cout << "The average value was " << statHelper.getAverage() << endl;
return 0;
}
Here is the modified version defining addNumber as void:
// This program uses a private Boolean function to determine if
// a new value sent to it is the largest value received so far
#include <iostream>
usingnamespace std;
class SimpleStat
{
private:
int largest; // The largest number received so far
int sum; // The sum of the numbers received
int count; // How many numbers have been received
bool isNewLargest(int);
public:
SimpleStat(); // Default constructor
void addNumber(int);
double getAverage();
int getLargest()
{ return largest; }
int getCount()
{ return count; }
};
// SimpleStat Class Implementation Code
/*
===================
SimpleStat Default Constructor
Initialize the three variables to zero
===================
*/
SimpleStat::SimpleStat()
{
largest = sum = count = 0;
}
/*
===================
SimpleStat::addNumber
===================
*/
void SimpleStat::addNumber(int num) // function for adding the numbers
{ // bool goodNum = true;
if (num >= 0) // If num is valid
{
sum += num; // Add it to the sum
count++; // Count it
if(isNewLargest(num)) // Find out if it is
largest = num; // the new largest
}
// else // num is invalid
// goodNum = false;
// return goodNum;
}
/*
===================
SimpleStat::isNewLargest
===================
*/
bool SimpleStat::isNewLargest(int num)
{
if (num > largest)
returntrue;
elsereturnfalse;
}
/*
===================
SimpleStat::getAverage
===================
*/
double SimpleStat::getAverage()
{
if (count > 0)
returnstatic_cast<double>(sum) / count;
elsereturn 0;
}
// Client Program
/*
===================
Main
===================
*/
int main()
{
int num;
SimpleStat statHelper;
cout << "Please enter the set of non-negative integer \n";
cout << "values you want to average. Separate them with \n";
cout << "spaces and enter -1 after the last value. \n\n";
cin >> num;
while (num >= 0)
{
statHelper.addNumber(num);
cin >> num;
}
cout << "\nYou entered " << statHelper.getCount() << " values. \n";
cout << "The largest value was " << statHelper.getLargest() << endl;
cout << "The average value was " << statHelper.getAverage() << endl;
return 0;
}
My only guess is that by defining addNumber as bool one can include an else statement. But this also produces a few lines of (seemingly) superfluous code -- unless this is considered cleaner programming.
SimpleStat::addNumber() only accepts positive numbers, so the variable sum keeps increasing and so isNewLargest(num) is also true unless a non-positive number is entered in which case the function returns – so is this one way of accepting positive numbers only from user and calculating the sum, average and the max sum?
Any yes, it would also work with addNumber() returning void and you could also consider spewing out the sum, count and average from within addNumber() itself
Bool would be used so you can tell if the function succeeded or not
all functions could be bool in this sense, rather void might be enough with this bit as suggested earlier that can tell whether the function ran successfully or not:
with addNumber() returning void and you could also consider spewing out the sum, count and average from within addNumber() itself
Using a function that returns bool (or int) would make the code cleaner. (e.g. non-messy main function). On line 125 that condition wouldn't be needed. If the function is doesn't return anything then at least check the condition in another function.