0

I have coded thus far and I am not sure how to sort using the 2-dimensional array. Basically, one function is for sorting an array of strings, another function is for swapping two strings. Any help would be appreciated. (Also I am not allowed to use c++ 11 :/)

#include <iostream>
#include <string>
#include <algorithm>


using namespace std;

void input_name(string&);
void sort_names(string&);
void repeat_pro(int&);
void sortArray(string, int);
int main() {

    string b_list[100][2];                                              
    string name;
    int choice;
    int count=0;

    cout << "Welcome to the Business Sorting Program!" << endl;
    do{
        input_name(name);
        b_list[count][1] = name;      
        count++; 
        repeat_pro(choice);
        cout<<"\n \n Your Businesses are:"<<endl;
            for(int j=0; j<count; j++){
                cout<<b_list[j][1]<<endl;  
            }
        cout << "\n\n";
    }while(choice == 0);
    cout << "Thanks for using this program"<<endl;


    return 0;
}


void input_name(string &name){
    cout << "Enter in the name of the business: ";
    getline(cin, name);
}

void sort_names(string &name){

}

void repeat_pro(int &choice){
  cout << "Do you want to enter in more names: ";
  string answ;
  cin>>answ;
  cin.ignore(1000,'\n');
  if (answ == "YES" || answ == "Y" || answ == "yes" || answ == "y"){
      choice = 0;
  }
  else {
      choice = 1;
  }
  }
6
  • 1
    C++03 has std::sort. It also has std::vector. Better avoid raw arrays. Commented Sep 6, 2017 at 3:36
  • 1
    Why the 2d array? If you have a pair of items use a pair, or a struct to group them. Especially difficult to tell why since your example doesn't use both. Which one is the key to sort on? IMO struct is easier to manage than pair, but either is workable. Commented Sep 6, 2017 at 3:45
  • I have to use arrays since my instructor asked me to. I was thinking about using the second row of it to place them in order. But I am not sure how to do it with strings Commented Sep 6, 2017 at 4:19
  • If you don't need the 2d array there's plenty of answers here: stackoverflow.com/questions/2803071/c-sort-array-of-strings Commented Sep 6, 2017 at 5:04
  • I haven;t seen any b_list[x][0], are you even refering to your 1st column of your 2 dimensional array? Commented Sep 6, 2017 at 5:26

1 Answer 1

1

it is not clear to me from the description what problem the program really tried to solve. I'm assuming it's kind of like a two column spreadsheet, the second column is the name entered by the user(but what is in the first column?).

assume you need to keep the array in sorted order as the data goes in, just do a binary search (you can do a linear search for small dataset like 100 entries).

// we don't have lambda before C++11
struct comparator {
  bool operator () (const string (&x)[2], const string (&y)[2]) const {
    return x[1] < y[1];
  }
};

//... omitted

string data[100][2];
int count = 0;
while (count < 100) {
    // no rvalue, move, rvo, etc. before C++11
    string name;
    input_name(name);
    // no type deduction and lambda
    string (*position)[2] =
            std::lower_bound(&data[0], &data[count], name, comparator());
    int index = position - &data[0];
    // simulate an vector::insert operation, but for our array
    for (int i = count; i > index; --i) {
        // before we had move in C++, we would do swap with an empty element.
        // in this case, the entry at data[count] is default constructed
        std::swap(data[i][1], data[i-1][1]);
    }
    data[index][1] = name;
}
//... omitted

of course we can use a typedef to make it cleaner, but that's left to you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.