data-structures · intermediate · ~15 min
Read a graph stored as an adjacency matrix.
Graphs here use an adjacency matrix: adj[i*n+j]==1 means a directed edge i→j.
int out_degree(const int *adj,int n,int v);
Return the out-degree of vertex v (number of edges leaving it).
adj is n×n row-major 0/1; 0<=v<n.
Out-degree of v.
No self-loops in tests.
#include <stddef.h>
/* Adjacency matrix adj (n x n, row-major): adj[i*n+j]=1 means an edge i->j. Return the out-degree of v (edges leaving v). */
int out_degree(const int *adj,int n,int v){ (void)adj;(void)n;(void)v; return 0; }
Indexing the column instead of the row; that gives in-degree.
Isolated vertex → 0; row of all 1s → n.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.