【发布时间】:2014-08-19 22:20:31
【问题描述】:
所以下面是我的代码,由于某种原因它不会编译。我无法从允许用户动态分配一个数组,到使用指向该数组的指针数组,再到对该动态分配的数组进行排序。我不断收到编译器错误提示:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'arrPtrscores': unknown size
另外,我需要知道如何使用输入验证来确保没有输入负测试分数。
代码如下:
//Write a program that dynamically allocates an array large enough to hold
//a user-defined number of test scores. Once all the scores are entered,
//the array should be passed to a function that sorts them in ascending order.
//Another function should be called that calculates the average score. The
//program should display the sorted list of scores and averages with
//appropriate headings. Use pointer notation rather than array notation whenever
//possible.
//Input Validation: Do not accept negative numbers for test scores.
#include<iostream>
#include<iomanip>
using namespace std;
//Function prototypes.
void arrSelectSort(int *, int);
void showArray(int *, int);
void showArrPtr(int *, int);
int main()
{
int *scores, //To dynamically allocate an array (pointer)
total = 0, //Accumulator
average; //To hold average test scores.
int numTests, //To hold the number of days of tests
count; //Counter variable
//Get the number of tests.
cout << "How many tests would you like to enter? ";
cin >> numTests;
//Dynamically allocate an array large enough to hold that many test scores.
scores = new int[numTests];
//Get the scores for each test.
cout << "\nEnter the test scores.\n";
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> scores[count];
}
//Calculate the total test score.
for (count = 0; count < numTests; count++)
{
total += scores[count];
}
//int *
//Calculate the average sales per day.
average = total / numTests;
//Display the results.
cout << "\n\nTotal Score: " << total << endl;
cout << "\n\nAverage Score: " << average << endl;
//Free dynamically allocated memory
delete[] scores;
scores = 0; //Make scores point to null.
//An array of pointers to int.
int *arrPtrscores[count];
//Sort the elements of the array of pointers.
arrSelectSort(scores, scores[count]);
//Display the Test Scores in ascending order.
cout<<"In ascending order, the test scores are: \n";
showArrPtr(scores, scores[count]);
system("pause");
return 0;
}
//Definition of function arrSelectSort.
//This function performs an ascending order selection sort.
void arrSelectSort(int *arr[], int size)
{
int startScan;
int minIndex;
int *minElem;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = arr[startScan];
for (int index = startScan + 1; index < size; index++)
{
if(*(arr[index]) < *minElem)
{
minElem = arr[index];
minIndex = index;
}
}
arr[minIndex] = arr[startScan];
arr[startScan] = minElem;
}
}
//Definition of function showArray.
//This function displays the contents of arr. size is the number of
//elements.
void showArray(const int arr[], int size)
{
for(int count = 0; count < size; count++)
cout<<arr[count]<< " ";
cout<<endl;
}
//Definition of function showArrPtr.
//This function displays the contents of the array pointed to by arr.
//size is the number of elements.
void showArrPtr(int *arr[], int size)
{
for(int count = 0; count < size; count++)
cout<<*(arr[count])<<" ";
cout<<endl;
}
【问题讨论】:
-
int **arrPtrscores = new int*[count]; -
int *arrPtrscores[count];您的问题很可能在这里。可变大小的堆栈分配数组是特定编译器的扩展(您的显然不支持)。正如@NeilKirk 建议的那样,在那里使用new[]。 -
所以我会将该定义添加到 int main() 中?原谅基本的问题。我是一个绝对的初学者。非常感谢!
-
对不起,我明白你在说什么。替换该行。谢谢。没有睡觉。我会使用 while 循环来进行输入验证吗?
-
我尝试使用 int *arrPtrscores = new int*[count]; 编译它行,但它仍然不会编译。错误如下: 错误 C2057: 预期的常量表达式 C2466: 无法分配常量大小为 0 的数组 C2440: 'initializing': cannot convert from 'int **' to 'int **[]'
标签: c++ arrays pointers dynamic