How to create a hash from two equal-length arrays (keys and values) in C

1 Answer

0 votes
// C program that demonstrates three approaches to building 
// a “hash‑like” structure from two equal‑length arrays

#include <stdio.h>
#include <string.h>

#define MAX_ITEMS 10
#define MAX_KEY_LEN 32

// ------------------------------------------------------------
// Structure for a key/value pair
// ------------------------------------------------------------
typedef struct {
    char key[MAX_KEY_LEN];
    int value;
} Pair;

// ------------------------------------------------------------
// Build an array of Pair structs (preserves order)
// ------------------------------------------------------------
int make_pair_array(const char keys[][MAX_KEY_LEN], const int values[],
                    Pair out[], int count)
{
    for (int i = 0; i < count; i++) {
        strncpy(out[i].key, keys[i], MAX_KEY_LEN);
        out[i].value = values[i];
    }
    
    return count;
}

// ------------------------------------------------------------
// Build a sorted array of Pair structs (sorted by key)
// ------------------------------------------------------------
int make_sorted_pairs(const char keys[][MAX_KEY_LEN], const int values[],
                      Pair out[], int count)
{
    // First copy into output array
    make_pair_array(keys, values, out, count);

    // Simple bubble sort by key (idiomatic C, no std::map)
    for (int i = 0; i < count - 1; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            if (strcmp(out[j].key, out[j + 1].key) > 0) {
                Pair temp = out[j];
                out[j] = out[j + 1];
                out[j + 1] = temp;
            }
        }
    }

    return count;
}

// ------------------------------------------------------------
// Parallel arrays version (keys[] and values[] stay separate)
// ------------------------------------------------------------
void print_parallel_arrays(const char keys[][MAX_KEY_LEN],
                           const int values[], int count)
{
    for (int i = 0; i < count; i++) {
        printf("  %s => %d\n", keys[i], values[i]);
    }
}

// ------------------------------------------------------------
// Print an array of Pair structs
// ------------------------------------------------------------
void print_pairs(const Pair arr[], int count)
{
    for (int i = 0; i < count; i++) {
        printf("  %s => %d\n", arr[i].key, arr[i].value);
    }
}

// ------------------------------------------------------------
// Main program demonstrating all three
// ------------------------------------------------------------
int main(void)
{
    const char keys[][MAX_KEY_LEN] = {"a", "b", "c"};
    const int values[] = {1, 2, 3};
    const int count = 3;

    Pair ordered[MAX_ITEMS];
    Pair sorted[MAX_ITEMS];

    make_pair_array(keys, values, ordered, count);
    make_sorted_pairs(keys, values, sorted, count);

    printf("Parallel arrays:\n");
    print_parallel_arrays(keys, values, count);

    printf("\nArray of Pair structs (preserves order):\n");
    print_pairs(ordered, count);

    printf("\nSorted Pair array (sorted by key):\n");
    print_pairs(sorted, count);

    return 0;
}



/*
run:

Parallel arrays:
  a => 1
  b => 2
  c => 3

Array of Pair structs (preserves order):
  a => 1
  b => 2
  c => 3

Sorted Pair array (sorted by key):
  a => 1
  b => 2
  c => 3
  
*/

 



answered 3 hours ago by avibootz
...