c++ - incorrect sorting and/or searching an array populated through an external file -
so problem i'm taking sample file input asking user account number. now, output is:
what filename of account numbers? sample.txt account number looking for? 5552012 -858993460 4520125 5658845 7895122 8451277 1302850 8080152 4562555 5552012 5050552 7825877 1250255 1005231 6545231 3852085 7576651 7881200 4581002 account number looking is: 1 press key continue . . .
the numbers output underneath: "what account number?" due 'for' loop debugging purposes. problem output top number (-858993460) isn't number exists inside sample.txt file. number should there 5658845 instead. i'm guessing negative number smallest int possible though.
this part code i'm working with. seems bubble sort algorithm isn't working quite right. but, instructed, follow book example did exactly. i've checked algorithm functions several times , found no errors wrong assessment. issue leads larger issue prevents correct account number being found in sorted array, in it's current state returns 1, isn't number exists.
#include <stdafx.h> #include <stdio.h> #include <fstream> #include <string> #include <algorithm> #include <iostream> using namespace std; //prototypes void sortacctnums(int accounts[], int arraysize); bool findacctnum(int accounts[], int numelems, int accountnumsearched); //main part of program. magic happens int main(){ string filename; int accountnumsearched, count = 0; const int arraysize = 18; int accounts[arraysize]; int location; fstream inputfile(filename, ios::in); cout << "what filename of account numbers? "; cin >> filename; inputfile.open(filename); if (inputfile.is_open()){//makes sure file able read, if not requests user try again cout << "what account number looking for? "; cin >> accountnumsearched; while (inputfile >> accounts[count]){ count++;//populates array } inputfile.close();//closes file stream sortacctnums(accounts, arraysize);//sorts account numbers location = findacctnum(accounts, arraysize, accountnumsearched);//assigns value of fundacctnum function location if (location == -1){ cout << "the account number not found" << endl; exit; } else cout << "the account number looking is: " << location << endl; } else cout << "error opening file. please try again. " << endl; return 0; } //this function sorts account numbers bubble sort algorithm void sortacctnums(int accounts[], int arraysize){ bool swap; int temp; do{ swap = false; (int = 0; < (arraysize - 1); i++){ if (accounts[i] > accounts[arraysize + 1]){ temp = accounts[i]; accounts[i] = accounts[arraysize + 1]; accounts[arraysize + 1] = temp; swap = true; } } } while (swap); //this loop testing purposes , should removed during final build (int = 0; < arraysize; i++){ cout << accounts[i] << endl; } } //this function searches sorted array specified account number binary sort algorithm bool findacctnum(int accounts[], int numelems, int accountnumsearched){ int first = 0, last = numelems - 1, middle, position = -1; bool found = false; while (!found && first <= last){ middle = (first + last) / 2; if (accounts[middle] == accountnumsearched){ found = true; position = middle; } else if (accounts[middle] > accountnumsearched) last = middle - 1; else first = middle + 1; } return position; }//end search
your bubble sort not @ correct. have:
do{ swap = false; (int = 0; < (arraysize - 1); i++){ if (accounts[i] > accounts[arraysize + 1]){ temp = accounts[i]; accounts[i] = accounts[arraysize + 1]; accounts[arraysize + 1] = temp; swap = true; } } } while (swap);
that's not bubble sort. you'll need nested for
loops bubble sort. go example , make sure you're implementing exactly. typical bubble sort similar this:
swap = true; (int = 0; < (arraysize - 1) && swap; ++i) { swap = false; (int j = 0; j < (arraysize - - 1); ++j) { if (array[j] > array[j+1]) { temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; swap = true; } } }
the reason you're getting bogus number in array because you're comparing current item (accounts[i]
) accounts[arraysize+1]
. in case, you're comparing against accounts[20]
. since there 18 items in array, you're comparing against random value that's stored in memory off end of array. negative number not smallest possible int.
Comments
Post a Comment