返回一个整数数组

时间:2013-09-13 03:58:16

标签: c++

这是我必须为我的作业制作的方法之一

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

但是从搜索中,你不能从函数返回数组?我可以从给定数组计算模态值,将它放在一个数组中,但返回一个整数数组?也许教授意味着别的什么?我知道如何做除了那个以外的所有其他方法。我甚至不知道那个方法意味着什么(来自java)

标头文件

#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 



}

2 个答案:

答案 0 :(得分:2)

使用这样的原型,我猜他希望你new返回一个数组:

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

答案 1 :(得分:0)

尝试:

int foo(int arrayBar[])

int foo(int* arrayBar)

int* foo(int arrayBar[])

如果这些不起作用,请确保指针位于数组的开头。

来源:Return array in a function