c++获得最高分和使用指针
C++ getting highest test score and using pointers
创建了一个程序来提示用户输入考试成绩,并找到平均分、最高分,并对考试成绩进行排序。我遇到了问题,我似乎无法从我所做的函数中获得最高的测试分数,当我创建排序测试分数函数时,程序崩溃了。谁能看看我的函数,看看我的问题在哪里,我似乎得到平均考试成绩显示。
double getAverage(double*, int);
double gethighest(double*, int);
double getlowest(double*, int);
void arrayAscending(double*, int);
int main()
{
// Variable declarations
double *testScores; // Dynamically allocate an array of test scores
double average, highest, lowest;
int numTests; // Number of test scores
int count; // Counter variable
// Get the number of tests
cout << "How many tests do you wish to process? ";
cin >> numTests;
// Verify input is not a negative number
while (numTests <= 0)
{
// Get input again.
cout << "Please enter a positive number: ";
cin >> numTests;
}
// Dynamically allocate an array large enough to hold the test scores
testScores = new double[numTests];
// Get the specified number of test scores
cout << "Enter the test scores below.n";
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> *(testScores + count);
// Verify input is not a negative number
while (*(testScores + count) < 0)
{
// Get input again.
cout << "Please enter a valid test score.n";
cin >> *(testScores + count);
}
}
// Calculate the average test score
average = getAverage(testScores, numTests);
highest = gethighest(testScores, numTests);
lowest = getlowest(testScores, numTests);
// Display the results.
arrayAscending(testScores, numTests);
cout << endl;
cout << "The average of those scores is: " << average << endl;
cout << "The higest of those scores is: " << highest << endl;
cout << "The lowest of those scores is: " << lowest << endl;
// Free dynamically allocated memory
delete[] testScores;
testScores = 0; // Make testScores point to null
return 0;
}
//function getAverage - calculates the average of the test scores
double getAverage(double* scores, int num)
{
double avg;
double total = 0.0;
for (int count = 0; count < num; count++)
{
total += scores[count];
}
avg = total / num;
return avg;
}
double gethighest(double* scores, int num)
{
double highnum = 0.0;
for (int i = 0; i < num; i++)
{
if (scores[i] > highnum)
highnum = scores[i];
}
return highnum;
}
double getlowest(double* scores, int num)
{
double lowestnum = 100;
for (int i = 0; i < num; i++)
{
if (scores[i] < lowestnum)
lowestnum = scores[i];
}
return lowestnum;
}
void arrayAscending(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ((array[index]) < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void arrayAscending(double *[], int);
应该void arrayAscending(double *, int);
你正在排序的是一个双精度型数组,而不是一个指向双精度型的指针数组。
,
double *minElem;
稍后使用,不会为其分配任何内存。同样,你可能只需要
double minElem;
而不是指针
如果您不需要使用指针,则使用std::vector
和标准库中的算法,如std::sort
。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- c++获得最高分和使用指针