| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Search/Sort Program Errors
I was given the assignment to create a program to perform a sequential and binary search, if binary search is selected, quicksort, bubblesort, insertion sort or selection sort can be chosen to be performed on an array entered by the user. the user also selects what type of array they will enter, character, double, or integer. This is my first program, so bear with me. I am getting an error with the double arrays. the program seems to be truncating the numbers after the decimal. everything else seems to be working ok. I get warnings that say 'possible loss of data, converting from double to int'. I think that if someone can help me to pin down where these errors are coming from, I would be able to make the double arrays work properly. here is the program, please copy and run it throughyour compiler and see what you think:
#include "stdafx.h" #include <iostream> #include <string> using namespace std; const int MaxArray=150; void callScreen(); int grabScreenInt(int iarray[]); int grabScreenDoub(double darray[]); int grabScreenChar(char carray[]); void doIntSequential(int iarray[], const int arrsize); void doDoubSequential(double darray[], const int arrsize); void doCharSequential(char carray[], const int arrsize); void doIntBinary(int iarray[], const int arrsize); void doDoubBinary(double darray[], const int arrsize); void doCharBinary(char carray[], const int arrsize); void bubbleSortInt(int iarray[], int n); void bubbleSortDoub(double darray[], int n); void bubbleSortChar(char carray[], int n); int binarySearchInt(const int iarray[], int first, int last, int value); double binarySearchDoub(const double darray[], int first, int last, int value); char binarySearchChar(const char carray[], int first, int last, int value); void insertionInt(int iarray[], int arrsize); void insertionDoub(double darray[], int arrsize); void insertionChar(char carray[], int arrsize); void quickSortInt(int iarray[], int arrsize); void q_sortInt(int iarray[], int left, int right); void quickSortDoub(double darray[], int arrsize); void q_sortDoub(double darray[], int left, int right); void quickSortChar(char carray[], int arrsize); void q_sortChar(char carray[], int left, int right); void selectionSortInt(int iarray[], int arrsize); void selectionSortDouble(double darray[], int arrsize); void selectionSortChar(char carray[], int arrsize); int main() { int flag = 0; int option; //HERE, THE USER IS PROMPTED FOR //THE SOURCE OF THE ARRAY AND IS //SENT TO THE APPROPRIATE FUNCTION do { cout<<"To begin creating your own array, press 1: " << endl; cin>>option; } while (option != 1); { callScreen(); } return 0; } //THIS FUNCTION PROMPTS THE USER //FOR THE TYPE OF ARRAY THAT THEY //WOULD LIKE TO ENTER, THEN SENDS //THEM TO THE APPROPRIATE FUNCTION void callScreen() { int option, iarray[MaxArray], arrsize, arrtype; double darray[MaxArray]; char carray[MaxArray]; do { cout<<"Integer(1), double(2) or character(3) array?" << endl; cin>>option; } while (option < 1 && option > 3); switch (option){ case 1: arrsize = grabScreenInt(iarray); break; case 2: arrsize = grabScreenDoub(darray); break; default: arrsize = grabScreenChar(carray); } arrtype = option; do { cout<<"Please select Binary(1) or Sequential Search(2)?" << endl; cin>>option; } while (option != 1 && option != 2); switch (option) { case 1: if (arrtype == 1) doIntBinary(iarray, arrsize); else if (arrtype == 2) doDoubBinary(darray, arrsize); else doCharBinary(carray, arrsize); break; case 2: if (arrtype == 1) doIntSequential(iarray, arrsize); else if (arrtype == 2) doDoubSequential(darray, arrsize); else doCharSequential(carray, arrsize); } } //THIS FUNCTION IS TO //CATCH THE INTEGER ARRAY //ENTERED BY THE USER int grabScreenInt(int iarray[]) { int arrsize = 0; cout<<"Type in your array. Seperate items with spaces:" << endl; // input, until maxsize is reached in the array or newline or end of line is found do { cin >> iarray[arrsize]; arrsize++; } while (cin.peek() != '\n' && arrsize < MaxArray); return arrsize; } |
|
#2
|
|||
|
|||
|
//THIS FUNCTION IS TO
//CATCH THE DOUBLE ARRAY //ENTERED BY THE USER int grabScreenDoub(double darray[]) { int arrsize = 0; cout<<"Type in your array. Seperate items with spaces:" << endl; // input, until maxsiz is reached in the array or newline or end of line is found do { cin >> darray[arrsize]; arrsize++; } while (cin.peek() != '\n' && arrsize < MaxArray); return arrsize; } //THIS FUNCTION IS TO //CATCH THE CHARACTER ARRAY //ENTERED BY THE USER int grabScreenChar(char carray[]) { int arrsize = 0; cout<<"Type in your array. Seperate items with spaces:" << endl; // input, until maxsize is reached in the array or newline or end of line is found do { cin >> carray[arrsize]; arrsize++; } while (cin.peek() != '\n' && arrsize < MaxArray); return arrsize; } //THIS IS A FUNTION FOR //PERFORMING A SEQUENTIAL //SEARCH ON INTEGER ARRAYS void doIntSequential(int iarray[], const int arrsize) {int flag = 0; int target = 0; cout<<"Please enter item to be found: "; cin>>target; for(int cntr = 0; cntr < arrsize; cntr++) { if(iarray[cntr] == target) { cntr=cntr+1; cout << "Target found in array index " << cntr << "." << endl; flag += 1; } } // Test to see if target was found. if(flag < 1) { cout << "Target not found." << endl; } } //THIS IS A FUNCTION FOR //PERFORMING A SEQUENTIAL //SEARCH ON DOUBLE ARRAYS void doDoubSequential(double darray[], const int arrsize) {int flag = 0; double target = 0; cout<<"Please enter item to be found: "; cin>>target; for(int cntr = 0; cntr < arrsize; cntr++) { if(darray[cntr] == target) { cntr=cntr+1; cout << "Target found in array index " << cntr << "." << endl; flag += 1; } } // Test to see if target was found. if(flag < 1) { cout << "Target not found." << endl; } } //THIS IS A FUNCTION FOR //PERFORMING A SEQUENTIAL //SEARCH ON CHARACTER ARRAYS void doCharSequential(char carray[], const int arrsize) { char target = 0; int flag = 0; cout<<"Please enter item to be found: "; cin>>target; for(int cntr = 0; cntr < arrsize; cntr++) { if(carray[cntr] == target) { cntr=cntr+1; cout << "Target found in array index " << cntr << "." << endl; flag += 1; } } // Test to see if target was found. if(flag < 1) { cout << "Target not found." << endl; } } //THIS IS A FUNCTION FOR //PERFORMING A BINARY //SEARCH ON INTEGER ARRAYS void doIntBinary(int iarray[], const int arrsize) { int option, find, found; do { cout<<"What type of sort would you like to do?" << endl; cout<<"Quicksort(1), Bubble Sort(2), Insertion(3), Selection(4)" << endl; cin>>option; } while (option < 1 && option > 5); switch (option) { case 1: quickSortInt(iarray,arrsize); break; case 2: bubbleSortInt(iarray,arrsize); break; case 3: insertionInt(iarray,arrsize); break; default: selectionSortInt(iarray, arrsize); break; } cout<<"Enter target to be found" << endl; cin>>find; found = binarySearchInt(iarray,0,arrsize,find); cout << "Target found at: " << found+1 << endl; } //THIS IS A FUNCTION FOR //PERFORMING A BINARY //SEARCH ON DOUBLE ARRAYS void doDoubBinary(double darray[], const int arrsize) { int option, found; double find; do { cout<<"What type of sort would you like to do?" << endl; cout<<"Quicksort(1), Bubble Sort(2), Insertion(3), Selection(4)" << endl; cin>>option; } while (option < 1 && option > 5); switch (option) { case 1: quickSortDoub(darray,arrsize); break; case 2: bubbleSortDoub(darray,arrsize); break; case 3: insertionDoub(darray,arrsize); break; default: selectionSortDouble(darray,arrsize); break; } cout<<"Enter target to be found" << endl; cin>>find; found = binarySearchDoub(darray,0,arrsize,find); cout << "Target found at: " << found+1 << endl; } //THIS IS A FUNCTION FOR //PERFORMING A BINARY //SEARCH ON CHARACTER ARRAYS void doCharBinary(char carray[], const int arrsize) { int option, found; char find; do { cout<<"What type of sort would you like to do?" << endl; cout<<"Quicksort(1), Bubble Sort(2), Insertion(3), Selection(4)" << endl; cin>>option; } while (option < 1 && option > 5); switch (option) { case 1: quickSortChar(carray,arrsize); break; case 2: bubbleSortChar(carray,arrsize); break; case 3: insertionChar(carray,arrsize); break; default: selectionSortChar(carray,arrsize); break; } cout<<"Enter target to be found" << endl; cin>>find; found = binarySearchChar(carray,0,arrsize,find); cout << "Target found at: " << found+1 << endl; } //THIS IS THE CODE FOR //BUBBLE SORTING INTEGER VALUES void bubbleSortInt(int iarray[], int arrsize) { bool sorted = false; for (int pass=1; (pass < arrsize) && !sorted; ++pass) { sorted = true; for (int index = 0; index < arrsize-pass; ++index) { int next = index + 1; if (iarray[index] > iarray[next]) { int swap = iarray[next]; iarray[next] = iarray[index]; iarray[index] = swap; sorted = false; } } } } //THIS IS THE FUNCTION FOR //BUBBLE SORTING DOUBLE VALUES void bubbleSortDoub(double darray[], int arrsize) { bool sorted = false; for (int pass=1; (pass < arrsize) && !sorted; ++pass) { sorted = true; for (int index = 0; index < arrsize-pass; ++index) { int next = index + 1; if (darray[index] > darray[next]) { double swap = darray[next]; darray[next] = darray[index]; darray[index] = swap; sorted = false; } } } } |
|
#3
|
|||
|
|||
|
//THIS IS THE FUNCTION FOR
//BUBBLE SORTING CHARACTERS void bubbleSortChar(char carray[], int arrsize) { bool sorted = false; for (int pass=1; (pass < arrsize) && !sorted; ++pass) { sorted = true; for (int index = 0; index < arrsize-pass; ++index) { int next = index + 1; if (carray[index] > carray[next]) { char swap = carray[next]; carray[next] = carray[index]; carray[index] = swap; sorted = false; } } } } //THIS IS THE FUNCTION FOR THE //BINARY SEARCH OF INTEGER VALUES int binarySearchInt(const int iarray[], int first, int last, int value) { int index; if (first > last) index = -1; else { int mid = (first + last)/2; if (value == iarray[mid])index = mid; else if (value < iarray[mid]) index = binarySearchInt(iarray, first, mid-1, value); else index = binarySearchInt(iarray, mid+1, last, value); } return index; } //THIS IS THE FUNCTION FOR THE //BINARY SEARCH OF DOUBLE VALUES double binarySearchDoub(const double darray[], int first, int last, int value) { int index; if (first > last) index = -1; else { int mid = (first + last)/2; if (value = darray[mid])index = mid; else if (value < darray[mid]) index = binarySearchDoub(darray, first, mid-1, value); else index = binarySearchDoub(darray, mid+1, last, value); } return index; } //THIS IS THE FUNCTION FOR THE //BINARY SEARCH OF CHARACTERS char binarySearchChar(const char carray[], int first, int last, int value) { int index; if (first > last) index = -1; else { int mid = (first + last)/2; if (value == carray[mid])index = mid; else if (value < carray[mid]) index = binarySearchChar(carray, first, mid-1, value); else index = binarySearchChar(carray, mid+1, last, value); } return index; } //THIS IS THE FUNCTION FOR //THE INSERTION SORT OF INTEGERS void insertionInt(int iarray[], int arrsize) { int a[30]; a[0] = iarray[0]; for (int i = 1; i < arrsize; i++) { int temp = iarray[i]; int j = i - 1; while (( a[j] > temp) && (j>=0)) { a[j+1] = a[j]; j--; } a[j+1] = temp; } for (int k = 0; k < arrsize; k++) { iarray[k] = a[k]; } k=k+1; } //THIS IS THE FUNCTION FOR //THE INSERTION SORT OF DOUBLE VALUES void insertionDoub(double darray[], int arrsize) { int a[30]; a[0] = darray[0]; for (int i = 1; i < arrsize; i++) { double temp = darray[i]; int j = i - 1; while (( a[j] > temp) && (j>=0)) { a[j+1] = a[j]; j--; } a[j+1] = temp; } for (int k = 0; k < arrsize; k++) { darray[k] = a[k]; } } //THIS IS THE FUNCTION FOR //THE INSERTION SORT OF CHARACTERS void insertionChar(char carray[], int arrsize) { int a[30]; a[0] = carray[0]; for (int i = 1; i < arrsize; i++) { char temp = carray[i]; int j = i - 1; while (( a[j] > temp) && (j>=0)) { a[j+1] = a[j]; j--; } a[j+1] = temp; } for (int k = 0; k < arrsize; k++) { carray[k] = a[k]; } } //THIS IS THE FUNCTION FOR //THE QUICK SORT OF INTEGERS void quickSortInt(int iarray[], int arrsize) { q_sortInt(iarray, 0, arrsize - 1); } void q_sortInt(int iarray[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = iarray[left]; while (left < right) { while ((iarray[right] >= pivot) && (left < right)) right--; if (left != right) { iarray[left] = iarray[right]; left++; } while ((iarray[left] <= pivot) && (left < right)) left++; if (left != right) { iarray[right] = iarray[left]; right--; } } iarray[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sortInt(iarray, left, pivot-1); if (right > pivot) q_sortInt(iarray, pivot+1, right); } //THIS IS THE FUNCTION FOR //THE QUICK SORT OF DOUBLE VALUES void quickSortDoub(double darray[], int arrsize) { q_sortDoub(darray, 0, arrsize - 1); } void q_sortDoub(double darray[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = darray[left]; while (left < right) { while ((darray[right] >= pivot) && (left < right)) right--; if (left != right) { darray[left] = darray[right]; left++; } while ((darray[left] <= pivot) && (left < right)) left++; if (left != right) { darray[right] = darray[left]; right--; } } darray[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sortDoub(darray, left, pivot-1); if (right > pivot) q_sortDoub(darray, pivot+1, right); } //THIS IS THE FUNCTION FOR //THE QUICK SORT OF CHARACTERS void quickSortChar(char carray[], int arrsize) { q_sortChar(carray, 0, arrsize - 1); } void q_sortChar(char carray[], int left, int right) { int pivot, l_hold, r_hold; l_hold = left; r_hold = right; pivot = carray[left]; while (left < right) { while ((carray[right] >= pivot) && (left < right)) right--; if (left != right) { carray[left] = carray[right]; left++; } while ((carray[left] <= pivot) && (left < right)) left++; if (left != right) { carray[right] = carray[left]; right--; } } carray[left] = pivot; pivot = left; left = l_hold; right = r_hold; if (left < pivot) q_sortChar(carray, left, pivot-1); if (right > pivot) q_sortChar(carray, pivot+1, right); } //THIS FUNCTION PERFORMS //A SELECTION SORT ON //INTEGERS void selectionSortInt(int iarray[], int arrsize) { // Local Definitions int current; int smallest; int holdData; int x; for (current = 0; current < arrsize; current++) { smallest = current; for (x = current + 1; x <= arrsize; x++) if (iarray[x] < iarray[smallest]) smallest = x-1; // Smallest selected: exchange with current holdData = iarray[current]; iarray[current] = iarray[smallest]; iarray[smallest] = holdData; } return; } //THIS FUNCTION PERFORMS //A SELECTION SORT ON //DOUBLE VALUES void selectionSortDouble(double darray[], int arrsize) { // Local Definitions int current; int smallest; int holdData; int x; for (current = 0; current < arrsize; current++) { smallest = current; for (x = current + 1; x <= arrsize; x++) if (darray[x] < darray[smallest]) smallest = x; // Smallest selected: exchange with current holdData = darray[current]; darray[current] = darray[smallest]; darray[smallest] = holdData; } return; } //THIS FUNCTION PERFORMS //A SELECTION SORT ON //CHARACTERS void selectionSortChar(char carray[], int arrsize) { // Local Definitions int current; int smallest; int holdData; int x; for (current = 0; current < arrsize; current++) { smallest = current; for (x = current + 1; x <= arrsize; x++) if (carray[x] < carray[smallest]) smallest = x; // Smallest selected: exchange with current holdData = carray[current]; carray[current] = carray[smallest]; carray[smallest] = holdData; } return; } |
|
#4
|
|||
|
|||
|
No help? anyone?
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Search/Sort Program Errors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|