[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

How do I send these values to another function?

Hello Guys.

I need to return the variable (getValues and max) to AboveandBelow()or to Main, The objective is to send it to AboveandBelow(). The Code is below. Thanks in Advance.

#include <iostream>
#include <iomanip>
using namespace std;

typedef int *myPointer;    
int CheckErrors(int max);                                //Function prototype
int ReadData(void);                                      //Function prototype
int * AboveAndBelow(int *underaHundred, int);            //Function prototype
void PrintData(int *);                                   //Function prototype
const int MAX = 100;
const int MIN = 1;

int main()
{
     int values = 3;
     int *findings = new int [values];
        findings = AboveAndBelow(getValues, max);
        ReadData();
        PrintData(findings);
     delete []findings;
     return 0;
}

int CheckErrors(int max)                                   //Definiton for CheckErrors
{
     if(max < 1)
     {
          cout<<"Error: input "<<max<<" is too small"<<endl;
          return 1;
     }

     if(max>100)
     {
          cout<<"Error:input "<<max<<" is too large"<<endl;
          return 1;
     }
     return 0;
}

int ReadData(void)                         //Definition for ReadData
{
     int max;    
     cout<<"HOW MANY NUMBERS DO YOU WANT TO ENTER?:\n"
     <<"You can not enter more than 100 integers!!\n";
     cin>>max;

     while ((max > MAX) || (max < MIN))
     {
          cin>>max;
          CheckErrors(max);
     }

     cout<<endl;
     cout<<"Enter "<<max<<" numbers, separated by a space and end with ENTER:\n";

     myPointer getValues;                                
     getValues = new int [max];
     for(int count = 0; count < max; count++)
     {
          cin >> getValues[count];
     }
          delete []getValues;
          return 0;
}

int * AboveAndBelow(int *status, int max)                      //Definition for AboveandBelow
{
     int under = 0;
     int over = 0;
     int equiv = 0;
     int results = 3;
     int count = 0;
     int * answer = new int [results];
     for(status; count < max; count++)
     {
          if((*(status+count) < 100))
          {
               under++;
          }
          if((*(status+count) > 100))
          {
               over++;
          }
          if((*(status+count) == 100))
          {
               equiv++;
          }

          answer[0] = under;
          answer[1] = over;
          answer[2] = equiv;
     }
     return answer;
}

void PrintData(int *result)                                      //Definition for PrintData
{
     cout<<endl;
     cout<<"  ---------------------------------------------------"<<endl;
     cout<<" |Your Results According to the Integers You Entered|"<<endl;
     cout<<" |";
     cout<<"--------------------------------------------------|"<<endl;
     cout<<" |There were "<<*(result+1)<<" Integers above a Hundred             |"<<endl;
     cout<<" |There were "<<*(result)<<" Integers under a Hundred             |"<<endl;
     if((*(result+2)))
     {
     cout<<" |There were "<<*(result+2)<<" Integer(s) equal Hundred             |"<<endl;
     }
     cout<<" |==================================================|"<<endl;
     cout<<" |==================================================|"<<endl;
     cout<<endl;
}





0
kamarja
Asked:
kamarja
  • 4
  • 3
1 Solution
 
oferhCommented:
the function AboveAndBelow should have the followin interface:
int * AboveAndBelow(int *status, int &max)

main should be:
int main()
{
    int values = 3;
    int getValues = 0;
    int max = 5;
    int getValues[] = {100, 321,900, 10, 4};
       findings = AboveAndBelow(getValues, max);
       ReadData();
       PrintData(findings);
    delete []findings;
    return 0;
}


   
0
 
SalteCommented:
Hmm...

What is 'GetValues' in your code?

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.

so:

list<int> GetValues()
{
   list<int> values;
   ... read the values and stuff them into 'values'....
   return values;
}

Thinking about this a bit you might realize that you will make a copy there and copy the elements over to a new list and then destroy the local variable values, so a better idea is to do:

int GetValues(list<int> & values)
{
   values.clear(); // clear the list.
   ... read the values and stuff them into values...
   return values.count();
}

Now, this function is actually pretty good :-)

Next the AboveAndBelow function, I suggest you make a class like this:

class count {
private:
   int m_below;
   int m_equal;
   int m_above;

   friend void AboveAndBelow(count & cnt, const list<int> & values);

   void clear() { m_below = m_equal = m_above = 0; }

public:
   count() : m_below(0), m_equal(0), m_above(0);
   int below() const { return m_below; }
   int above() const { return m_above; }
   int equal() const { return m_equal; }
};

void AboveAndBelow(count & cnt, const list<int> & values)
{
   // loop through values and count values above, below and
   // equal to 100.
   cnt.clear();
   for (list<int>::iterator p = values.begin();
        p != values.end(); ++p) {
      int d = *p - 100;
      if (d > 0)
         cnt.m_above++;
      else if (d < 0)
         cnt.m_below++;
      else
         cnt.m_equal++;
   }
}

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:
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?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kamarjaAuthor Commented:
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.
0
 
SalteCommented:
list<int> is very dyanmic, it doesn't even have a value to indicate the size when you create, it just adds elements as needed.

If you know how many values to read prior to GetValues call then you can also use a vector instead of list:

vector<int> vec;

as soon as you know how many values (max) you can do:

vector<int> vec(max);

and then call GetValues(vec); to let GetValues fill in the max values into vec.

You can also use 'new int' as you did if so the GetValues function will return that pointer and so the AboveAndBelow function must get both that pointer as well as the max value as arguments:

AboveAndBelow(..., const int arr[], int max)
{
   // walk through the array and modify the count as needed.

}

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:

delete [] arr;

when you no longer need the array.

Alf
0
 
SalteCommented:
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:

int counters[3];

and pass that as a parameter as well.

AboveAndBelow would then be something like:

void AboveAndBelow(const int values[], int max, int counters[3])
{
  ....
}

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:

class array {
private:
   int * m_ptr;
   int m_max;

public:
   array();
   ~array();
   void Allocate(int max);
   void ReadData();
   int operator [](int k) const;
   int & operator [] (int k);
   int max() const { return m_max; }
   const int * arr() const { return m_ptr; }
};

void AboveAndBelow(const array & arr, counters & c);

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:
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.
0
 
kamarjaAuthor Commented:
Thanks for the pointers...
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now