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

Dynamic allocation of an array into a function with pointer parameters

本文关键字:参数 函数 指针 数组 动态分配      更新时间:2023-10-16

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

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

你对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);

附言

我没有浏览您的其余代码。可能存在其他错误。