以下是我的代码,由于某种原因它不会编译。我无法从允许用户动态分配数组,然后使用指向该数组的指针数组,然后对动态分配的数组进行排序。我不断收到编译错误:
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;
}
答案 0 :(得分:0)
//Free dynamically allocated memory
delete[] scores;
scores = 0; <- u delete all your scores
int *arrPtrscores[count];
arrSelectSort(scores, scores[count]); <- and pass null pointer to the function
然后你尝试从arrSelectSort函数中的空指针读取
minElem = arr[startScan];
另外你可能想要这个arrSelectSort(scores, count)
而不是arrSelectSort(scores, scores[count])
在此行的分数之前也添加&
:
arrSelectSort(&scores, scores[count]);
showArrPtr(&scores, count);