Can't bubble sort char 2d array, gets segmentation fault 11

So I get segmentation fault 11 if i try to use bubble sort on this 2d char array.
It should read a file that is comma-delimited like this:
Rico, Suave
Migos, Quavo
Johhny, Juliano
...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void bubbleSort(char names[][30], int rows);

int main()
{
  char names[100][30];

  ifstream readfile("names.txt");

  int i, j;

  for (i = 0; i < 100; i++)
  {
    for (j = 0; j < 30; j++)
    {
      readfile.get(names[i][j]);
    }
  }

  readfile.close();

  bubbleSort(names, 100);

  for (i = 0; i < 100; i++)
  {
    for (j = 0; j < 30; j++)
    {
      cout << names[i][j];
    }
  }

  return 0;
}

void bubbleSort(char names[][30], int s)
{
  int i, j;

  char temp[30];
  for (i = 0; i < rows; ++i)
  {
      for (j = i + 1; j < rows - 1; j++)
      {
          if (strcmp (names[i], names[j]) > 0)
          {
              strcpy (temp, names[j]);
              strcpy (names[j], names[i]);
              strcpy (names[i], temp);
          }
      }
  }
}
Probably because your strings are full of garbage, and the strings that do exist are not \0 terminated.


1
2
3
4
5
6
7
8
9
  int numRows = 0;
  for (i = 0; i < 100; i++)
  {
    if (readfile.getline(names[i],30) ) {
      numRows++;
    }
  }

  bubbleSort(names, numRows);

@salem c

Thanks a lot, I just made that change and added the variable for tracking number of names in the text file.

I dont know if the bubble sort is working properly though, i get a very weird output.

my text file is 10 names:
sev, side
ogz, ygz
mac, crip
migos, quavo
higuain, gonzalo
messi, lionel
ronaldo, cristiano
pogba, paul
fernandes, bruno
jota, diogo


the output i get is:
1
2
3
4
5
6
ernandes, bruno
4��ernandes, brunoP4��g�S�des, bruno�
4���des, brunooT��des, brunoo�noo�des, brunoo�des, brunoo�des, brunoo��S�des, brunoo.��des, brunoo`
                               ����des, brunoo�des, brunoo���]��des, brunoo4��yg��des, brunoo�des, brunoo�
                                                                                                            �des, brunoo�����ǡ��des, brunoo��
                                                                                                                                             �des, brunoo(4��8�des, brunooP4���des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo1	���'>;�des, brunoo9g�xa�des, brunoo8�g�xa�des, brunoo`�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo�des, brunoo,S�des, brunoo4��ndes, brunoofernandes, brunoohiguain, gonzaloojota, diogolianomac, cripnelianomessi, lionelianomigos, quavotianoogz, ygzulstianopogba, paulstiano;P4��ronaldo, cristiano(4��sev, side�4����deristiano4��� 
Last edited on
you are not running the code you gave.
error: 'rows' was not declared in this scope

you may be running an old executable from before you tried to fix it?
also you are not including all the includes for the tools you used (cstdio, cstring, fstream, ?) which some compilers will gloss over, mine is mad about it.

this works. see if you can spot the changes and understand why I made them. Some of them are to debug, to ensure it is doing what is wanted at various steps. You were REALLY close.
its polishing a poop (not your fault, bubble sort just stinks) but if you changed it to char* you could swap just a pointer, instead of copying each string a billion times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#include <cstdio>
#include <bitset>
#include <cfloat>
#include <cstring>
#include <cstdio>
#include <fstream>
using namespace std;


void bubbleSort(char names[][30], int rows);

int main()
{
  char names[100][30]{};

  ifstream readfile("names.txt");

  int i{}, j{1};

  while (readfile.getline(names[i],30) )
  {
	 cout << names[i++] << endl; 	  
	 j++;
  }
  cout << "----------\n";

  readfile.close();


  bubbleSort(names, j);

   i = 0;
   while(names[i][0])
   {
	  cout << names[i++] << endl;	  
   }
  return 0;
}

void bubbleSort(char names[][30], int rows)
{
  int i, j;

  char temp[30];
  for (i = 0; i < rows; ++i)
  {
      for (j = i + 1; j < rows - 1; j++)
      {		  
          if (strcmp (names[i], names[j]) > 0)
          {
              strcpy (temp, names[j]);
              strcpy (names[j], names[i]);
              strcpy (names[i], temp);
          }
      }
  }
}
Last edited on
@jonnin thanks a lot for your reply.

I just tried running this but I get undeclared identifier for 'j', and expected ';' at end of declaration for 'char names[100][3]{}' and 'int i{}, j{1}'.
> I just tried running this but I get undeclared identifier for 'j', and expected ';' at end of declaration
You'll need a compiler that supports C++11

Eg
g++ -std=c++11 file.cpp
Why not take advantage of the <string> library instead of just #include'ing it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void display( string aNames[][2], int no_names )
{
    for(int i = 0; i < no_names; i++)
    {
        std::cout << i << '-' << aNames[i][0] << '-' << aNames[i][1] << '\n';
    }
    std::cout << '\n';
    
}

void bubbleSort( string aNames[][2], int no_names )
{
    std::string temp;
    
    for(int i = 0; i < no_names - 1; i++)
    {
        for(int j = 0; j < no_names - i - 1; j++)
        {
            if(aNames[j][1] > aNames[j+1][1])
            {
                for(int k = 0; k < 2; k++)
                {
                    temp = aNames[j][k];
                    aNames[j][k] = aNames[j+1][k];
                    aNames[j+1][k] = temp;
                }
            }
        }
    }
}

int main()
{
    const int LIMIT{100};
    string names[LIMIT][2];
    string fname, lname;
    std::string blank;
    
    ifstream readFile("0000.txt");
    
    if(!readFile)
        cout << "Failed\n";
    
    // READ NAMES FROM FILE
    int count{0};
    while(
          getline(readFile, fname, ',') &&
          getline(readFile, blank, ' ') &&
          getline(readFile, lname, '\n')
          )
    {
        names[count][0] = fname;
        names[count][1] = lname;
        count++;
    }
    display(names, count);
    
    // SORT NAMES
    bubbleSort(names, count);
    display(names, count);
    
    readFile.close();
    
    return 0;
}


0-sev-side
1-ogz-ygz
2-mac-crip
3-migos-quavo
4-higuain-gonzalo
5-messi-lionel
6-ronaldo-cristiano
7-pogba-paul
8-fernandes-bruno
9-jota-diogo

0-fernandes-bruno
1-mac-crip
2-ronaldo-cristiano
3-jota-diogo
4-higuain-gonzalo
5-messi-lionel
6-pogba-paul
7-migos-quavo
8-sev-side
9-ogz-ygz

Program ended with exit code: 0
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

void display(std::string aNames[][2], size_t no_names) {
	for (size_t i = 0; i < no_names; ++i)
		std::cout << aNames[i][0] << ", " << aNames[i][1] << '\n';

	std::cout << '\n';
}

void bubbleSort(std::string aNames[][2], size_t no_names)
{
	std::string temp;

	for (size_t i = 0; i < no_names - 1; ++i)
		for (size_t j = 0; j < no_names - i - 1; ++j)
			if (aNames[j][0] > aNames[j + 1][0]) {
				std::swap(aNames[j][1], aNames[j + 1][1]);
				std::swap(aNames[j][0], aNames[j + 1][0]);
			}
}

int main()
{
	constexpr size_t LIMIT {100};
	std::string names[LIMIT][2];

	std::ifstream readFile("names.txt");

	if (!readFile)
		return (std::cout << "Cannot open files\n"), 1;

	size_t count {};

	for (std::string spa; count < LIMIT && std::getline(readFile, names[count][0], ',') && std::getline(readFile, spa, ' ') && std::getline(readFile, names[count][1], '\n'); ++count);

	display(names, count);
	bubbleSort(names, count);
	display(names, count);
}



sev, side
ogz, ygz
mac, crip
migos, quavo
higuain, gonzalo
messi, lionel
ronaldo, cristiano
pogba, paul
fernandes, bruno
jota, diogo

fernandes, bruno
higuain, gonzalo
jota, diogo
mac, crip
messi, lionel
migos, quavo
ogz, ygz
pogba, paul
ronaldo, cristiano
sev, side

change to int i = 0; and int j= 1; type initialization then. You need a newer compiler, but you can do that once you have your homework done. as stated, this is c++ 2011 version, there is also 2017 and 2020. 2020 is still fairly new and not a lot of online code uses the new stuff from it.
Last edited on
Topic archived. No new replies allowed.