Click here to Skip to main content
15,886,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to read a matrix from a file and I have to sort the rows of the matrix to be an ascending order in the first column, and finally I have to write the ordered matrix to a file.

My problem that doesn't write and I guess doesn't read the matrix, because I can't see anything in the output file.

Here's the code what I've written:
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
int main(void){
        FILE *file;
        file = fopen("input.dat","r");
        if(file == NULL)
        {
        printf("Error opening file!\n");
        exit(1);
        }
        int **m;
        m = (int**) malloc(noOfRows * sizeof(int *));
        for(i = 0; i < noOfRows; i++)
        {
                m[i] = (int*) malloc(noOfRows * sizeof(int));
        }
        for(i = 0; i < noOfRows; i++)
        {
                for(j = 0; j < lenghtRows; j++)
                {
                        fscanf(file,"%d", &m[i][j]);
                }
        }
        for(i = 0; i < noOfRows ;++j)
        {
                for(j = 0; j < lenghtRows;++j)
                { for(k=(j+1);k < lenghtRows; ++k)
                        {
                        if (m[i][j] > m[i][k]){
                        swap(m, noOfRows, lenghtRows,i, i + 1);
                        }
                        }
                }
        }
        fclose(file);
        for(i = 0; i < noOfRows; ++i)
        {
                for(j = 0; j < lenghtRows;++j) {
                        printf(" %d", m[i][j]);
        }
                printf("\n");
        }
        FILE *p;
        p = fopen("output.dat", "w");
        if(p == NULL)
        {
         printf("Error opening file!\n");
         exit(1);
        }
        for(i = 0; i < noOfRows; ++i)
        {
                for(j = 0; j < lenghtRows; ++j)
                {
                        fprintf(p, "%d", m[i][j]);
                }
                fprintf(p,"\n");
        }
        fclose(p);
}


What I have tried:

There's no error code and I've tried everything. I'm using a header file and another .c file where the function is and also I use a makefile as well.

PLEASE, anybody can help me?
Posted
Updated 11-May-20 12:51pm
Comments
[no name] 11-May-20 18:28pm    
Have your program print "Hello, world!", first thing. If you didn't get that far, you're asking the wrong question.

1 solution

What you have here is a monolithic program - there is one function. That is only reasonable for the most trivial of programs and, in my opinion, your program is not quite trivial enough for that to be acceptable. For this reason, I think you need to break this into functions. This will also help you sort things out because you can focus on and debug one thing at a time. This needs to be broken into a series of functions. I would make one function to read the file and create the matrix. The creation of the matrix should also be its own function and there needs to be a corresponding matrix free function the releases all the memory it allocated. Then you should make a function to sort the matrix and one to write the matrix. The matrix writer should take a file object pointer as an argument so you can pass it a handle to an open file to write it or pass it stdout to display it in your console. BTW - you know that "printf" and "fprintf to stdout" are essentially the same thing, right?

You have all the code for each of these functions so just need to call them appropriately. The hardest thing will probably be to sort out how to mass your matrix around. The answer is simple - you pass an argument of the type "int**" since that is how you declared m. Once you get these functions and their prototypes sorted out your main function should look something like this :
C++
int main(void)
{
    const char * inputFile = "input.dat";
    const char * outputFile = "output.dat";
    FILE *pfile = nullptr;
    int **m = nullptr;

    m = ReadMatrix( inputFile );   // read the file and allocate the matrix
    if( ! m )
        return -1;    // failed to read the file

    printf( "file %s was read - matrix is :\n", inputFile );
    WriteMatrix( m, stdout );

    SortMatrix( m );

    printf( "sorted matrix is :\n" );
    WriteMatrix( m, stdout );
   
    pfile = fopen( outputFile, "w");
    if( ! pfile )
    {
       fprintf( stderr, "Error opening file '%s'!\n", outputFile );
       return -1;
    }

    WriteMatrix( m, pfile );
    fclose( pfile );

    printf( "file %s was written\n", outputFile );

    ReleaseMatrix( m );

    printf( "matrix was released - sayonara\n" );
    return 0;
}
That makes four functions you need to write and you have the code for all of them already. Now you can focus on debugging each of them as they are called in your program. If you separate creating the matrix and reading file into two functions then you will have five functions.

This is the larger programs and libraries are written - you break the code into components that can potentially be used many times in various places.

-edit-

I see noOfRows comes from somewhere else. This means you can separate reading and allocating the matrix easily.
 
Share this answer
 
v2
Comments
User-14784327 12-May-20 1:37am    
Thanks for the solution, but please can you include the functions as well? I'm a little bit rookie at C language.
Rick York 12-May-20 15:07pm    
You already have the code for all of the functions now. You just have to make the declarations for them and add the code. You can see how they are called in the solution and you know the types of all the arguments - you just pass them exactly as they are declared. For example, WriteMatrix takes a pointer to a file object and the matrix. The pointer to the file is a FILE * and the matrix is an int ** so that is what you pass to it. It has no return of interest so its prototype will look like this : void WriteMatrix( int **matrix, FILE * fp );

That's really all there is to it.
Go through this process for the other functions and you will have it. Make sure the names of the variables in the functions matches the names you give the arguments.
User-14784327 13-May-20 9:10am    
I don't get it and I can't do that. But thank you for the answers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900