对用户生成的数组进行排序具有垃圾数据
Sorting an array that has been user generated has garbage data
>我有一个用户最多输入50个整数的程序。然后,它允许用户搜索整数,显示排序后它在数组中的位置,以及所有元素的平均值。
我卡住的地方是,用户生成的输入是...错。当我打印出数组时,它是不正确的值。
代码:
//inputSearch.cpp
//This program will allow the user to enter up to 50 integers.
//It will then allow the user to search the array they just created.
#include<iostream>
using namespace std;
const int SIZE = 50;
void displayArray(int[],int); // protoype to print out array
void sortArray(int [], int); // prototype to sort array function
int binarySearch(int[], int, int); // prototype to binary search function
int getAverage(int[], int); // prototype to get average of array
int main()
{
int arrayList[SIZE]; //array...
int searchFor=0; // we're gonna search for this
int found;
int pos = 0;
cout << "Please enter up to 50 integers (or -99 to stop): n" ;
cin >> arrayList[pos]; // puts integers in array
while (arrayList[pos] != -99) // -99 to stop otherwise we'll be here all day
{
pos++;
cout << "nPlease input up to 50 integers (or -99 to stop)n";
cin >> arrayList[pos];
}
int value=pos; //how many elements are entered
sortArray(arrayList, SIZE); // Sorts array
cout << "Enter an integer to search for: ";
cin >> searchFor;
found = binarySearch(arrayList, SIZE, searchFor);
if (found == -1)
cout << "The value " << searchFor << " is not in the list" << endl;
else
{
cout << "The value " << searchFor << " is in position number "
<< found + 1 << " of the list" << endl;
}
cout << "nn";
displayArray(arrayList,value);
cout << "nn";
cout << "The mean of the values of the array is: " << getAverage(arrayList, value);
cout << "nn";
cout << "nn The array has " << value << " inputted elementsnn";
return 0;
}
void sortArray(int array[], int elems)
{
bool swap;
int temp;
int bottom = elems - 1;
do
{
swap = false;
for (int count = 0; count < bottom; count++)
{
if (array[count] > array[count+1])
{ // the next three lines do a swap
temp = array[count];
array[count] = array[count+1];
array[count+1] = temp;
swap = true; // indicates that a swap occurred
}
}
bottom--; // bottom is decremented by 1 since each pass through
// the array adds one more value that is set in order
}while(swap != false);
// loop repeats until a pass through the array with
// no swaps occurs
}
int binarySearch(int array[],int numElems,int value) //function heading
{
int first = 0; // First element of list
int last = numElems - 1; // last element of the list
int middle; // variable containing the current
// middle value of the list
while (first <= last)
{
middle = (first + last) / 2;
if (array[middle] == value)
return middle; // if value is in the middle, we are done
else if (array[middle]>value)
last = middle - 1; // toss out the second remaining half of
// the array and search the first
else
first = middle + 1; // toss out the first remaining half of
// the array and search the second
}
return -1; // indicates that value is not in the array
}
void displayArray(int array[], int elems)
{
for (int count = 0; count < elems; count++)
{
cout << array[count] << " " << endl;
}
}
int getAverage (int array[], int size)
{
int sum = 0; // holds the sum of all the numbers
for (int pos = 0; pos < size; pos++)
sum = sum + array[pos]; // sum now holds sum plus next element
return (sum / size); //returns the average
}
输入 100 和 100 得到的输出:
请输入最多 50 个整数(或 -99 表示停止):
100
请输入最多 50 个整数(或 -99 停止)
100
请输入最多 50 个整数(或 -99 停止)
-99
输入要搜索的整数:100
值 100 位于列表的第 36 位
-99
-1
数组值的平均值为:-50
数组有 2 个输入元素
任何帮助,不胜感激。
由于
以下问题,您看到奇怪的行为:
- 您尚未初始化数组
arrayList
。 - 您正在对整个数组进行排序,而不是仅对用户输入的条目数进行排序。
- 您正在使用整个数组进行搜索。
通过改变
sortArray(arrayList, SIZE); // Sorts array
自
sortArray(arrayList, value); // Sorts array
和
found = binarySearch(arrayList, SIZE, searchFor);
自
found = binarySearch(arrayList, value, searchFor);
您将能够解决您的问题。
您可以使用以下命令初始化数组:
int arrayList[SIZE] = {0};
一旦您修复了sortArray
和binarySearch
的使用,这不会改变您的程序的工作方式。
相关文章:
- 如何对点云数据进行排序
- 如何使用 QSortFilterProxyModel::sort 对 Qlist 中的数据进行排序
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 使用选择排序对数组数据结构进行排序,但它不起作用
- 为什么我的代码没有对数组中第二个索引上的数据进行排序?
- 使用 QSortFilterProxyModel 对 Qml ListView 的数据进行排序
- 读取 C++ 中的 txt 数据,展开为行或列并排序
- C++ 从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
- 存储在 std::map/std::set 中,与在存储所有数据后对向量进行排序
- 尝试创建排序的动态列表(数据结构)
- 固定大小的容器,其中元素被排序,并可以为C 中的数据提供原始指针
- 排序算法,使用模板按内部数据对对象进行排序
- 对多个数据成员进行排序
- 递归定义数组中的数据对齐和排序
- 如何修复输出显示,以便显示正确排序的数据
- 是否有具有对数时间插入、删除和查找(带距离)的排序数据结构
- 为什么大多数STL算法都需要将排序数据作为输入
- 用于插入大部分排序数据的数据结构,这些数据将保持排序顺序
- 可更新的DAWG库或来自未排序数据的DAWG构造
- 实现在单链表中添加和排序数据的函数(addsort)