Thread: transposing matrix from file to file

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    transposing matrix from file to file

    This is what I have, but when I run it the file that is printed out has a single number. Can anyone detect anything out of place?

    Code:
    #include <stdio.h>
    #define max_row 20
    #define max_col 20
    
    int main(void)
    {
    
        int matrix[max_row][max_col];
        int i, j, nr, nc;
        
       
        FILE * input;
        FILE * output;
       
        input = fopen("data.in.txt", "r");
        output = fopen("data.out.txt", "w");
        
        if(!input)
    	{
    		printf("failed to open file");
    	}
    	
    	/* read input*/
    	
    	fscanf(input, "%d%d", &nr, &nc);
        for(i = 0; i < nr; i++)
    	{
    		for(j = 0; j < nc; j++)
    		{
    			fscanf(input, "%d", &matrix[i][j]);
    		}
    	}
    		
    	/* print transpose in file*/ 
    	
    	for(j = 0; j < nc; j++)
    	{
    		for(i = 0; i < nr; i++)
    		{
    			fprintf(output, "%d ", matrix[i][j]);
        	}
        return 0;
        }
       }
    Thanks so much for looking at it.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Can anyone detect anything out of place?
    Well, since you didn't actually show us an example of the input...

    Code:
    if(!input)
    	{
    		printf("failed to open file");
    	}
    What's the point of even checking to see if the file opened correctly if all you do is continue on regardless?

    Now let's see what happens when we learn how to indent properly, shall we?
    Code:
    int main(void)
    {
        int matrix[max_row][max_col];
        int i, j, nr, nc;
        FILE * input;
        FILE * output;
       
        input = fopen("data.in.txt", "r");
        output = fopen("data.out.txt", "w");
        
        if(!input)
        {
            printf("failed to open file");
        }
    	
        /* read input(even if we have no file to read from...)*/
    	
        fscanf(input, "%d%d", &nr, &nc);
        for(i = 0; i < nr; i++)
        {
            for(j = 0; j < nc; j++)
            {
                fscanf(input, "%d", &matrix[i][j]);
            }
        }
    		
        /* print transpose in file*/ 
        for(j = 0; j < nc; j++)
        {
            for(i = 0; i < nr; i++)
            {
                fprintf(output, "%d ", matrix[i][j]);
            }
            return 0;
        }
    }
    Much better.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Sorry my indentation wasn't up to par. I'm not very good at this, obviously.

    As for an input, I must have forgotten
    it would look something like this:

    3 4
    1 2 3 5
    4 5 6 5
    7 8 9 5

    It only transposes the first column.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to be missing the point. Your braces is in the wrong place, which is why I colored them for you. What exactly do you think that return statement inside your loop is going to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Thanks. I did realize that my braces weren't right. I fixed it, but the program still doesn't do what I'd like it to do. (thanks btw, I probably wouldn't have noticed it.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM