Hey everybody,

I am still pretty new to programming. I am not getting a compiler error here, but I can't figure out what the problem is. I get the message

"an unhandled win32 exception occured [1576]"

It seems like some type of overflow in the insertion_sort function. I am still trying to figure it out, but if anyone sees the problem, please let me know.

#include "utility.h"


void insertion_sort(short array1[10][10000], int size);

int main()
{
	const short column = 10;
	const short row = 10000;
	const string fileName = "Lab9a.txt";
	int matrix_size = column*row;

	//vector< vector<short> > matrix;
	//short matrix[10][10000];
	short (*matrix) [row]= new short [column][row];


	// Open the data file
    ifstream fin(fileName.c_str());
    if (!fin.is_open()) 
	{
        cout << endl << "Unable to open input file " << fileName << endl;
        return 1;
    }// End if (!fin.is_open())


	// Fill up matrix
	//int temp = 0;  //was being used for vector
	for(int t = 0; t < column ; t++)
	{
		for(int i = 0; i < row; i++)
		{
				fin >> matrix[t][i];

		}// End for(int i = 0; i < r; i++)
	}// End for(int t = 0; t < s ; t++)

	//insertion_sort(matrix);

	cout << matrix[9][9999] << endl;
	insertion_sort(matrix, matrix_size);    // Very slow. Error at end

	cout << matrix[0][0] << endl;
	cout << matrix[0][1] << endl;
	cout << matrix[0][2] << endl;
	cout << matrix[0][3] << endl;
	cout << matrix[0][4] << endl;


}



void insertion_sort(short array1[10][10000], int size)
/*
Post: The entries of the Sortable_list have been rearranged so that
      the keys in all the  entries are sorted into nondecreasing order.
*/
{
	int first_unsorted;    //  position of first unsorted entry
	int position;          //  searches sorted part of list
	short current;    //  holds the entry temporarily removed from list
	int count = size;

	for (int column = 0; column < 5; column++)
	{
		for(first_unsorted = 1; first_unsorted < count; first_unsorted++)
		{  
			if (array1[column][first_unsorted] < array1[column][first_unsorted - 1]) 
			{
				position = first_unsorted;
				current = array1[column][first_unsorted];         //  Pull unsorted entry out of the list.
				do 
				{                    //  Shift all entries until the proper position is found.
					array1[column][position] = array1[column][position - 1];
					position--;                           //  position is empty.
				} while (position > 0 && array1[column][position - 1] > current);
				array1[column][position] = current;
			}
		}
	}		
}

Recommended Answers

All 3 Replies

I take it that line 40 executes properly, but line 42 does not?

Right off the bat, I see a problem in line 69. size appears to be 100,000, count is assigned to equal size, and first_unsorted can equal anywhere up to 99,999, and that's used in line 69 as an array index. Using 99,999 in an array index where the highest valid array index is 9999 can only lead to problems.

I can't guarantee that's the problem, but it sure looks like one. Make sure your first array index is always less than 10 and your second is always less than 10,000.

Use your compiler's debugger and single step through the program to find out what the problem is. You can also put print statements around the program so that you can see the value of various variables.

line 65: for (int column = 0; column < 5; column++)

Why 5? why not column or 10? (make those two const ints global so that they can be used throughout the program.

Good catch, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.