data-structures · intermediate · ~15 min
Traverse the whole matrix.
int count_edges_directed(const int *adj,int n);
Return the total number of directed edges (count every adj[i*n+j]==1).
adj n×n row-major 0/1.
Total edge count.
—
#include <stddef.h>
/* Number of directed edges in the adjacency matrix (count of 1 entries). */
int count_edges_directed(const int *adj,int n){ (void)adj;(void)n; return 0; }
Dividing by 2 — that is for undirected edges, not a directed count.
Empty graph → 0; complete digraph → n·(n−1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.