Sort function causing crash.

EDIT:: Need some help with the sort function causing the program to crash. Updated code is below.

All other problems have been solved, just trying to get the sort to work now.

Full code:

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm> 
#include <string>

using namespace std;

void importTable(string tabledata, int** &table, int& rowCount, int& colCount);


//This function reads the table stored in the file at the specified location. The file is expected to have the following form 
//-The first line consists of a single integer, R, that indicates the number of rows in the table.
//-The second line consists of a single integer, C, that indicates the number of columns in the table.
//-The remainder of the file will contain the table values.The file consists of R more lines and each of those lines with have C integers separated by spaces, potentially more than one.
//-No two rows will share the same initial value(think of them as unique ids)

void sort(int** rowReferences, int rowCount, int colCount, int sortColumn);

//This function sorts the row references such that the values in column number sortColumn are in ascending order.
//The sorting is ascending and is performed using a select sort.This function assumes 0 ? sortColumn < colCount; that is, the column is an actual column in the table.

void showTable(int** table, int rowCount, int colCount);

//This function displays the table.There is no specific format for the output, just make the output look tidy - evenly spaced columns and such.
int main(){
	//int& rowNum;
	//int& colNum;
	//string tablestring;
	int rows = 0;
	int columns = 0;
	int ** my_table;
	int ** reference1;
	int sorting = 0;

	//importTable(string tabledata, int** &table, int& rowCount, int& colCount);
	cout << "Table;" << endl;
	importTable("v:\\Desktop\\tabledata.txt", my_table, rows, columns);
	importTable("tabledata.txt", reference1, rows, columns);
	sort(reference1, rows, columns, sorting);
	showTable(my_table, rows, columns);
	//cout << my_table[0][1] << " " << endl;

	//cout << &table;
	showTable;
	cout << endl;
	cout << "Program success" << endl;
	return 0;
}

void importTable(string tabledata, int** &table, int& rowCount, int& colCount){ //int** &table
	ifstream in;
	in.open(tabledata);
	in >> rowCount;
	in >> colCount;
	table = new int*[rowCount];
	for (int d = 0; d < rowCount; ++d){
		table[d] = new int[colCount];
		for (int e = 0; e < colCount; ++e){
			in >> table[d][e];
		}

	};
	in.close();
}

void sort(int** rowReferences, int rowCount, int colCount, int sortColumn){
	int startScan;
	int minIndexC;
	int minElemC;

	for (startScan = 0; (startScan < colCount - 1); ++startScan){
		minIndexC = startScan;
		minElemC = rowReferences[startScan][sortColumn];

		for (int indexC = startScan + 1; indexC < colCount; ++indexC)
		{
			if (rowReferences[indexC][sortColumn] < minElemC)
			{
				minElemC = rowReferences[indexC][sortColumn];
				minIndexC = indexC;
			}
		}
		rowReferences[minIndexC][sortColumn] = rowReferences[startScan][sortColumn];
		rowReferences[startScan][sortColumn] = minElemC;
	}

}

void showTable(int** my_table, int rowCount, int colCount){
	for (int count = 0; (count < rowCount); ++count)
	{
		for (int twocount = 0; (twocount < colCount); ++twocount)
		{
			cout << setw(4) << my_table[count][twocount] << " ";
		}
		cout << endl;
	}
}


The problem area is below. Running the debugger, it'll get to the sort function, but one it gets to the part of the code shown below, it loops until indexC = 4 and colCount = 6 (since my table for this example is 4x6) and then crashes.

1
2
3
4
5
6
7
8
9
 for (int indexC = startScan + 1; indexC < colCount; ++indexC)
		{
			if (rowReferences[indexC][sortColumn] < minElemC)
			{
				minElemC = rowReferences[indexC][sortColumn];
				minIndexC = indexC;
			}
		}
  


Seems to stop at the "if (rowReferences[indexC][sortColumn] < minElemC)" statement.
Last edited on
closed account (E0p9LyTq)
If I were writing the program I'd use one of the standard library containers, std::array<> is a new C++11 container, and use the std::sort or std::stable_sort algorithm.

http://www.cplusplus.com/reference/array/array/
http://www.cplusplus.com/reference/algorithm/sort/
http://www.cplusplus.com/reference/algorithm/stable_sort/

There are examples at the links.
I think I managed to figure out the sorting code now, but I'm still having a bit of problems with getting the table to display (I've edited the updated code into the OP).

The output looks like this for the current code:

>000F160
>Program Success

aka, eight digits of (seemingly random) 0-9 and A-F values and the throwaway line that's there to ensure the program is running.

EDIT:: Should I be trying to display the table by its individual values (cout << tablename[x][y], etc) instead of the entire table at once or is my general direction alright? I've chosen the latter since the table dimensions aren't necessarily the same for each file, but not sure if what I'm doing is right.
Last edited on
>000F160

Those random digits come from line 27. You're displaying the address of the importTable function. The importTable function is never getting called.


Alright, I think I'm seeing the problem now. I need something sort of like this?

1
2
3
4
5
6
7
8
        const int& rowNum = 10;
	const int& colNum = 10;
	string tablestring;
	int** &tablecall[][];



	cout << "Table" << importTable(tablestring, tablecall, rowNum, colNum)


I think? I'm trying to go off of the codes I've done before with this but I'm not really seeing how I'm supposed to get this one.
closed account (D80DSL3A)
A problem in the sort function. Lines 64, 65:
1
2
rowReferences[minIndexC] = rowReferences[startScan];
rowReferences[startScan] = &minElemC;

That's a nightmare! Do you see what that 2nd line does? The array pointed to gets lost and now some local variable in a function is pointed to. The 1st line is pretty destructive too. Either will cause a crash when the destructor is called.

I'm guessing those 2 lines should actually be:
1
2
3
// swap value at startscan with value found at minIndexC  = minElemC
rowReferences[minIndexC][sortColumn] = rowReferences[startScan][sortColumn];
rowReferences[startScan][sortColumn] = minElemC;
Last edited on
A problem in the sort function. Lines 64, 65:


Thanks for the heads up. I think I originally had
1
2
rowReferences[minIndexC] = rowReferences[startScan];
rowReferences[startScan] = minElemC;

And was getting an error trying to convert from int* to int, thus resulting in what you saw.
Alright, so I've got the importable and showTable to work correctly, but now I'm getting a problem with the sort function.

Running the debugger, it'll get to the sort function, but one it gets to the part of the code shown below, it loops until indexC = 4 and colCount = 6 (since my table for this example is 4x6) and then crashes.

1
2
3
4
5
6
7
8
for (int indexC = startScan + 1; indexC < colCount; ++indexC)
		{
			if (rowReferences[indexC][sortColumn] < minElemC)
			{
				minElemC = rowReferences[indexC][sortColumn];
				minIndexC = indexC;
			}
		}


Seems to stop at the "if (rowReferences[indexC][sortColumn] < minElemC)" statement.

Full code below:


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <fstream>
#include <iomanip>
#include <iostream>
#include <algorithm> 
#include <string>

using namespace std;

void importTable(string tabledata, int** &table, int& rowCount, int& colCount);


//This function reads the table stored in the file at the specified location. The file is expected to have the following form 
//-The first line consists of a single integer, R, that indicates the number of rows in the table.
//-The second line consists of a single integer, C, that indicates the number of columns in the table.
//-The remainder of the file will contain the table values.The file consists of R more lines and each of those lines with have C integers separated by spaces, potentially more than one.
//-No two rows will share the same initial value(think of them as unique ids)

void sort(int** rowReferences, int rowCount, int colCount, int sortColumn);

//This function sorts the row references such that the values in column number sortColumn are in ascending order.
//The sorting is ascending and is performed using a select sort.This function assumes 0 ? sortColumn < colCount; that is, the column is an actual column in the table.

void showTable(int** table, int rowCount, int colCount);

//This function displays the table.There is no specific format for the output, just make the output look tidy - evenly spaced columns and such.
int main(){
	//int& rowNum;
	//int& colNum;
	//string tablestring;
	int rows = 0;
	int columns = 0;
	int ** my_table;
	int ** reference1;
	int sorting = 0;

	//importTable(string tabledata, int** &table, int& rowCount, int& colCount);
	cout << "Table;" << endl;
	importTable("v:\\Desktop\\tabledata.txt", my_table, rows, columns);
	importTable("tabledata.txt", reference1, rows, columns);
	sort(reference1, rows, columns, sorting);
	showTable(my_table, rows, columns);
	//cout << my_table[0][1] << " " << endl;

	//cout << &table;
	showTable;
	cout << endl;
	cout << "Program success" << endl;
	return 0;
}

void importTable(string tabledata, int** &table, int& rowCount, int& colCount){ //int** &table
	ifstream in;
	in.open(tabledata);
	in >> rowCount;
	in >> colCount;
	table = new int*[rowCount];
	for (int d = 0; d < rowCount; ++d){
		table[d] = new int[colCount];
		for (int e = 0; e < colCount; ++e){
			in >> table[d][e];
		}

	};
	in.close();
}

void sort(int** rowReferences, int rowCount, int colCount, int sortColumn){
	int startScan;
	int minIndexC;
	int minElemC;

	for (startScan = 0; (startScan < colCount - 1); ++startScan){
		minIndexC = startScan;
		minElemC = rowReferences[startScan][sortColumn];

		for (int indexC = startScan + 1; indexC < colCount; ++indexC)
		{
			if (rowReferences[indexC][sortColumn] < minElemC)
			{
				minElemC = rowReferences[indexC][sortColumn];
				minIndexC = indexC;
			}
		}
		rowReferences[minIndexC][sortColumn] = rowReferences[startScan][sortColumn];
		rowReferences[startScan][sortColumn] = minElemC;
	}

}

void showTable(int** my_table, int rowCount, int colCount){
	for (int count = 0; (count < rowCount); ++count)
	{
		for (int twocount = 0; (twocount < colCount); ++twocount)
		{
			cout << setw(4) << my_table[count][twocount] << " ";
		}
		cout << endl;
	}
}

Topic archived. No new replies allowed.