Confused on boolean functions and classes

closed account (3voN6Up4)
Very confused on what it is asking and how to start. I am completely new to boolean stuff and classes I am still new to as well. Can I get some help getting on the ball and in the right direction? I have tried to do all I could below, and I am lost at the "the stats class should have a boolean storevalue function that accepts a double value from the client program and stores it in the array."

I can't store anything in the array because it's in a class. It's private.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*Create a stats class whose member data includes an array capable of storing 30 double
data values, and whose member functions include total, average, lowest, and highest
functions for returning information about the data to the client program. These are general
versions of the same functions you created for Programming Challenge 7, but now they
belong to the stats class, not the application program.
In addition to these functions, the
stats class should have a Boolean storeValue function that accepts a double value from
the client program and stores it in the array. It is the job of this function to keep track of
how many values are currently in the array, so it will know where to put the next value it
receives and will know how many values there are to process when it is carrying out its
other functions. It is also the job of this function to make sure that no more than 30 values
are accepted. If the storeValue function is able to successfully store the value sent to it, it
should return true to the client program. However, if the client program tries to store a
thirty-first value, the function should not store the value and should return false to the
client program.

The client program should create and use a stats object to carry out the same rainfall
analysis requested by Programming Challenge 7. Notice that the stats object does no I/O.
All input and output is done by the client program.*/
#include <iostream>
using namespace std;
class stats
{
    double Data[30];
    total()
    average()
    lowest()
    highest()
    bool storeValue()
    {
       
    }
};
int main()
{
    
}
I've changed your class a bit to reflect the requirements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class stats {
public :
	stats() {}

	//This function returns the average of the 'values' array.
	double average();

	//This function returns the highest value found in the 'values' array.
	double highest();

	//This function returns the lowest value found in the 'values' array.
	double lowest();

	//This function returns the sum of all values(?)
	double total();

	//This function "appends" a value to the 'values' array.
	bool storeValue(double value);

private :
	const static int max_values = 30;
	double values[max_values] = {};
	int current_value = 0;
};


You weren't able to modify the 'Data' array from outside the class because it's private. Take a look at the snippet I've posted, and you'll see that all functions are public, and the member variables are private.

It is considered good practice to separate member variables - and the functions that modify them - in this way. Generally, "getters" and "setters" and such methods are public, and can be accessed outside of the class.

You'll also notice that I've taken advantage of C++11's in-class initialization for some of the member variables. Your instructor might not approve, but initializing them in the constructor instead is trivial.

I've also renamed your 'Data' array to 'values', and I've added a static constant to define the maximum number of values our array should contain (in this case 30). There's one more member variable I've added named 'current_value', which represents the current index of the "last" element.

Let's talk about the storeValue member function some more.

storeValue should return a bool. We know this because the instructions tell us that it should return true if the function succeeds, and false if it fails.

So, we already know the function signature should look something like this:
bool storeValue();

There's more though. The instructions say it should accept a double and store it in the array. That means, it will have to take a double parameter as well:

bool storeValue(double value);

I've added some pseudo-code here:

1
2
3
4
5
6
7
8
9
10
bool stats::storeValue(double value) {

	//is current_value less than max_values?
	//if yes, set 'values[current_value]' to 'value'.
	//otherwise, return false.

	//increment 'current_value'
	//return true.

}
Last edited on
closed account (3voN6Up4)
@xismn

Hey thanks, if you don't mind I would like to ask a couple questions. I do like that you've taken the time to explain to me what it's asking!

1.) What does line 3 do? stats() {}
2.) You didn't include the double array in the class, did I do that part right? Were you just adding to my class?
3.) Unsure of what line 22 and 23 do.
I know that line 21: const static int max_values = 30; is supposed to reflect on the array, never used static before but isn't it the same as const? Line 22: double values[max_values] = {}; and line 23: double current_value = 0; don't make sense to me. Particularly the {} on line 22 and why there is a current value. Just initializing to 0? I do that for most of my variables and arrays.
What does line 3 do? stats() {}


That is the class constructor. Until you initialize your member variables, they will contain garbage. It's in the body of the constructor that you initialize members.
As you can see, I left the body of the constructor blank - I will explain further down.

You didn't include the double array in the class, did I do that part right? Were you just adding to my class?

The double array is still there. It's on line 22. Your array was named 'Data', but I chose to name mine 'values'.

Unsure of what line 22 and 23 do.


Let's take a look at line 22:
double values[max_values] = {};
This line declares an array named 'values' with max_values number of elements. We know that max_values is an integer with a value of 30 by looking at line 21. Therefore, our double array ('values') has 30 elements (doubles).
The = {}; at the end is taking advantage of a feature that was introduced in C++11, which allows me to initialize non-const member variables (in this case an array) on the line on which it is declared. It used to be that you could only initialize const static integral member variables on the same line on which they are declared, and all other members (such as arrays) had to be initialized in the constructor. If you leave the body of this initialization empty, the default constructor will be invoked - in this case for all 30 doubles, which initializes them to zero.
In summary, line 22 declares an array of doubles named 'values'. It has 30 elements and each one of them is initialized to zero.

Let's take a look at line 23:
int current_value = 0;

I just edited my other post, realizing the type should have been int and not double.

This is a member variable integer named 'current_value'. I've used the same feature I just described to initialize it to zero on the same line on which it was declared. The instructions you've provided don't explicitly necessitate this variable, but it's heavily hinted that you'll have to implement some sort of counter to keep track of which index is considered to be the last one of your array. That's what this variable does. It's simply meant to be used as a counter in your storeValue function.

never used static before but isn't it the same as const?
const and static are entirely different keywords used for different things, but they're commonly seen together in classes.
const just means that this variable cannot be modified. It's value can be read, but it cannot change.
static means that this variable will continue to exist past the current scope. In other words, it's lifetime is not bound by the scope in which it is declared. When your class needs a constant (such as the maximum number of elements in an array), the constant is declared as const static. const because it cannot change, and static because this constant is universally shared between all instances of this class (it's more of a property of the actual class, and not of any instances derived from it).

I hope that makes sense. Let me know if I should clarify some more.
Last edited on
closed account (3voN6Up4)
That seems to make sense now, thank you for your explanations, you are being very helpful to me.

I'm now only confused on if that is all I should have or if I have to include my previous program and use it with the class. (I am not too great at classes or arrays if I have not mentioned before)
The only thing left to do would be to define the function bodies for the member functions 'average', 'highest', 'lowest' and 'total'.
Topic archived. No new replies allowed.