Afr0
Afr0

Reputation: 41

Arrange all the words in a list in alphabetical order in C++

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>

using namespace std;

void alpha_sorting(vector<string>& a);
void swap_function(vector<string>& x,int& l);
string alpha_least(vector<string>& list,int& idx);
int min_word_index(string& min,vector<string>& listed);

int main(){
    ifstream infile;
    infile.open("words.txt");
    if(!infile.is_open()){
        cout << "Could not open the input file" << endl;
        exit(EXIT_FAILURE);
    }
    vector<string> words;
    string temp;
    while(infile >> temp){
        words.push_back(temp);
    }
    sort_words_alphabetically(words);
    cout << "Those words in alphabetical order are " << endl;

    return 0;
}

void alpha_sorting(vector<string>& a){
    int index = 0;
    while(index < a.size()){
        swap_min_to_top(a, index);
        index++;
    }
    for(int i = 0;i < a.size();i++){
        cout << a[i] << endl;
    }
}

void swap_function(vector<string>& x,int& l){
    string temporary;
    temporary = x[l];
    wrdx[loc] = minimum_word(x, l);
    int min_idx = min_word_index(x[l], x);
    x[min_idx] = temporary;
}

string alpha_least(vector<string>& list,int& idx){
    string temp = list[idx];
    for(int i = idx;i < list.size();i++){
        if(list[i] < temp){
            temp = list[i];
        }
    }
    return temp;
}

int min_word_index(string& min,vector<string>& listed){
    for(int i = 0;i < listed.size();i++){
        if(min == listed[i]){
            return i;
        }
    }
    return -1;
}

Hi all,

In short, the assignment requires us to read words from a file and arrange them all in alphabetical order and display them to the screen.

The assignment specifically prohibits the use of c++ functions like sort etc. and expects us to create our own algorithm to arrange this vector.

My algorithm finds the minimum (the one that comes in the alphabet) string (using the '<' operator) in the list and then places it at the start of the vector, updates the index by 1 and repeats the process till the list is ordered. My only problem is that as others have informed me, using vectors filled with strings is always messy, my code doesn't display the correct answer, (as if the vectors aren't being updated or are undergoing any operations for that matter).

Can someone please find what is wrong with my code?

Upvotes: 0

Views: 1469

Answers (2)

user5412774
user5412774

Reputation: 1

With a classic bubble sort you must go on. Thanks to Wikipedia. Please give the answers rights to user Christophe BoueeCinq . [email protected]

 #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <vector>
    #include <string>
    #include <stdlib.h>
    #include <conio.h>
    using namespace std;

    int flag=0;
    string tmp="";
    int index=0;
    int k=0;

    int main()
    {
        ifstream infile;
        infile.open("words.txt");
        if(!infile.is_open())
        {
            cout << "Could not open the input file" << endl;
            exit(EXIT_FAILURE);
        }
        vector<string> words;
        string temp;
        while(infile >> temp)
        {
            words.push_back(temp);
            cout <<temp<<endl;
        }
        cout <<"-----"<<endl;

        int inx=0;
        int i=0;
        int change=0;
        int j=0;

        i =words.size(); // longueur(t)
        change = 1;
        while ((i>0) && (change==1))
        {
            change = 0;
            for (j=1; j<words.size()-1; j++) //        pour j allant de 1 à i-1 pas 1 faire
            {
                if  (words[j] > words[j + 1])
                {
                    tmp = words[j];
                    words[j] = words[j+1];
                    words[j+1] = tmp;
                    change = 1;
                }
            } //       fin pour
            i = i - 1;
            // fin tant que
        }

        _getch();


      for (j=0; j<words.size(); j++)
      cout <<"xx "<<words[j]<<endl;
    }

Upvotes: -1

theAlias
theAlias

Reputation: 396

After a quick look here's one issue, In swap_min_to_top you do wrdx[loc] = minimum_word(wrdx, loc); AND THEN int min_idx = min_word_index(wrdx[loc], wrdx); This will make your min_idx always equal to loc (because you just assigned it)

You may want min_word_index to process from 1+idx, instead of zero

Upvotes: 2

Related Questions