Help please 2d array

Hi i need to write program that sort number in ascending order with 2 D array from inputfie to an outpute file i m real behind i tried this code but it does show any thing this assignment is due tomorrow please help

//This program is sorting intergers from inpute file in ascending order


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main ()
{
//Declare the variables

ifstream infile;
ofstream outfile;
int const Row = 10 ;
int const Column = 10;

int MyArray[Row][Column];
int i,j;

int swap ;
int end = 100;
int temp = 100;

infile.open("P6_F12_data.txt");
outfile.open("out.txt");

if(infile.fail())

{
cout<<"input file did not open please check it\n";
return 1;
}
if(outfile.fail())

{
cout<<"input file did not open please check it\n";
return 1;
}
for(int counter = 1;counter < temp;counter--)

{
for(i=0;i<Row;i++)
{
for(j=0;j<Column; j++)
{
infile>>MyArray[i][j];

if (MyArray[i+1][j+1] < MyArray[i][j])
{
swap = MyArray[i+1][j+1];
MyArray[i][j] = MyArray[i+1][j+1];
MyArray[i+1][j+1] = swap;
}

}
}
end--;
}

for(i=0;i<Row;i++)
{
for(int j=0;j<Column;j++)

{
outfile <<MyArray[i][j] <<" " ;
}


}

infile.close();
outfile.close();

return 0;



}
Last edited on
What do you mean by "it does show any thing" ?

Also, please enclose your code in code tags, so that we can read it more easily.
i mean when i run the program i don t get any result in the output file
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
//This program is sorting intergers from inpute file in ascending order 


#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
int main ()
{
														//Declare the variables
	
	ifstream infile;																												        
	ofstream outfile;																													    
	int const Row = 10 ;
    int const Column = 10;																													
																																																											
	int MyArray[Row][Column];
	int i,j;
	
	int swap ;																														   
	int end = 100;																					            
	int temp = 100;
	 
    infile.open("P6_F12_data.txt");																										                                                              
	outfile.open("ELBOUCHIKHI.txt");

    if(infile.fail())

    {
        cout<<"input file did not open please check it\n";
         return 1;
	}																						
		if(outfile.fail())

    {
        cout<<"input file did not open please check it\n";
         return 1;
	}					
for(int counter = 1;counter < temp;counter--)

	{
		for(i=0;i<Row;i++)
	 {
		 for(j=0;j<Column; j++)
		 {
			 infile>>MyArray[i][j];
			 
		 if (MyArray[i+1][j+1] < MyArray[i][j])
		 {		 
		 swap = MyArray[i+1][j+1];
		MyArray[i][j] = MyArray[i+1][j+1];
	   MyArray[i+1][j+1] = swap;
	 }
		 
		}	 
		}
	 end--;
	}
		
	for(i=0;i<Row;i++)
	 {
		 for(int j=0;j<Column;j++)
	
		 {
			 outfile <<MyArray[i][j] <<" " ;
		 }

		
	}
	 
   infile.close();
   outfile.close();

   return 0;
	 
	
	
	}	
Does it hang? Does it crash? Do you get any error messages?

It looks to me as though your for loop at line 41 is incorrect. You initialise counter to 1, and then you subtract 1 from it each time through the loop. That means that it goes to 0 the next time, then -1 and so on.

This means that (counter < temp) will always be true, until counter reaches the largest possible negative number an int can hold, at which point it will become a huge positive number (I think...) and cause the loop to exit.

Surely this isn't your intended behaviour?
dude are you serious put it in your compiler yourself .... you will get the error messaages if it does.... for the record i don't think it does...
no i don t get any error and no result in the output file either
now when i initialized counter to 0 and increment it by 1 i get garbage numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(int counter = 0;counter < temp;counter++)

	{
		for(i=0;i<Row;i++)
	 {
		 for(j=0;j<Column; j++)
		 {
			 infile>>MyArray[i][j];
			 
		 if (MyArray[i+1][j+1] < MyArray[i][j])
		 {		 
		 swap = MyArray[i+1][j+1];
		MyArray[i][j] = MyArray[i+1][j+1];
	   MyArray[i+1][j+1] = swap;
	 }


please help
Last edited on
Is this the sort of program you were after?

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
	const int ROWS(10), COLS(10);
	int myarray[ROWS][COLS];

	ifstream ifs;
	ifs.open("P6_F12_data.txt");

	if (ifs.fail())
		cout << "failed to read input file" << endl;
	else
	{
		// read data into array:
		int row(0);
		string line;

		while (getline(ifs, line) && row < ROWS)
		{
			istringstream iss(line);
			int n, col(0);

			while (iss >> n && col < COLS)
				myarray[row][col++] = n;

			++row;
		}

		ifs.seekg(0, ios::beg);
		ifs.clear();
		ifs.close();

		// do sort:
		int* beg = reinterpret_cast<int*>(myarray);
		int* end = beg + ROWS * COLS;
		sort(beg, end);

		// write sorted data out to new file:
		ofstream ofs;
		ofs.open("out.txt");

		if (ofs.fail())
			cout << "failed to open out file" << endl;
		else
		{
			for (int r = 0; r < ROWS; ++r)
			{
				for (int c = 0; c < COLS; ++c)
					ofs << myarray[r][c];

				ofs << endl;
			}

			ofs.flush();
			ofs.close();
		}
	}

	return 0;
}


Let me know if that helps?

Andrew
thank you for your help
Topic archived. No new replies allowed.