简体   繁体   中英

Binary search on string array and extra feature

This is a program I have been working on to learn C++. It inputs a file into two different arrays (string, 2d double) than sorts, averages, adds averages to ends of 2d double array than outputs. I'm trying to implement a search (preferably binary) that finds the name matching to searchItem variable than gets the scores associated with that name to output. I have an idea of how to implement grabbing the scores. Store into a variable the element position the name was found and use that to reference the row where the right scores will be. Having trouble with the binary search as in it gives no compile errors and isn't doing anything. From what I understand the algorithm I'm using is the same as others I've seen working. Also am I on the right track with the idea I had or is there a better method? It seemed to me to be the simplest way but my experience with this language is limited. Thank you in advance.

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;  

const int ROWS = 11;
const int COLS = 4;

void ArrayManip (string x[], double y[][COLS], int length);

int main() 
{
    ifstream inFile;

    inFile.open("bowlers2.txt");

    const int SIZE = 10;    

    double scores[ROWS][COLS];
    string names[SIZE];
    string mystring;

    if (!inFile)
    {
        cout << "Can not open the input file"
             << " This program will end."<< "\n";
        return 1;

    }

    for(int r = 0; r < SIZE; r++)
    {
        getline(inFile, names[r]);
        for(int c = 0; c < (COLS - 1); c++)
        {
            getline(inFile, mystring);
            scores[r][c] = atoi(mystring.c_str());
        }
    }

    inFile.close();

    ArrayManip (names, scores, SIZE);

    return 0;
}

void ArrayManip (string x[], double y[][COLS], int LENGTH)
{
    int i,j,k,
        first = 0,
        last = LENGTH - 1,
        middle,
        position = -1;    
    bool found = false;
    string searchItem = "keith";
    string sValue;
    double dValue;
    double dArray[COLS - 1];
    double sum = 0;
    double avr;

    //Sort names and scores
    for(i = 1; i < LENGTH; i++)
    {
        sValue = x[i];
        for (k = 0; k < (COLS - 1); k++)
        {
           dArray[k] = y[i][k];
        }
        for(j = i - 1; j >= 0 && x[j] > sValue; j--)
        {
            x[j + 1] = x[j];
            for (k = 0; k < (COLS - 1); k++)
            {
               y[j + 1][k] = y[j][k];
            }
        }
        x[j + 1] = sValue;
        for (k = 0; k < (COLS - 1); k++)
        {
           y[j + 1][k] = dArray[k];
        }
    }

    for(k = 0; k < LENGTH; k++)
        for(i = 1; i < (COLS - 1); i++)
        {
            dValue = y[k][i];
            for(j = i - 1; j >= 0 && y[k][j] > dValue; j--)
            {
                y[k][j + 1] = y[k][j]; 
            }
            y[k][j + 1] = dValue;
        }

    //average and store rows
    for(i=0;i<LENGTH;i++)
    {
        for(j = 0; j < (COLS - 1); j++)
        {
            sum += y[i][j];             
        }
        avr = sum/(COLS -1);
        y[i][j++] = avr;
        sum = 0;        
    }

    //average and store columns
    for(j=0;j<(COLS - 1);j++)
    {
        for(i=0;i<LENGTH;i++)
        {
            sum += y[i][j];
        }
        avr = sum/LENGTH;
        y[i++][j] = avr;
        sum = 0;
    }

    cout << fixed << showpoint << setprecision(2);
    cout << "Averages for all Players:\n";
    for(i=0;i<(COLS - 1);i++)
    {
        cout << "Score " << (i+1) << " average: " << y[LENGTH][i] << "\n";
    }

    cout << "\n";

    for(i=0;i<LENGTH;i++)
    {
        cout << "player " << (i+1) << ": " << x[i] << "\n";
        for(j=0;j<COLS;j++)
        {
            if(j < (COLS - 1))
            {
            cout << "score " << (j+1) << ": " << y[i][j] << "\n";            
            }
            else
            cout << "player average: " << y[i][j] << "\n\n";            
        }
    }    

    while(!found && first <= last)
    {
        middle = (first + last) / 2;
        if(x[middle] == searchItem)
        {
            found = true;
            cout << x[middle] << "\n";
        }
        else if (x[middle] > searchItem)
            last = middle - 1;
        else
            first = middle + 1;
    }
}

Input file: bowlers2.txt

Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
keith hallmark
222
300
180
anna hallmark
222
111
211
roxie hallmark
100
100
2

When you compare two std::string objects using the equality operator, it compares the whole strings. If you're looking for "keith" and compare it to the string "keith hallmark" then it won't match.

You can use std::string::find to find a substring in a string.

Like

if (x[middle].find(searchItem) != std::string::npos)
{
    // Sub-string found
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM