0

This is one the methods that I have to make for my assignment

int*mode(int &) – returns an integer array containing the modal value(s) of the
population, the reference parameter should be set to the size of the result

But from searching, you cannot return array's from a function? I can calculate modal value from a given array, put it in an array, but return an integer array? maybe the professor meant something else? I know how to do all other methods except that one. I don't even know what that method means (Coming from java)

Header File

#include <string>

using namespace std;

class population
{
public:
    //Default Constructor
    population(void);

    //Constructor that accepts an integer array object, and the size for that array object
    population(int[], int);

    //Constructor for creating a deep copy
    population (const population &);

    //For overloading purposes
    population & operator = (const population &);

    //Destructor that frees dynamic memory associated with the underlying array
    ~population(void);

    //Method for loading new content into an array
    void load(string);

    //Method for adding new content into existing array
    void add(string);

    //Accessors
    //Returns the size of the population (The number of values stored in the array)
    int size();

    //Returns the sum of the popluation (The sum of the contents in the array)
    int sum();

    //Returns the mean of the population
    float mean();

    //Returns the median of the population
    float median();

    //Returns the standard deviation of the population
    float stddev();

    //Returns an integer array containing the modal values of the popluation
    int*mode(int &);


private:
    int arraySize;
    bool sorted;
    int * popArray;

};

CPP

#include "population.h"


//Default Constructor
population::population(void)
{
}

//Constructor to intialize the population object
population::population(int arr[], int s)
{
    //Store s into a variable
    arraySize = s;

    //Declare popArray as a Dynamic array
    popArray = new int[arraySize];

    //Copy the passed array into the popArray 
    for ( int i=0; i < s; i++)
    {   
        popArray[i] = arr[i];
    };


}

//Constructor for deep copying purposes
population::population (const population & p)
{

}


population::~population(void)
{

}



int population::size(void)
{
    //Return size of the array, which is the amount of population in the array.
    return arraySize;

}

int population::sum(void)
{
    //Variable to hold sum of the array
    int sumArray = 0;

    //Add all the contents of the array into one variable
    for ( int i = 0; i < arraySize; i++)
        {
            sumArray += popArray[i];    
        }

    //Return the sum of the array
    return sumArray;

}

float population::mean(void)
{
    //Variable to hold sum and the mean of the array
    int sumArray = 0;
    float meanArray;

    //Add all the contents of the array into one variable
    for ( int i=0; i < arraySize; i++)
    {
        sumArray += popArray[i];
    }

    //Sum of the array divided by number of contents in the array (Average)
    meanArray = (sumArray / arraySize);


    //Returns mean value in type float
    return meanArray;
}


float population::median ()
{
    return 1;
}

float population::stddev()
{
    return 1;
}

int population::*mode(int & i)
{

    return 



}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Aaron
  • 959
  • 2
  • 9
  • 14
  • [Here](http://stackoverflow.com/questions/3473438/c-return-array-in-a-function) similar question and good examples for you. – Ilya Sep 13 '13 at 04:03
  • Why is mode being declared as a pointer? – Aaron Sep 13 '13 at 04:06
  • I would try staying awake in your class, this will help you avoid not knowing how to do the work in the assignments. – kfsone Sep 13 '13 at 04:41
  • Unrelated: Avoid putting `using namespace` in a header file. – WhozCraig Sep 13 '13 at 04:52
  • to not deal with the memory, i would suggest you to use `std::vector` to do the job for you. you can return it as you like and have to troubles with leaks or validating the ptr – Zaiborg Sep 13 '13 at 06:04
  • You are right, you cannot return an array from a function, but people often use words loosely. What your professor means is to return a pointer to the first element of an array. That is allowed. Probably the array should be dynamically allocated in the mode method. – john Sep 13 '13 at 06:06

2 Answers2

2

With a prototype like that, I'm guessing that he wants you to new an array to be returned:

int *population::mode(int & i)
{
    // compute the number of modal values you need to return

    i = /* whatever the size of the return array will be */;
    int * ret = new int[i];

    // fill in ret

    return ret;
}
T.C.
  • 133,968
  • 17
  • 288
  • 421
0

Try:

int foo(int arrayBar[])

or

int foo(int* arrayBar)

or

int* foo(int arrayBar[])

If those don't work, make sure your pointer is at the beginning of the array.

Source: Return array in a function

Community
  • 1
  • 1
JaneGoodall
  • 1,478
  • 2
  • 15
  • 22
  • The professor already provided the header, and that's the one I have to use. The reference parameter is the size of the array. – Aaron Sep 13 '13 at 04:11