Assuming that the directed graphs the user will specify in this program will not be of more than 10 vertices, define a macro called max of value 10 and a two-dimensional matrix called edg, consisting of max rows and max columns. However, you can always increase the size of the macro if you think the user can specify a graph of more than 10 vertices.
In order to initialize all of the elements of the edg matrix to 0, define it as a static matrix. Thereafter, the user will be prompted to specify how many vertices there are in the graph. Suppose the user enters 5 to indicate that there are 5 vertices in the graph, then that value will be assigned to the numb variable.
To make the recipe easy to understand, we assume that the vertices are sequentially numbered from 1 to 5. The user is prompted to specify the edges between the vertices. This means that if there is an edge between vertices 1 and 3, then the user is supposed to enter the edge as 1,3. The vertices entered representing these edges are then assigned to the vertices of v1 and v2. Because the user is asked to specify the edges of the graph and to enter 0 0 when over, when the edge is assigned to the vertices of v1 and v2, we first ensure that the vertices are not 0 and 0. If they are, the program will stop asking for more edges and will branch to the statement from where the display of the adjacency matrix begins. If the vertices in the edge are not zero, then a value, 1, is assigned in the two-dimensional edg matrix at the index location of [v1][v2]. So, if there is an edge between vertices 1 and 2, then value 1 will be assigned at the edg[1][2] index location, replacing the value 0 that was initially there.
When all the edges of the graph are entered, the user will enter the vertices as 0 0 to indicate that all the edges have been entered. In that case, a nested for loop is executed and all the elements of the edg matrix are displayed on screen.
The program is compiled using GCC, as shown in the following screenshot. Because no error appears during the compilation, this means the adjmatdirect.c program has successfully compiled into the adjmatdirect.exe file. On executing the file, the user will be prompted to specify the number of vertices and its edges. Once the vertices and edges are entered, the program will display the adjacency matrix representation of the graph (take a look at the following screenshot):
Now, let's explore how to do the same thing for an undirected graph.