Silvester
Silvester

Reputation: 3199

how to make adjacency matrix like this input

input is like this

5 // is # of vertices
1 1 0 1 // 1<->2 1<->3 1<->4 1<->5
0 0 0 // 2<->3 2<->4 2<->5
0 1 // 3<->4 3<->5
1 // 4<->5

I want to make adjacency matrix, when that input is inserted. how to do that?

I made already matrix like this

array = (int)malloc(sizeof(int)*numVetex);

Upvotes: 1

Views: 1800

Answers (1)

codaddict
codaddict

Reputation: 455440

There are many ways to do this. Here is one of them:

    int **array;
    int numVertex;
    int i,j;

    scanf("%d",&numVertex);
    array = malloc(sizeof(int*) * numVertex);
    for(i=0;i<numVertex;i++) {
            array[i] = malloc(sizeof(int) * numVertex);
    }
    for(i=0;i<numVertex-1;i++) {
            for(j=i+1;j<numVertex;j++) {
                    scanf("%d",&array[i][j]);
                    array[j][i] = array[i][j];
            }
            array[i][i] = 0;
    }
    // use array 
    // free it

Upvotes: 2

Related Questions