SiteBanner

 

(right-click to download)

/* CExplorer -- use two arrays of integers as arbitrary indicies into an array of characters */
/* Note; all arrays contain 'NUM_ITEMS' elements. Note also that in the two integer */
/* arrays, none of the elements exceed '25', which is the last element of a 26-element */
/* array. Thus, the program won't stray outside established storage */

#include <stdio.h>

#define NUM_ITEMS 26

int main(void) {
// the alphabet
char c_items[NUM_ITEMS] = {'a','b','c','d','e','f','g','h','i','j','k','l','m', 'n','o','p','q','r','s','t','u','v','w','x','y','z'};
// number values 0 - 25, in a randomized order
int mixup1[NUM_ITEMS] = {4,10,11,12,25,5,6,1,16,7,2,20,21,3,9, 14, 22,7,23,24,15,8,0,18,19,13};
// an array of arbitrary number values between 0 - 25, but not inclusive of all 0 - 25
int mixup2[NUM_ITEMS] = {0,0,0,1,1,1,6,6,6,7,7,7,2,2,2,25,24,23,22,21,20,19,18,17,16,15};

int x;

printf("\nCharacters directly accessed:\n");
for (x = 0; x < NUM_ITEMS; x++)
     printf("%c ", c_items[x]);

printf("\n\nCharacters accessed through a randomized array of indicies:\n");
for (x = 0; x < NUM_ITEMS; x++)
     printf("%c ", c_items[mixup1[x]]);

printf("\n\nCharacters accessed through a tailored array of indicies:\n");
for (x = 0; x < NUM_ITEMS; x++)
     printf("%c ", c_items[mixup2[x]]);

printf("\n\nCharacters accessed by using both indicies arrays:\n");
for (x = 0; x < NUM_ITEMS; x++)
     printf("%c ", c_items[mixup2[mixup1[x]]]);

printf("\n\nLet's examine one case. We pick index value '14'.\n");
printf("The contents of 'c_items[14]' is the letter '%c'\n", c_items[14]);
printf("The contents of 'mixup1[14]' is %d\n", mixup1[14]);
printf("Using %d from 'mixup1[14]' as an index, 'mixup2[%d]' gives %d\n", mixup1[14], mixup1[14], mixup2[mixup1[14]]);
printf("Thus, 'c_items[mixup2[mixup1[14]]]' gives letter '%c'\n\n", c_items[mixup2[mixup1[14]]]);

return(0);
}