C++ in linux problem (long program)

This is a discussion on C++ in linux problem (long program) within the C++ Programming forums, part of the General Programming Boards category; Hi A program i made runs well in windows but when i run it in linux it doesn't work properly. ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    18

    C++ in linux problem (long program)

    Hi

    A program i made runs well in windows but when i run it in linux it doesn't work properly. Any ideas??

    Thanks

    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    #define MAXL 100
    #define MAXC 100
    int total = 0;
    
    int alcatroar(char mat[][MAXC],int l, int c);
    int procura_buraco_linha(char mat[][MAXC]);
    int procura_buraco_coluna(char mat[][MAXC]);
    bool procura_buraco(char mat[][MAXC]);
    bool VerificaAdjacencia_4(char mat[][MAXC]);
    bool VerificaAdjacencia_3(char mat[][MAXC]);
    bool VerificaAdjacencia_2(char mat[][MAXC]);
    bool VerificaAdjacencia_1(char mat[][MAXC]);
    int devolve_linha_adjacencia_4(char mat[][MAXC]);
    int devolve_coluna_adjacencia_4(char mat[][MAXC]);
    int devolve_linha_adjacencia_3(char mat[][MAXC]);
    int devolve_coluna_adjacencia_3(char mat[][MAXC]);
    int devolve_linha_adjacencia_2(char mat[][MAXC]);
    int devolve_coluna_adjacencia_2(char mat[][MAXC]);
    int devolve_linha_adjacencia_1(char mat[][MAXC]);
    int devolve_coluna_adjacencia_1(char mat[][MAXC]);
    
    int main(void)
    {
    	ifstream dados;
    	int i,j,k, numero_matrizes,linhas, colunas;
    	char linha[100], *p;
    	int t,l,c;
    	char mat[MAXL][MAXC];
    	
    	dados.open("dados.txt");
    	if(!dados)
    	cerr<<"\n\n\t\tErro: Nao Abriu ficheiro de texto."<<endl;
    	
    	dados.getline(linha,100);
    	p = strtok(linha,"\n"); 
    	numero_matrizes = atoi(p);
    	
    	for(i=0;i<numero_matrizes;i++)
    	{
    		dados.getline(linha,100);
    		p = strtok(linha," ");
    		linhas = atoi(p);
    		p = strtok(NULL,"\n");
    		colunas = atoi(p);
    		for(j=0;j<linhas;j++)
    		{
    			dados.getline(linha,100);
    			for(k=0;k<colunas;k++)
    				mat[j][k] = (char)linha[k];
    		}
    		if(VerificaAdjacencia_4(mat))
    		{
    			l = devolve_linha_adjacencia_4(mat);
    			c = devolve_coluna_adjacencia_4(mat);
    			t = alcatroar(mat,l,c);
    		}	
    		if(VerificaAdjacencia_3(mat))
    		{
    			l = devolve_linha_adjacencia_3(mat);
    			c = devolve_coluna_adjacencia_3(mat);
    			t = alcatroar(mat,l,c);
    		}
    		if(VerificaAdjacencia_2(mat))
    		{
    			l = devolve_linha_adjacencia_2(mat);
    			c = devolve_coluna_adjacencia_2(mat);
    			t = alcatroar(mat,l,c);
    		}	
    		if(VerificaAdjacencia_1(mat))
    		{
    			l = devolve_linha_adjacencia_1(mat);
    			c = devolve_coluna_adjacencia_1(mat);
    			t = alcatroar(mat,l,c);
    			
    		}	
    		while(procura_buraco(mat))
    		{
    			l = procura_buraco_linha(mat);
    			c = procura_buraco_coluna(mat);
    			t = alcatroar(mat,l,c);
    		}
    	cout<<"\nMatriz #"<<i+1<<":"<<" Movimentos="<<total;
    	total=0;
    	}
    	dados.close();
    	return 0;
    }
    
    int alcatroar(char mat[][MAXC],int l, int c)
    {
    	int t;
    	if(l<0 || l>MAXL || c<0 || c>MAXC)
    		return 0;
    	if(mat[l][c] == ' ')
    	{	
    		total++;
    		mat[l][c] = 'X';
    		mat[l-1][c]= 'X';
    		mat[l+1][c]= 'X';
    		mat[l][c-1]= 'X';
    		mat[l][c+1]= 'X';
    		if(VerificaAdjacencia_4(mat))
    		{
    			l = devolve_linha_adjacencia_4(mat);
    			c = devolve_coluna_adjacencia_4(mat);
    			t = alcatroar(mat,l,c);
    		}	
    		if(VerificaAdjacencia_3(mat))
    		{
    			l = devolve_linha_adjacencia_3(mat);
    			c = devolve_coluna_adjacencia_3(mat);
    			t = alcatroar(mat,l,c);
    		}
    		if(VerificaAdjacencia_2(mat))
    		{
    			l = devolve_linha_adjacencia_2(mat);
    			c = devolve_coluna_adjacencia_2(mat);
    			t = alcatroar(mat,l,c);
    		}	
    		if(VerificaAdjacencia_1(mat))
    		{
    			l = devolve_linha_adjacencia_1(mat);
    			c = devolve_coluna_adjacencia_1(mat);
    			t = alcatroar(mat,l,c);
    		}	
    		if(procura_buraco(mat))
    		{
    			l = procura_buraco_linha(mat);
    			c = procura_buraco_coluna(mat);
    			t = alcatroar(mat,l,c);
    		}
    	}
    	return total;
    }
    
    int procura_buraco_linha(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if(mat[i][j] == ' ')
    				return i;
    	return 0;
    }
    
    int procura_buraco_coluna(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if(mat[i][j] == ' ')
    				return j;
    	return 0;
    			
    }
    
    bool procura_buraco(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if(mat[i][j] == ' ')
    				return true;
    	return false;
    }
    
    bool VerificaAdjacencia_4(char mat[][MAXC])
    {
    	int l, c;
    	for(l=0;l<MAXL;l++)
    		for(c=0;c<MAXC;c++)
    			if((mat[l-1][c] == ' ') && (mat[l+1][c] == ' ') && (mat[l][c-1] == ' ') && (mat[l][c+1] == ' '))
    				return true;
    	return false;
    			
    }
    
    bool VerificaAdjacencia_3(char mat[][MAXC])
    {
    	int l, c;
    	for(l=0;l<MAXL;l++)
    		for(c=0;c<MAXC;c++)
    			if(((mat[l][c+1] == ' ') && (mat[l][c-1] == ' ') && (mat[l-1][c] == ' ')) || ((mat[l][c+1] == ' ') && (mat[l][c-1] == ' ') && (mat[l+1][c] == ' '))
    				|| ((mat[l-1][c] == ' ') && (mat[l+1][c] == ' ') && (mat[l][c-1] == ' ')) || ((mat[l-1][c] == ' ') && (mat[l+1][c] == ' ') && (mat[l][c+1] == ' ')))
    				return true;
    	return false;
    			
    }
    
    bool VerificaAdjacencia_2(char mat[][MAXC])
    {
    	int i, j;
    	for(i=0;i<MAXL;i++)
    		for(j=0;j<MAXC;j++)
    			if(((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i-1][j] == ' ')) ||
    				 ((mat[i-1][j] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i+1][j] == ' ') && (mat[i][j+1] == ' ')) 
    				 || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' ')))
    				return true;
    	return false;
    			
    }
    
    bool VerificaAdjacencia_1(char mat[][MAXC])
    {
    	int i, j;
    	for(i=0;i<MAXL;i++)
    		for(j=0;j<MAXC;j++)
    			if(((mat[i][j+1] == ' ') || (mat[i][j-1] == ' ') || (mat[i-1][j] == ' ') || ((mat[i+1][j] == ' '))))
    				return true;
    	return false;
    			
    }
    
    
    int devolve_linha_adjacencia_4(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    	{
    		for(j=0; j<MAXC; j++)
    		{
    			if((mat[i][j] == ' ') && (mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' '))
    				return i;
    		}
    	}
    	return -1;
    }
    
    int devolve_coluna_adjacencia_4(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    	{
    		for(j=0; j<MAXC; j++)
    		{
    			if((mat[i][j] == ' ') && (mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' '))
    				return j;
    		}
    	}
    	return -1;
    }
    
    int devolve_linha_adjacencia_3(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && (((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j-1] == ' ')) || ((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j+1] == ' '))
    				|| ((mat[i-1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i+1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' '))))
    				return i;
    	return -1;
    }
    
    int devolve_coluna_adjacencia_3(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && (((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j-1] == ' ')) || ((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ') && (mat[i][j+1] == ' '))
    				|| ((mat[i-1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i+1][j] == ' ') && (mat[i][j-1] == ' ') && (mat[i][j+1] == ' '))))
    				return j;
    	return -1;
    }
    
    int devolve_linha_adjacencia_2(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && (((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i-1][j] == ' ')) ||
    				 ((mat[i-1][j] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i+1][j] == ' ') && (mat[i][j+1] == ' ')) 
    				 || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' '))))
    				return i;
    	return -1;
    }
    
    int devolve_coluna_adjacencia_2(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && (((mat[i-1][j] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i-1][j] == ' ')) ||
    				 ((mat[i-1][j] == ' ') && (mat[i][j+1] == ' ')) || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' ')) || ((mat[i+1][j] == ' ') && (mat[i][j+1] == ' ')) 
    				 || ((mat[i][j-1] == ' ') && (mat[i+1][j] == ' '))))
    				return j;
    	return -1;
    }
    
    int devolve_linha_adjacencia_1(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && ((mat[i][j+1] == ' ') || (mat[i][j-1] == ' ') || (mat[i-1][j] == ' ') || ((mat[i+1][j] == ' ')))) 
    				 return i;
    	return -1;
    }
    
    int devolve_coluna_adjacencia_1(char mat[][MAXC])
    {
    	int i,j;
    	for(i=0; i<MAXL; i++)
    		for(j=0; j<MAXC; j++)
    			if((mat[i][j] == ' ') && ((mat[i][j+1] == ' ') || (mat[i][j-1] == ' ') || (mat[i-1][j] == ' ') || ((mat[i+1][j] == ' ')))) 
    				return j;
    	return -1;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Can you narrow it down? What input do you give it? When does it fail? Is there a reason you are using C style strings and strtok in this C++ program?

    Usually when something works in one environment and not in another it is because of an uninitialized variable, access memory out of bounds, or some other undefined behavior that is wrong in both environments but just happens not to cause a crash in one.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    18
    Hi!

    The program resumes to this:

    Parking Lot.

    The surface of the park becomes damaged over time, having holes on it. To repair this event and cover the holes, a machine is used to cover them with tar. Tar, being a slimy substance, when dropped it spreads by all free parts that are next to the point where it is placed. The tar moves only to north, east, west and south to the point of origin.
    The surface of the park is represented by a matrice, composed by L lines and C columns, up to 100x100. The occupied squares are represented by an X and the holes by a space ( ). Build a program that determines the minimum number of moves the machine has to make so that it can cover all the holes in the park.

    The program receives a text file with the following input:

    2
    5 5
    XXXXX
    X X
    X X X
    X X
    XXXXX
    5 5
    XXXXX
    X X
    XXXXX
    X X
    XXXXX

    where the first line represents the number of matrices to "evaluate" the folloing lines represent the number of lines and columns followed by the matrice itself.

    Than it produces the output:

    Matrice #1: Moves=1
    Matrice #2: Moves=2

    My problem is that the program as written runs correclty in windows, but when i compile it on unix, with g++ and the - ansi and -Wall options its works just fine except that in all the first matrices evaluted the number of moves presented is the number of moves +13. I simply do not know where the +13 comes from...

    Any ideas??

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    In addition to Daved's comments, on a quick look, the only obvious potential causes of problems I see are;

    1) You're using macros MAXC and MAXL. Try using them as const int values (probably more a style issue, but you never know).

    2) A variable named l (lower case L) that is used as an array index. If you have a typo, it will be hard to distinguish between that and a 1 (one).

    3) Code like this (in your alcatroar() function);
    Code:
    	if(l<0 || l>MAXL || c<0 || c>MAXC)
    		return 0;
    	if(mat[l][c] == ' ')
    	{	
    		total++;
    		mat[l][c] = 'X';
    		mat[l-1][c]= 'X';
    		mat[l+1][c]= 'X';
    		mat[l][c-1]= 'X';
    		mat[l][c+1]= 'X';
    is a problem. If l is zero, for example, will modify mat[-1][c] (among others) which is an element outside array bounds. Similar situation if l is zero. Even worse, if l is equal to MAXL, then the code modifies mat[MAXL][c], mat[MAXL+1][c], mat[MAXL][c-1], and mat[MAXL][c+1] which are all potentially outside array bounds (as array indices run from zero to the dimension minus one). Similar situation if c is MAXC.

    4) Other functions have the same problem with array indices, except (on a quick look) they check the values rather than modifying them. Which means the results of the tests are undefined.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my program i cant figure out...
    By youareafever in forum C Programming
    Replies: 7
    Last Post: 11-01-2008, 11:56 PM
  2. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 09:37 PM
  3. Knight's Tour Recursion Problem
    By thephreak6 in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 09:18 AM
  4. Peculiar Problem with Printf
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-02-2002, 12:34 AM
  5. need help
    By emperor in forum C Programming
    Replies: 1
    Last Post: 03-04-2002, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21