data-structures · intermediate · ~15 min

Count directed edges

Traverse the whole matrix.

Challenge

int count_edges_directed(const int *adj,int n);

Return the total number of directed edges (count every adj[i*n+j]==1).

Input format

adj n×n row-major 0/1.

Output format

Total edge count.

Constraints

Starter code

#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; }

Common mistakes

Dividing by 2 — that is for undirected edges, not a directed count.

Edge cases to handle

Empty graph → 0; complete digraph → n·(n−1).

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.