Link to home
Start Free TrialLog in
Avatar of kamarja
kamarja

asked on

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;
}





Avatar of oferh
oferh

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;
}


   
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
Avatar of kamarja

ASKER

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?
Avatar of kamarja

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kamarja

ASKER

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.
Avatar of kamarja

ASKER

Thanks for the pointers...