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

时间:2015-04-24 20:07:49

标签: c++ arrays sorting cin

我有一个程序,用户输入最多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 个答案:

答案 0 :(得分:1)

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

  1. 您尚未初始化数组arrayList
  2. 您正在对整个数组进行排序,而不是仅对用户输入的条目数进行排序。
  3. 您正在使用整个数组进行搜索。
  4. 通过更改

    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后,这不会改变您的计划的工作方式。