Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数组动态分配到具有指针参数的函数中_C++_Pointers_Memory Management_Dynamic - Fatal编程技术网

C++ 将数组动态分配到具有指针参数的函数中

C++ 将数组动态分配到具有指针参数的函数中,c++,pointers,memory-management,dynamic,C++,Pointers,Memory Management,Dynamic,我在通过一系列函数通过指针传递数组时遇到问题。我使用动态分配来创建函数。即使这是成功的,我也不能让它通过以指针为参数的函数。函数返回平均中值和模式,并已完成。但是,在将它们转换为指针语法时,我无法传递它们。提前谢谢你的帮助 #include <iostream> #include <cstdlib> using namespace std; int students; int * studentarray; int stumode; double stuavg;

我在通过一系列函数通过指针传递数组时遇到问题。我使用动态分配来创建函数。即使这是成功的,我也不能让它通过以指针为参数的函数。函数返回平均中值和模式,并已完成。但是,在将它们转换为指针语法时,我无法传递它们。提前谢谢你的帮助

#include <iostream>
#include <cstdlib>


using namespace std;
int students;
int * studentarray;
int  stumode;
double  stuavg;
int  stumed;
int arr;


int mode(int *[], int );
double average(int *[], int);
double median(int *[], int);
void selectSort(int [], int);
void swap(int *, int *);
int makeArray(int*, int);

int main()
{
    studentarray = &arr;
    cout << "How many students are there?" << endl;
    cin >> students;

    makeArray(studentarray, students);

    for (int i = 0; i < students; i++) {
        cout << "How many movies did student " << i + 1 << " view?" << endl;
        cin >> studentarray[i];
    }

    selectSort(studentarray, students);
    stumode = mode(&studentarray, students);
    stuavg = average(&studentarray, students);
    stumed = median(&studentarray, students);

    cout << "The array has been sorted in ascending order." << endl;
    cout << "The mode is " << stumode << "." << endl;
    cout << "The mean is " << stuavg << "." << endl;
    cout << "The median is " << stumed << "." << endl;

    delete[] studentarray;

    return 0;
}

int mode(int *arr, int size)
{
    if (size <= 0) return 0;

    int most = 0, position = 0, most_count = 0;
    int counter = 1;
    for (int i = 1; i < size; i++) 
    {
        if (* (arr +  i) != * (arr + position) ) 
        {
            if (counter > most)
            {
                most = counter;
                most_count = 0;
            }
            else if (counter == most) most_count++;
            position = i;
            counter = 0;
        }
        else counter++;
    }
    if (most_count) return 0;
    else return * ( arr + position );
}

double average(int *arr, int size)
{
    if (size <= 0) return 0;
    int total = 0;
    for (int i = 0; i < size; i++) {
        total += *(arr + i);
    }
    return (double)total / size;
}

double median(int *arr, int size)
{
    if (size <= 0) return 0;
    if (size % 2 == 0)
        return (double) (* (arr + (size + 1) / 2));
    else {
        int mid = size / 2;
        return (double)(* (arr + mid) + * (arr + mid + 1) / 2);
    }
    return 0;
}


void selectSort(int arr[], int size)
{
    int min;
    for (int i = 0; i < size - 1; i++)
    {
        min = i;
        for (int j = i + 1; j < size; j++)
        {
            if ( arr[j] < arr[min])
            {
                min = j;
            }
        }
        swap(&arr[min], &arr[i]);
    }

 }

void swap(int *one, int *two) {
    int temp = *one;
    *one = *two;
    *two = temp;
}

int makeArray(int *arr, int size)
{
    arr = new int[size];
    return *arr;
}
#包括
#包括
使用名称空间std;
国际学生;
国际*学生宿舍;
int模式;
双stuavg;
int stumed;
int arr;
int模式(int*[],int);
双平均值(int*[],int);
双中位数(int*[],int);
void selectSort(int[],int);
无效掉期(整数*,整数*);
int makeArray(int*,int);
int main()
{
学生阵列=&arr;
cout学生;
makeArray(学生阵列,学生);
for(int i=0;icout您对
makeArray
的实现不正确

int makeArray(int *arr, int size)
{
    // Allocates memory and assigns it to arr.
    // This is a local change to arr. The value of the variable in
    // main remains unchanged.
    arr = new int[size];

    // Returns an uninitialized value.
    return *arr;

    // The memory allocated in the previous line is now a memory leak.
}
您可以使用以下方法使其更简单:

int* makeArray(int size)
{
   return new int[size];
}
并在
main
中将其用作:

arr = makeArray(students);
但是,我不认为这比使用:

arr = new int[students];
如果这样做,
makeArray
将变得不必要。如果
makeArray
需要额外的代码来用一些值填充数组,它将非常有用。否则,它不会向程序添加任何有用的功能


说到这里,最好使用
std::vector
,而不是在您自己的代码中管理动态分配的内存。您可以使用:

std::vector<int> arr(students);
std::向量arr(学生);

PS


我没有阅读您的其余代码。可能还有其他错误。

感谢您就我的简单性带来的复杂性所作的演讲。我重新启动了visual studio,这就解决了问题。然后我能够自己调试它一次。谢谢。@BrianMoore,不客气。很高兴我能提供帮助。