-2

I need a simple program to accept string vector and sort using bubble sort and display the result. While executing the program automatically terminates.

#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>

using namespace std;

vector<string> v2;
void Bsortchar(vector <string> &ch)
{
    int i, j;
    string temp[1][200];
    int charLength = ch.size();
    cout<<ch.size();
    for(i = 0; i <= charLength; i++)
    {
        for (j=0; j < (charLength -1); j++)
        {
            if (ch[j+1] < ch[j])
            {
                temp[1][200] = ch[j];
                ch[j] = ch[j+1];
                ch[j+1]= temp[1][200];
            }
        }
    }
    return;  
}

int main()
{
    int charSize;
    //**********************************************************
    cout<<"Enter the Size of the char Vector"<<endl;
    cin>>charSize;
    cout<<"Enter the char Vector"<<endl;
    string c;
    for(int i=0;i<=charSize;i++)
    {
        cout<<i<<":";
        getline(cin,c);
        v2.push_back(c);
    }
    //************************************************************
    Bsortchar(v2);
    //***********************************************************
    cout<<endl<<"The sorted character vector array is : "<<endl;
    for(int i=0;i<=charSize;i++)
    {
        cout<<v2[i]<<" ";
    }
    //***********************************************************
    return 0;
}

Need to accept string from the user and display the results after performing a bubble sort.

barbsan
  • 3,238
  • 11
  • 18
  • 27
  • 3
    Please indent your code. And what exactly is your problem? – user6556709 Jun 04 '19 at 09:31
  • 1
    you are forced to post some minimum of text for a reason, please dont cheat by spamming, instead try to describe what is the problem. Every program eventually terminates, so what is wrong about that? Please try to be more precise – 463035818_is_not_a_number Jun 04 '19 at 09:49
  • Sounds like a school exercise. You might want to solve that on your own if you don't want to fall behind. Learn to debug, like by printing the current state within each iteration and what it compares. Other than that, if it is not a school exercise, use `std::sort`. – Aziuth Jun 04 '19 at 09:56
  • 1
    In any case, `temp[1][200] = ch[j];` is wrong, the coordinate (1, 200) is out of bounds for an array of size 1x200. Not sure why you use an array anyway here. – Aziuth Jun 04 '19 at 09:56
  • Why do you need 200 * 1 temporary strings, when you only ever use 1 at a time? – Caleth Jun 04 '19 at 10:18
  • while calling the bubble sort function the program terminates. – user3673294 Jun 04 '19 at 10:21
  • You have a large number of variables declared in scopes they don't need to be in. It's preferable to declare things at exactly the point you need them, and no earlier. E.g. `v2` is only used in `main` – Caleth Jun 04 '19 at 10:36
  • 2
    Unrelated, [get rid of this steaming pile: `#include`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). It's neither standard compliant, nor necessary. Know what you're including and what you're not. – WhozCraig Jun 04 '19 at 10:40
  • `i` must be strictly less than `charLength` – Damien Jun 04 '19 at 10:40
  • @Damien no, `i` never indexes `ch`. It's safe, but pointless, to have it above `charLength -1` – Caleth Jun 04 '19 at 10:49
  • @Caleth My comment was too short a little bit. I did not want to mention a UB, but useless operations. In practice this programme is far to be optimised, independently of the choice of bubble sort. But it is not the question here effectively – Damien Jun 04 '19 at 11:00

1 Answers1

1

You have undefined behaviour temp[1][200] is off the ends of both parts of this (gratuitous) 2d array.

You don't need an array here, just a single temporary.

auto temp = ch[j];
ch[j] = ch[j+1];
ch[j+1]= temp;

Or, preferably, you can use an existing function

std::swap(ch[j], ch[j+i]);
Caleth
  • 35,377
  • 2
  • 31
  • 53