Question

I'm writing code to create a matrix out of a list of edges.

However, when I run said code, I get a "phantom edge" that is not in the input data, which goes on to screw up the rest of my program. The edge is 9,2 in the matrix, or 8,1 in elemental code form.

All elements in the matrix are initialized to 0 before hand.

Here is the input data to do with the matrix:

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

7 10
8 4
8 6
9 4
9 5
10 7
10 3

Here are the functions that handle the input:

void displayMatrix(int **matrix, int numberVertices){                           //function displays the matrix

        int i, j;

        for(i=0; i<numberVertices; i++)                                         //go through eveyr element
        {
                for(j=0; j<numberVertices; j++)
                {
                        printf("%d ", matrix[i][j]);                            //print element
                }
                printf("\n");
        }

        printf("\n\n");
}


void inputMatrix(FILE *fp, int ** matrix)                                       //file places value 1 into matrix if edge exists for the adjacency matrix
{
        int e1, e2;

        while(!feof(fp))                                                        //continue to the end of the file
        {
                fscanf(fp, "%d %d", &e1, &e2);                                  //get pairs
                e1 = e1 - 1;                                                    //adjust the edges for array use
                e2 = e2 - 1;
                matrix[e1][e2] = 1;                                             //place value 1 into appropriate location in adjacency matrix
        }

        fclose(fp);                                                             //close the file connection
}

0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0 0
1 0 0 0 1 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 1 0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 1
0 0 0 1 0 1 0 0 0 0
0 *1 0 1 1 0 0 0 0 0
0 0 1 0 0 0 1 0 0 0

*the entry that should not exist, not in the input data

Was it helpful?

Solution

The problem is that you're looping one more additional time than necessary, causing fscanf to fail before the first conversion and thus leaving e1 and e2 as they were from the prior read. As it turns out, the last entry has e1 set to 10 and e2 to 3, so e1 becomes 9 and e2 becomes 2, thus causing your phantom edge.

The cause of this additional loop is because your loop condition doesn't do what you think it does. feof checks if the end-of-file flag has been set, and this can only be set when attempting to read at the end of the file. Since you're checking for end-of-file before your read, you're not actually picking up on this until the next iteration, thus you loop an additional time. The proper correction is very simple; just continue until fscanf results in EOF.

    while (fscanf(fp, "%d %d", &e1, &e2) != EOF)
    {
            matrix[e1 - 1][e2 - 1] = 1;                  
    }

OTHER TIPS

As pointed out in the comments, you're not testing for errors in fscanf.

In particular, you have not yet reached the end of file after reading 10 3, presumably because a newline was encountered.

However, in the next time around fscanf will return zero. Then you subtract 1 from those values (which were not read) to get 9 2.

You can make sure that two integers were read by doing this:

if( 2 != fscanf(fp, "%d %d", &e1, &e2) ) break;

You can try this:

fscanf(fp, "%d %d\n", &e1, &e2);

When you finish the last two digit, there is one more \n,the loop have to continue,this will make trouble

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top