gpt4 book ai didi

c++ - 对用户生成的数组进行排序有垃圾数据

转载 作者:行者123 更新时间:2023-11-28 06:18:06 25 4
gpt4 key购买 nike

我有一个程序,用户最多可以输入 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 << "\n\n";
displayArray(arrayList,value);

cout << "\n\n";
cout << "The mean of the values of the array is: " << getAverage(arrayList, value);
cout << "\n\n";
cout << "\n\n The array has " << value << " inputted elements\n\n";
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个输入元素


感谢任何帮助。

最佳答案

由于以下问题,您看到了奇怪的行为:

  1. 您还没有初始化数组arrayList
  2. 您正在对整个数组进行排序,而不是仅根据用户输入的条目数进行排序。
  3. 您正在使用整个数组进行搜索。

通过改变

sortArray(arrayList, SIZE);     // Sorts array

sortArray(arrayList, value);     // Sorts array

found = binarySearch(arrayList, SIZE, searchFor); 

found = binarySearch(arrayList, value, searchFor); 

您将能够解决您的问题。

您可以使用初始化数组:

int arrayList[SIZE] = {0};

一旦您修复了 sortArraybinarySearch 的使用,这不会改变您的程序的工作方式。

关于c++ - 对用户生成的数组进行排序有垃圾数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com