💾 Archived View for tilde.pink › ~smlckz › log › 2024-03-23-re-c-arrays.gmi captured on 2024-05-10 at 12:04:31. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Re: C Arrays

thrig’s post on "C Arrays"

If you don’t mind using C99 VLA constructs, you can write like this:

#include <stdio.h>

void show(size_t rows, size_t cols, int grid[rows][cols])
{
    for (size_t r = 0; r < rows; ++r) {
        for (size_t c = 0; c < cols; ++c)
            printf("%d ", grid[r][c]);
        putchar('\n');
    }
}

int main(int argc, char *argv[])
{
    int grid[3][3] = {{1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}};
    show(3, 3, grid);
}

Here in our college, we had to implement algorithms on graphs in C for our assignments. Have a taste:

#include <stdbool.h>
#include <stdio.h>
#include <math.h>

void show_cost_matrix(int n, float a[n][n])
{
    for (int i = 1; i <= n; i++) {
        printf("\tv%d", i);
    }
    printf("\n");
    for (int i = 0; i < n; i++) {
        printf("v%d", i + 1);
        for (int j = 0; j < n; j++) {
            printf("\t%g", a[i][j]);
        }
        printf("\n");
    }
}

int find_min_vertex(int vertices, bool visited[vertices], float mincost[vertices])
{
    float minval = INFINITY, minvert = -1;
    for (int v = 0; v < vertices; v++) {
        if (!visited[v] && mincost[v] < minval) {
            minval = mincost[v];
            minvert = v;
        }
    }
    return minvert;
}

void dijkstra(int vertices, int source_vertex, float A[vertices][vertices], float mincost[vertices], int prev[vertices])
{
    bool visited[vertices];
    for (int v = 0; v < vertices; v++) {
        visited[v] = false;
        mincost[v] = A[source_vertex][v];
        prev[v] = source_vertex;
    }
    visited[source_vertex] = true;
    prev[source_vertex] = -1;
    while (true) {
        int v_min = find_min_vertex(vertices, visited, mincost);
        if (v_min < 0) {
            break;
        }
        visited[v_min] = true;
        for (int v = 0; v < vertices; v++) {
            if (!visited[v]) {
                float minval = mincost[v_min] + A[v_min][v];
                if (minval < mincost[v]) {
                    mincost[v] = minval;
                    prev[v] = v_min;
                }
            }
        }
    }
}

int main(void)
{
    int vertices, edges;
    printf("Find the minimum cost path for a single source vertex for a graph\n\n");
    printf("Enter the number of vertices: ");
    scanf("%d", &vertices);
    printf("Enter the number of edges: ");
    scanf("%d", &edges);
    // The matrix represents a weighted directed graph.
    float a[vertices][vertices], mincost[vertices];
    int prev[vertices];
    // Initialise the cost matrix.
    for (int i = 0; i < vertices; i++) {
        for (int j = 0; j < vertices; j++) {
            // Infinity is used for unknown distances.
            a[i][j] = (i == j) ? 0 : INFINITY;
        }
    }
    // Take the edges of the graph as input.
    printf("Enter the edges of the graph\n\n");
    for (int i = 0; i < edges; i++) {
        printf("Enter adjacent vertices of edge %d\n", i + 1);
        int x, y;
        float weight;
        printf("Enter adjacent vertices: ");
        scanf("%d%d", &x, &y);
        x -= 1, y -= 1;
        printf("Enter weight: ");
        scanf("%f", &weight);
        a[x][y] = weight;
        a[y][x] = weight;
    }
    printf("Enter the source vertex: ");
    int source_vertex;
    scanf("%d", &source_vertex);
    source_vertex -= 1;
    printf("The initial cost matrix:\n");
    show_cost_matrix(vertices, a);
    dijkstra(vertices, source_vertex, a, mincost, prev);
    printf("The minimum cost and previous vectors for each vertices:\n");
    printf("vert:");
    for (int v = 0; v < vertices; v++) {
        printf("\t%d", v + 1);
    }
    printf("\ncost:");
    for (int v = 0; v < vertices; v++) {
        printf("\t%g", mincost[v]);
    }
    printf("\nprev:");
    for (int v = 0; v < vertices; v++) {
        if (prev[v] >= 0) {
            printf("\t%d", prev[v] + 1);
        } else {
            printf("\tsrc");
        }
    }
    printf("\n");
    return 0;
}