Hi,
I have an array of strings and there about 20 lines that array. I'm wondering if there is a way I can check the first character of each line and see if one line has a character that comes before the latter line, I can switch them so that in total, I can sort the lines and the entire array alphabetically. Is there a way to do that, to say look at the first character in the line and if it comes later in the alphabet, then switch lines?
I know you can do bubble sort and selection sort but I've literally spent 7 hours today doing that and nothing, I MEAN NOTHING, is working and I asked some classmates and none of their suggestions help.
Yes, unfortunately we can't use the sort function and as I said before, all the different sort programs like bubble sort, insertion sort, and selection sort, are literally just not working
Why shouldn't a bubble sort work?
Instead of comparing the whole line you just compare the first char and if necessary swap the lines.
Why don't you show us your code ?
I suggest breaking your assignment to several parts:
* A bool-returning function which tests if c-string a < b (or >).
* A function which swaps two c-strings
* A sort function which uses the above implemented functions.
Okay, so this is the function I'm working on. Currently, with this code, I can get it to go down to like the fifth lines of the array(there is a total of 20) but it stops right there. I think it's because from the first line to the fifth, everything is already in alphabetical order but then at the sixth line, the sentence starts with an a and needs to be pushed to the first line which this doesn't do.
Also, I'm sure someone will ask why I've done i/j < size +1 in my for loops and the answer is because it doesn't work any other way. Even if I put i/j < max or i/j < max -1 or i/j < size -i - 1, nothing works.
As for where my cout statement is, I also tried putting it outside of that loop and made it its own separate for loop as well but the output is still the same. When I did make that loop though, i still had to put i < size +1 cause nothing else worked.
As for the sort function, I think I might be allowed to use it (I asked my instructor) but everytime I have, it gives me a weird jumble of letters. And honestly, I'd like to keep that sort function as a last resort.
And the question about why the bubble sort wasn't working, I have no clue. I made that bubble sort for another assignment and it did work then, so I know there wasn't a mistake.
If anyone can help me, I'd really appreciate it.
void sortRes(string res[], string cat[], int &s) {
ifstream in;
in.open("myassign.txt");
int const max = 20;
string temp2, temp1, temp;
int i;
if (in.is_open())
{
while (getline(in, temp1))
{
if ((temp1 != "yes") && (temp1 != "no") && (temp1 != "maybe"))
{
for (i = 0; i < size + 1; i++)
{
res[i] = temp1;
PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
It would be helpful if yo post all your code that can be compiled so everyone knows what you are working with. And include the input file so everyone is using the same information and not having to guess.
intconst max = 20;. Generally when you use something like this the variable name is in capital letters. This reminds you that it is defined as a constant.