A name such as 'Get...' indicate to me that it is a function but you say it is a variable so I am slightly confused.

The point that GetValues appear to not be declared anywhere also increases that confusion.

Assuming it is a function and that it is supposed to get a bunch of values (unknown amount) the best way to handle that is to have a class for it. STL already have a class that can handle a varying amount of data of a specified type. It is called 'list'.

With this in mind the GetValues should be a function to return a list and thus that is also what AboveAndBelow() should receive.

Now AboveAndBelow also return three values but it makes an array for it. Personally I think that is a bad choice. These three variables have different meanings. One is count of values below 100, one is count of values equal to 100 and one is count of values greater than 100. Personally I think that should be a class too - or at least a struct, most likely a class.

Now the main function must allocate room for the list and the count objects:

count c;
list<int> values;

GetValues(values);
AboveAndBelow(c,values);

will do the trick.

Also, as a matter of style I would remove those comments you have, they are on the whole useless as comments goes :-)

void foo(); // function prototype

doesn't tell me anything more than what I can read from the code. The comment doesn't add any more info.

Similarly:

void foo() // definition of foo
{
...
}

Comments are good but only if they actually tell you something that isn't obvious from the code.

For example:

// The function here uses the algorithm explained in the
// file foobar.txt section 2.1.3 and uses the variable
// gnarf to hold the number of gazonks that we have
// encountered so far.

is a useful type of comment. Another type is this:

// The class here implements a circular buffer, the
// variable m_put is the index of the first free element
// where we want to place a new value on next put()
// and m_get is the index of the oldest present element
// in the buffer. If m_put > m_get then m_put - m_get
// == m_count == the number of elements in the buffer.
// if m_put < m_get then m_put + N - m_get == m_count
// == the number of elements in the buffer.
// if m_get == m_put the buffer is either full if
// m_count == N or empty if m_count == 0.
// m_size holds the current value of N.

This comment is typically placed in front of a class and explains how this class is implemented. It is not a part of the interface, users of the class doesn't need to know this so you might also want to consider placing it in the .cpp or .cxx file instead of in the .h file. But it does explain what is the role of each of the member variables declared in the class and as such provide useful info.

Comments like:

x += 3; / increment x by 3

are rather useless and should not be used. They just clobber the code without adding any useful information.

Alf

0

kamarjaAuthor Commented: 2003-03-24

Thanks for your response. This not what I want though. The funcion ReadData is asking the user to enter any amount of integers less than 100, then assign this number to max. Thus getValues[max]. I don't want a static listing of array values, I want it to be dynamic. Should ReadData be returning a pointer to getValues[max]? If so, then how do i pass getValues and max to AboveandBelow?

Obviously I am new to this. I haven't learned STL as yet but I will definately cut and pass your comments. For now I am only allowed to use Functions to do this project. getValues is an array NOT a function. I have a working program but the program uses MAIN to ask the user for 'max', then uses 'getValues = new [max]' to create the array, main then calls ReadData and passes (getValues, max). I am trying to do all function calls from main. This is why I wanted to put everything in ReadData then send the array 'getValues' and 'max' - getValues[max] to AboveandBelow.Sorry about the useless comments, you are right :-). Thanks for your comment Salte.

You can allocate an array to hold the count (an array of 3 elements) but it is better coding style to use a struct or class to hold the count. Since the size is known at compile time (always 3 values) you might as well declare it as a struct or class object or an array of three values in Main() and just pass that object to AboveAndBelow.

If you use new to allocate the array you must also remember to delete [] it after:

Ok, this sounds like homework and we are not permitted to help with homework per se. However, we can help with specific questions and if you create an array named 'getValues' or whatever then the obvious solution is to pass that array as parameter to the function AboveAndBelow:

Since you already know how many elements GetValues is supposed to have this is very straight forward. The function that loop and fill in the values into the array can also receive the array as argument.

The result values (three counters) if they have to be in an array as well, the natural thing is for Main() to allocate room for it like this:

However, the natural thing for the counters would be to have a struct or a class that can hold the three values.

Another alternative is to allcoate an array or the class object from heap and return it. If so you can declare AboveAndBelow as:

int * AboveAndBelow(const int values[], int max)
{
...
}

In this case you must remember to delete [] the array returned once you're done with it. The array given by values is most likely an array created on heap since the size is dynamic and so that too must be deleted by

delete [] GetValues;

I do believe that the name 'GetValues' is a bad name for an array. Names with verbs in them indicate an action and as such indicate that it is a function and not an array. An array should just be named 'values' or 'Values' or some such.

Also, in the above I have used 'int values[]' as parameter, this is essentially the same as 'int * values' but is used to indicate that this pointer isn't a pointer to a single value but a pointer to an array. It doesn't make a difference to the compiler but is just information to the human reader. However, it isn't used everywhere, I frequently declare functions with 'char * buf' where the buffer is actually an array and so should have been declared as 'char buf[]' if I were to be consistent. Some times I remember to do that and some times I don't :-) However, you should never use 'T ptr[]' if the ptr is a pointer to a single object and not to a buffer.

I am not qute sure what you are getting at, but it appears that something like this should work for you:

1. Get the value of max into an int.
2. Create the array of values from heap by saying new int[max]. This array will not have any values at the moment but it has room for them.

3. Call ReadData(values,max) to fill in the array with the values.

4. Call AboveAndBelow(values,max,counters) or
int * counters = AboveAndBelow(values,max); Either way to get the result.

Now, you can't have a function returning two values but you can make a class or struct that can hold two values and a function can return that class or struct. So if you want to have values and max tied together you could make a class like this:

And then inside the body of AboveAndBelow use arr.max() to get the maximum value, arr[0] to get index 0, arr[4] to get index 4 of array etc or you could use arr.arr() to get the pointer to the whole array.

If you still want the function to return a pointer to an array just remove the argument 'counters & c' and replace it with an int * return. Just remember to deallocate the counters array in main(). Also, the array pointed to by m_ptr should be deleted in ~array(): delete [] m_ptr;

Hope this helps.

Alf

0

kamarjaAuthor Commented: 2003-03-25

That helped a great deal Salte, you earned these points. I increased it to 100 :-). Thanks a lot for taking the time to explain. It is for homework, but I thought I would get away with it since I did most of the work on my own :-). Besides, I want to learn the right way to program NOT just be able to program.