【问题标题】:Sorting an array that has been user generated has garbage data对用户生成的数组进行排序有垃圾数据
【发布时间】:2015-11-20 07:09:42
【问题描述】:

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


感谢任何帮助。

【问题讨论】:

  • 建议重新考虑您的输入程序。考虑这种情况:Please enter up to 50 integers (or -99 to stop): 1 2 3 4 5 -99 可以,但是输出不是你想要的。
  • 不使用 std::vector、std::sort 和 std::binary_search 有什么原因吗?

标签: c++ arrays sorting cin


【解决方案1】:

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

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

【讨论】:

  • binarySearch 与 sortArray 有同样的问题,并且搜索整个列表,而不仅仅是使用的部分。请改用found = binarySearch(arrayList, value, searchFor); 。考虑给value 起一个更好的名称以提高可读性。
  • 谢谢!我还移动了 main 中的部分,该部分表示向 displayArray 函数输入了多少元素以保持并发。我还将 value 重命名为 numOfElements 以提高可读性和更有意义。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 2010-11-09
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 2020-11-17
  • 2013-01-13
  • 1970-01-01
相关资源
最近更新 更多