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

Sorting an array that has been user generated has garbage data

本文关键字:排序 数据 用户 数组      更新时间:2023-10-16

>我有一个用户最多输入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 个输入元素


任何帮助,不胜感激。

由于

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

  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的使用,这不会改变您的程序的工作方式。