0

这是我必须为我的任务做的方法之一

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 



}
4

2 回答 2

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;
}
于 2013-09-13T05:34:50.303 回答
0

尝试:

int foo(int arrayBar[])

或者

int foo(int* arrayBar)

或者

int* foo(int arrayBar[])

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

来源:在函数中返回数组

于 2013-09-13T04:06:41.160 回答