Pergunta

So the problem is that I'm taking a sample file for input asking for a user account number. Now, the output is:

What is the filename of account numbers? sample.txt
What is the account number you are looking for? 5552012
-858993460
4520125
5658845
7895122
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
The account number you were looking for is: 1
Press any key to continue . . .

The numbers that are output underneath: "What is the account number?" are due to a 'for' loop for debugging purposes. The problem with the output as well is that the top number (-858993460) isn't a number that exists inside the sample.txt file. The number that should be there is 5658845 instead. I'm guessing the negative number is the smallest int possible though.

This part is the code that I'm working with. It seems like the bubble sort algorithm isn't working quite right. But, as instructed, we are to follow the book example which I did exactly. I've checked the algorithm functions several times and found no errors but I could be wrong with my assessment. This issue leads to a larger issue which prevents the correct account number from being found in the sorted array, which in it's current state returns 1, which isn't a number that 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 the program. This is where all the 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 is the filename of account numbers? ";
    cin >> fileName;

    inputFile.open(fileName);

    if (inputFile.is_open()){//makes sure the file is able to be read, if not then requests user to try again
        cout << "What is the account number you are looking for? ";
        cin >> accountNumSearched;
        while (inputFile >> accounts[count]){
            count++;//populates the array
        }
        inputFile.close();//closes the file stream

        sortAcctNums(accounts, arraySize);//sorts the account numbers
        location = findAcctNum(accounts, arraySize, accountNumSearched);//assigns the value of the fundAcctNum function to location
        if (location == -1){
            cout << "The account number could not be found" << endl;
            exit;
        }
        else
            cout << "The account number you were looking for is: " << location << endl;
    }
    else
        cout << "Error opening file. Please try again. " << endl;

    return 0;
}

//this function sorts the account numbers with a bubble sort algorithm
void sortAcctNums(int accounts[], int arraySize){

    bool swap;
    int temp;

    do{
        swap = false;
        for (int i = 0; i < (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 is only for testing purposes and should be removed during final build
    for (int i = 0; i < arraySize; i++){
        cout << accounts[i] << endl;

    }
}

//This function searches the sorted array for the specified account number with a 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
Foi útil?

Solução 2

The correct program is:

#include <stdafx.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

//prototypes
void sortAcctNums(int accounts[], size_t count);
int findAcctNum(int accounts[], int numElems, int accountNumSearched);


//main part of the program. This is where all the 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 is the filename of account numbers? ";
    cin >> fileName;

    inputFile.open(fileName);

    if (inputFile.is_open()){//makes sure the file is able to be read, if not then requests user to try again
        cout << "What is the account number you are looking for? ";
        cin >> accountNumSearched;
        while (inputFile >> accounts[count]){
            count++;//populates the array
        }
        inputFile.close();//closes the file stream

        sortAcctNums(accounts, sizeof(accounts)/sizeof(*accounts));//sorts the account numbers
        location = findAcctNum(accounts, count, accountNumSearched);//assigns the value of the fundAcctNum function to location



        if (location == -1){
            cout << "The account number could not be found" << endl;
            exit;
        }
        else
            cout << "The account number you were looking for is: " << accounts[location] << endl
    }
    else
        cout << "Error opening file. Please try again. " << endl;

    return 0;
}

//this function sorts the account numbers with a bubble sort algorithm
void sortAcctNums(int accounts[], size_t count){

    bool swap = true;
    int temp;

    while (count-- && swap){
        swap = false;
        for (size_t i = 0; i < count; ++i){
            if (accounts[i] > accounts[i + 1]){
                std::iter_swap(accounts + i, accounts + i + 1);
                swap = true;
            }
        }
    }
}

//This function searches the sorted array for the specified account number with a Binary search algorithm
int 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

Outras dicas

Your bubble sort is not at all correct. You have:

do{
    swap = false;
    for (int i = 0; i < (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 a bubble sort. You'll need nested for loops for a bubble sort. Go back to your example and make sure you're implementing it exactly. A typical bubble sort would look similar to this this:

swap = true;
for (int i = 0; i < (arraySize - 1) && swap; ++i)
{
    swap = false;
    for (int j = 0; j < (arraySize - i - 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 that bogus number in your array is because you're comparing the current item (accounts[i]) with accounts[arraySize+1]. In this case, you're comparing against accounts[20]. Since there are only 18 items in the array, you're comparing against some random value that's stored in memory off the end of the array. The negative number is not the smallest possible int.

Bubble sort should look like this:

for (int i = 0 ; i < (arraySize - 1); i++) {
    for (int j = 0 ; j < arraySize - i - 1; j++) {
        if (array[j] > array[j+1]) {
            int swap   = array[j];
            array[j]   = array[j+1];
            array[j+1] = swap;
        }
    }
}

Try that and see whether you're still getting bad results.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top