C++动态分配数组,Bubblesort+Finding Average
C++ Dynamically Allocate an Array, Bubblesort + Finding Average
我创建了bubblesort,我想我也有一个平均值。不确定。但我需要把它实现到我的主要功能中,我不知道如何实现。现在有点卡住了。任何帮助都会很棒。谢谢
编写一个程序,动态分配一个足够大的数组保存用户定义数量的测试分数。
输入所有分数后,数组应传递给函数(您创建的)作为一个指针,按升序对其进行排序。此函数可以命名为sortArray,并将采用两个参数:(双*anArray,int大小)。如果数组被排序,否则为false(例如,如果大小<=0)
应创建另一个计算平均分数的函数数组中项目的。此函数将接受两个参数(double*anyArray,int size),并以double形式返回平均值。
程序应显示分数和平均值的排序数组带有适当标题。
尽可能使用指针表示法而不是数组表示法。
#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);
int main()
{
double* anArray;
double total = 0.0;
double average;
int scores;
int count;
cout << "How many test scores are you entering?: ";
cin >> scores;
anArray = new double[scores];
cout << "Enter test scores: ";
for (count = 0; count < scores; count++)
{
cout << "Test Score " << count + 1 << ": ";
cin >> anArray[count];
}
system("pause");
return 0;
}
bool sortArray(double* anArray, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (anArray[j] > anArray[j + 1])
{
int temp = anArray[j];
anArray[j] = anArray[j + 1];
anArray[j + 1] = temp;
}
}
}
}
double averageArray(double* anyArray, int size)
{
double count = 0.0;
for (int i = 0; i < size; i++)
{
count += anyArray[i];
}
return (count / size);
}
您在sortarray函数中使用了bool,它永远不会返回任何值,所以将其设为void,并且您在sort array中使用了int temp,它必须是double。我为您使用此代码修复了它。顺便说一下,您没有调用函数,并且在排序后忘记打印数组的值。
#include <iostream>
#include <iomanip>
using namespace std;
bool sortArray(double* anArray, int size);
double averageArray(double* anyArray, int size);
int main()
{
double* anArray;
double total = 0.0;
double average;
int scores;
int count;
cout << "How many test scores are you entering?: ";
cin >> scores;
anArray = new double[scores];
cout << "Enter test scores: ";
for (count = 0; count < scores; count++)
{
cout << "Test Score " << count + 1 << ": ";
cin >> anArray[count];
}
sortArray(anArray, scores);
for (count = 0; count < scores; count++)
{
cout << anArray[count] << "n";
}
cout << averageArray(anArray, scores);
system("pause");
return 0;
}
bool sortArray(double* anArray, int size)
{
bool sort = false;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (anArray[j] > anArray[j + 1])
{
sort = true;
double temp = anArray[j];
anArray[j] = anArray[j + 1];
anArray[j + 1] = temp;
}
}
}
return sort;
}
double averageArray(double* anyArray, int size)
{
double count = 0.0;
for (int i = 0; i < size; i++)
{
count += anyArray[i];
}
return (count / (double) size);
}
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 错误"Could not find Boost"(缺少:上下文标头)
- /usr/bin/ld: cannot find -lc++
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 使用 find 解析文件会给出不同文件的奇怪结果
- 从 BubbleSort* 类型的右值初始化 'AssortedSorter&' 类型的非常量引用无效"
- 在 ifcondition al中 find() C++ STL 中的 == a.end() 有什么用?
- Cannot find -lglad
- "Cannot find -l<directory>"错误 - CMake
- 为什么在向量上使用 std::find() 时会出现错误?
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++ find() 在存储为变量时返回不同的值
- set::find 查找不存在的元素
- 使用 std::find 时没有匹配的函数调用错误
- 在先前调用 string::find 后添加内存分配和内存集会导致它返回 npos.为什么?
- Windows cannot find MinGW
- find() stl in vector in c++