Thread: can't find error(s) in my program..help?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    can't find error(s) in my program..help?

    I have to write a program to read this matrix

    3 4
    1 4 5 4
    4 5 6 5
    1 2 2 3

    I wrote it, and my program is not properly reading it.


    Code:
    #include <stdio.h>
    int
    main ()
    {
    
    FILE *cap;
    	int rowsize, colsize, i, j, x, y, b, c;
    	int mat[100][100];
    	cap = fopen ("mat1.txt", "r");
    
    	fscanf (cap, "%d", &rowsize);
    	fscanf (cap, "%d", &colsize);
    
    
    
    	for (i = 0; i < rowsize; ++i)
    	{
    		for (j = 0; j < colsize; ++j)
    		{
    			fscanf (cap, "%d", &mat[i][j]);
    		}
    	}
    
    			
    			y = mat[0][0];
    			x = mat[rowsize-1][colsize-1];
    			mat[0][0]= x;
    			mat[rowsize-1][colsize-1] = y;
    	
    			b = mat[0][colsize-1];
    			c = mat[rowsize-1][0];
    			mat[0][colsize-1] = c;
    			mat[rowsize-1][colsize-1] = b;
    		
    	
    		
    		for (j = 1; j < colsize - 2; ++j)
    		{
    			
    			
    			mat [rowsize-1][j] = mat[rowsize-1][j] * 2;
    			
    		}
    	  
    	 	 
    		for (i = 0; i < rowsize; ++i)
    		{
    			
    			if (mat[i][1] % 2 == 0) 
    			{
    				mat[i][1] = mat[i][1] / 2;
    			}
    			
    			    
    		}
    	
    
    
    			for (i = 0; i < rowsize; ++i);
    			{
    				for (j = 0; j < colsize; ++j)
    				{
    					printf ("%3d", mat[i][j]);
    					printf ("\n");
    				}
    			}
    
    	fclose (cap);
    		
    }

  2. #2
    Registered User hellork's Avatar
    Join Date
    Nov 2010
    Posts
    39
    What results were you expecting? It looks like you have some parenthesis out of place. Here is the pseudocode of what I think you were trying to do (indentation is used instead of parenthesis).

    Code:
    #include <stdio.h>
    int main  void
    	FILE *cap
    	int rowsize, colsize, i, j, x, y, b, c
    	int mat[100][100]
    	cap = fopen  "mat1.txt", "r"
    	fscanf  cap, "%d", &rowsize
    	fscanf  cap, "%d", &colsize
    	
    	printf  "read from file: \n%d %d\n", rowsize,colsize
    	for  i = 0; i < rowsize; ++i
    		for  j = 0; j < colsize; ++j
    			fscanf  cap, "%d", &mat[i][j]
    			printf  "%d ",mat[i][j]
    			y = mat[0][0]
    			x = mat[rowsize-1][colsize-1]
    			mat[0][0]= x
    			mat[rowsize-1][colsize-1] = y
    			b = mat[0][colsize-1]
    			c = mat[rowsize-1][0]
    			mat[0][colsize-1] = c
    			mat[rowsize-1][colsize-1] = b
    		printf  "\n"
    	printf  "\nresult:\n"
    		
    	for  j = 1; j < colsize - 2; ++j
            mat [rowsize-1][j] = mat[rowsize-1][j] * 2
    		for  i = 0; i < rowsize; ++i
    			if  mat[i][1] % 2 == 0
    				mat[i][1] = mat[i][1] / 2
    			for  i = 0; i < rowsize; ++i
    				for  j = 0; j < colsize; ++j
    					printf  "%3d", mat[i][j]
    				printf  "\n"
    	fclose  cap
    	return 0
    Anchor, a free, GPL project I'm working on: Anchor | freshmeat.net will convert the above pseudocode to C and compile it producing this result:
    Code:
    read from file: 
    3 4
    1 4 5 4 
    4 5 6 5 
    1 2 2 3 
    
    result:
      3  2  5  1
      4  5  6  5
      1  4  2  1
    This is what Anchor sends to the compiler. Just be sure to tell it if you prefer -tabs.
    Code:
    #include <stdio.h>
    int main (void){
    	FILE *cap;
    	int rowsize, colsize, i, j, x, y, b, c;
    	int mat[100][100];
    	cap = fopen ("mat1.txt", "r");
    	fscanf (cap, "%d", &rowsize);
    	fscanf (cap, "%d", &colsize);
    	printf ("read from file: \n%d %d\n", rowsize,colsize);
    	for (i = 0; i < rowsize; ++i){
    		for (j = 0; j < colsize; ++j){
    			fscanf (cap, "%d", &mat[i][j]);
    			printf ("%d ",mat[i][j]);
    			y = mat[0][0];
    			x = mat[rowsize-1][colsize-1];
    			mat[0][0]= x;
    			mat[rowsize-1][colsize-1] = y;
    			b = mat[0][colsize-1];
    			c = mat[rowsize-1][0];
    			mat[0][colsize-1] = c;
    			mat[rowsize-1][colsize-1] = b;
     		}	printf ("\n");
     	}	printf ("\nresult:\n");
    	for (j = 1; j < colsize - 2; ++j){
            mat [rowsize-1][j] = mat[rowsize-1][j] * 2;
    		for (i = 0; i < rowsize; ++i){
    			if (mat[i][1] % 2 == 0){
    				mat[i][1] = mat[i][1] / 2;
     			}	for (i = 0; i < rowsize; ++i){
    				for (j = 0; j < colsize; ++j){
    					printf ("%3d", mat[i][j]);
     				}	printf ("\n");
     	}	}	}	fclose (cap);
    	return 0;
    }
    Be sure to vote for my project if you find it useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. find at least 4 errors
    By Lego_TeCh in forum C Programming
    Replies: 20
    Last Post: 08-01-2009, 02:28 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM