c++ - Sorting an array that has been user generated has garbage data -
i have program user inputs 50 integers. allows user search integer, shows in array once sorted, , mean of elements.
where i'm stuck is, user generated input is...wrong. when print out array, it's incorrect values.
the code:
//inputsearch.cpp //this program allow user enter 50 integers. //it allow user search array created. #include<iostream> using namespace std; const int size = 50; void displayarray(int[],int); // protoype print out array void sortarray(int [], int); // prototype sort array function int binarysearch(int[], int, int); // prototype binary search function int getaverage(int[], int); // prototype average of array int main() { int arraylist[size]; //array... int searchfor=0; // we're gonna search int found; int pos = 0; cout << "please enter 50 integers (or -99 stop): \n" ; cin >> arraylist[pos]; // puts integers in array while (arraylist[pos] != -99) // -99 stop otherwise we'll here day { pos++; cout << "\nplease input 50 integers (or -99 stop)\n"; cin >> arraylist[pos]; } int value=pos; //how many elements entered sortarray(arraylist, size); // sorts array cout << "enter integer search for: "; cin >> searchfor; found = binarysearch(arraylist, size, searchfor); if (found == -1) cout << "the value " << searchfor << " not in list" << endl; else { cout << "the value " << searchfor << " in position number " << found + 1 << " of list" << endl; } cout << "\n\n"; displayarray(arraylist,value); cout << "\n\n"; cout << "the mean of values of array is: " << getaverage(arraylist, value); cout << "\n\n"; cout << "\n\n array has " << value << " inputted elements\n\n"; return 0; } void sortarray(int array[], int elems) { bool swap; int temp; int bottom = elems - 1; { swap = false; (int count = 0; count < bottom; count++) { if (array[count] > array[count+1]) { // next 3 lines swap temp = array[count]; array[count] = array[count+1]; array[count+1] = temp; swap = true; // indicates swap occurred } } bottom--; // bottom decremented 1 since each pass through // array adds 1 more value set in order }while(swap != false); // loop repeats until pass through array // 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 list int middle; // variable containing current // middle value of list while (first <= last) { middle = (first + last) / 2; if (array[middle] == value) return middle; // if value in middle, done else if (array[middle]>value) last = middle - 1; // toss out second remaining half of // array , search first else first = middle + 1; // toss out first remaining half of // array , search second } return -1; // indicates value not in array } void displayarray(int array[], int elems) { (int count = 0; count < elems; count++) { cout << array[count] << " " << endl; } } int getaverage (int array[], int size) { int sum = 0; // holds sum of numbers (int pos = 0; pos < size; pos++) sum = sum + array[pos]; // sum holds sum plus next element return (sum / size); //returns average } the ouput entering 100 , 100:
please enter 50 integers (or -99 stop):
100
please input 50 integers (or -99 stop)
100
please input 50 integers (or -99 stop)
-99
enter integer search for: 100
value 100 in position number 36 of list
-99
-1
the mean of values of array is: -50
the array has 2 input elements
any appreciated.
you seeing strange behavior due following problems:
- you haven't initialized array
arraylist. - you sorting entire array instead of sorting number of entries user entered.
- you using entire array search.
by changing
sortarray(arraylist, size); // sorts array to
sortarray(arraylist, value); // sorts array and
found = binarysearch(arraylist, size, searchfor); to
found = binarysearch(arraylist, value, searchfor); you able solve problems.
you can use initialize array using:
int arraylist[size] = {0}; that won't change how program works once fix use of sortarray , binarysearch.
Comments
Post a Comment