This is homework and I am using a getfile that is just a 5 row 4 column list of numbers. I can get this program to compile and when I go to run it, I get my cout text output but without my numbers and the program crashes. I am sure it is something to do with the way I call my functions. But I am so lost now. Any help is appreciated.

#include<iostream>
#include<fstream>

using namespace std;

const int ROWS = 5, COLS = 4;

//function prototypes
void Bubble_Sort(int,int,int[][COLS]);
void Show_Array(int,int,int[][COLS]);

int a[ROWS][COLS];

int main(int argc, int *argv[])
{
	int r,c,row,col;
	int a[ROWS][COLS];
    //read the data file
	ifstream inFile("data.dat");

	//fill the array
	cout<<"\nData Before Sorting:" <<endl;
	inFile >>row >>col;
 	for(r=0; r<row; r++)
 		{
 		for (c=0; c <col; r++)
			{
			cout<< a[r][c] << " ";
			}
		}

	//call the functions

 	//sort the array
 	Bubble_Sort(ROWS,COLS,a);

 	//display the sorted array
	cout<<"\nData After Exchange:" <<endl;
 	Show_Array(ROWS,COLS,a);

	return 0;
}//end of main
void Bubble_Sort(int ROWS,int COLS,int a[][4])
{      	
 	int* p = (int*)a;
 	int r,c,n(ROWS*COLS);
	for(r=0; r<(n-1); r++)
	{
            for(c=r+1; c<n; c++)
			{
 			if(p[r] > p[c])
 				{
 					int hold = p[r];
 					p[r] = p[c];
 					p[c] = hold;
 				}
 			}
	}
}//end sort
void Show_Array(int ROWS,int COLS,int a[][4])
{
 	int r,c;
 	for(r=0; r<ROWS; r++)
 	{
 		for(c=0; c<COLS; c++)
 		{
 			Show_Array(ROWS,COLS,a);
 		}
 		Show_Array(ROWS,COLS,a);
 	}
}//end show sorted
//end of file

Looks to me like you only read in 2 numbers from your text file. Then you go ahead and start printing out un initialised memory space....

Chris

Looks to me like you only read in 2 numbers from your text file. Then you go ahead and start printing out un initialised memory space....

Chris

How can you tell that it is only reading in two lines? I am not arguing that that may be what is occuring, but I am not sure where the program is telling it to do that. The sort is supposed to keep going till it runs out of rows.

I have probably been staring at it too long at this point

inFile >>row >>col;

This is the only line that reads data in from the file, its not looped or anything. This reads 1 integer, then skips whitespace and reads another. The fact that you don't even store them into the array at anypoint, I originally missed!

Also, you should do some error checking to make sure that the file is actually open, using

if(!inFile.good()){
  cerr<<"Unable to open file.";
  exit(0);
}

" inFile >>row >>col;" this is the only line that reads data in from the file, its not looped or anything. This reads 1 integer, then skips whitespace and reads another. The fact that you don't even store them into the array at anypoint, I originally missed!

Ok, I can understand that. It doesn't use the const int ROWS=5, COLS=4; from the top?

Also, you should do some error checking to make sure that the file is actually open, using "if(!inFile.good()){
cerr<<"Unable to open file."; exit(0);}"

ok, I should have had that. No, it wasn't reading it. However, once I got it to read it shows this crap.

Data Before Sorting:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 0 -
858993460 3078184 1581128 2147332096 3080192 3078252 3078192 2147332096 19799012
10 0 0 0 2147332096 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 Press any key to continue . . .

It is just frustrating that every time I fix one error, 5 more crop up. LOL

Thanks for the assist, if you can think of more... I am all ears.

Mikhala

Well you are not storing them into the array such as, a[x][y]. And your show function shouldn't be recoursive like that...it doesn't even print things out, it just cause lots of problems.

Chris

Well, looks like its time to pick it apart section by section and see what runs and what doesn't. Thanks for the advice.

Mikhala

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.