Computer Programming Contest Preparation

ToolBox - Source for: 3/394/a-debug.c



/home/toolbox/public_html/solutions/3/394/a-debug.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 #define MAX_NAME_LENGTH 15
   19 #define MAX_NUMBER_DIMENSIONS 15
   20 #define MAX_ARRAYS 10000
   21 
   22 /*
   23  *  Author: Isaac Traxler
   24  *    Date: 2021-08-26
   25  * Purpose: fun
   26  * Problem: 394 - Mapmaker
   27  */
   28 
   29 
   30 /*
   31  * This template reads data a specified number of times.
   32  */
   33 
   34 typedef struct ARRAY_STRUCT
   35 {
   36     char name[MAX_NAME_LENGTH];
   37     int base;
   38     int size;
   39     int dimCnt;
   40     int frst[MAX_NUMBER_DIMENSIONS];
   41     int last[MAX_NUMBER_DIMENSIONS];
   42     int diff[MAX_NUMBER_DIMENSIONS];
   43 } ARRAY_STRUCT;
   44 
   45 
   46 
   47 int N;
   48 int R;
   49 ARRAY_STRUCT a[MAX_ARRAYS];
   50 char query[MAX_NAME_LENGTH];
   51 int indicies[MAX_NUMBER_DIMENSIONS];
   52 int qIdx;
   53 
   54 void init()
   55 {
   56     /* FUNCTION init */
   57     scanf(" %d %d ", &N, &R);
   58 } /* FUNCTION init */
   59 
   60 void dumpArrays()
   61 {
   62     /* FUNCTION dumpArrays */
   63     int i;
   64     int j;
   65 
   66     for (i=0; N>i; i++)
   67         {
   68             /* for each array */
   69             printf("%8d(%1d): %-11s[(%d..%d[%d])", a[i].base, a[i].size, a[i].name, a[i].frst[1], a[i].last[1], a[i].diff[1]);
   70             for (j=2; a[i].dimCnt>=j; j++)
   71                 {
   72                     /* for each additional dimension */
   73                     printf(", (%d..%d[%d])", a[i].frst[j], a[i].last[j], a[i].diff[j]);
   74                 } /* for each additional dimension */
   75             printf("]\n");
   76         } /* for each array */
   77 } /* FUNCTION dumpArrays */
   78 
   79 void dumpQuery()
   80 {
   81     /* FUNCTION dumpQuery */
   82     int i;
   83 
   84     printf("query: %s[%d", query, indicies[1]);
   85     for (i=2; a[qIdx].dimCnt>=i; i++)
   86         {
   87             /* for 2 thru last index of query */
   88             printf(", %d", indicies[i]);
   89         } /* for 2 thru last index of query */
   90     printf("]\n");
   91 } /* FUNCTION dumpQuery */
   92 
   93 void getArrayDefinitions()
   94 {
   95     /* FUNCTION getArrayDefinitions */
   96     int i;
   97     int j;
   98 
   99     for (i=0; N>i; i++)
  100         {
  101             /* for each array definitions */
  102             scanf(" %s ", a[i].name);
  103             scanf(" %d ", &a[i].base);
  104             scanf(" %d ", &a[i].size);
  105             scanf(" %d ", &a[i].dimCnt);
  106             for (j=1; j<=a[i].dimCnt; j++)
  107                 {
  108                     /* for each dimension */
  109                     scanf(" %d %d ", &a[i].frst[j], &a[i].last[j]);
  110                     a[i].diff[j] = a[i].last[j] - a[i].frst[j] + 1;
  111                 } /* for each dimension */
  112         } /* for each array definitions */
  113 } /* FUNCTION getArrayDefinitions */
  114 
  115 int findArrayName(char name[])
  116 {
  117     /* FUNCTION findArrayName */
  118     int i;
  119     int fnd=-1;
  120 
  121     for (i=0; (N>i) && (0 != fnd); i++)
  122         {
  123             /* hunt for matching name */
  124             DEBUG printf("comparing |%s| to a[%d]|%s|", name, i, a[i].name);
  125             fnd = strcmp(name, a[i].name);
  126             DEBUG printf(" with result of %d\n", fnd);
  127         } /* hunt for matching name */
  128     return (i - 1);
  129 } /* FUNCTION findArrayName */
  130 
  131 void getReference()
  132 {
  133     /* FUNCTION getReference */
  134     int i;
  135 
  136     scanf(" %s ", query);
  137     DEBUG printf("query = [%s]\n", query);
  138     qIdx = findArrayName(query);
  139     DEBUG printf("%s is a[%d].name(%s) with %d indicies\n", query, qIdx, a[qIdx].name, a[qIdx].dimCnt);
  140     for (i=1; a[qIdx].dimCnt>=i; i++)
  141         {
  142             /* get each index */
  143             scanf(" %d ", &indicies[i]);
  144         } /* get each index */
  145 } /* FUNCTION getReference */
  146 
  147 void process()
  148 {
  149     /* FUNCTION process */
  150     /* qIDX says which array is being queried */
  151     /* indicies are the indicies to use for the query */
  152     int address;
  153     int i;
  154     int j;
  155     int idx;
  156     int tmp;
  157 
  158     DEBUG dumpQuery();
  159     idx = a[qIdx].dimCnt;
  160     /* last index just needs to subtract start address */
  161     address = indicies[idx] - a[qIdx].frst[idx];
  162     idx--;
  163     DEBUG printf("(address %d) (idx %d)\n", address, idx);
  164     for (i=idx; 0<i; i--)
  165         {
  166             /* for each index */
  167             tmp = 1;
  168             for (j=i; j<a[qIdx].dimCnt; j++)
  169                 {
  170                     /* for each remaining index */
  171                     tmp = tmp * a[qIdx].diff[j+1];
  172                     DEBUG printf("j loop (i %d) (j %d) (tmp %d) (diff %d)\n", i, j, tmp, a[qIdx].diff[j+1]);
  173                 } /* for each remaining index */
  174             address = address + (tmp * (indicies[i] - a[qIdx].frst[i]));
  175             DEBUG printf("outer loop (i %d) (tmp %d) (indicies %d) (address %d)\n",i, tmp, indicies[i], address);
  176         } /* for each index */
  177     address = (address * a[qIdx].size) + a[qIdx].base;
  178     /* actually output answer */
  179     printf("%s[%d", query, indicies[1]);
  180     for (i=2; i<=a[qIdx].dimCnt; i++)
  181         {
  182             /* print 2..N indicies */
  183             printf(", %d", indicies[i]);
  184         } /* print 2..N indicies */
  185     printf("] = %d\n", address);
  186 } /* FUNCTION process */
  187 
  188 int main()
  189 {
  190     /* main */
  191     int i;
  192 
  193     init();
  194     getArrayDefinitions();
  195     DEBUG  dumpArrays();
  196     for (i=0; i<R; i++)
  197         {
  198             /* while */
  199             DEBUG dumpArrays();
  200             getReference();
  201             DEBUG dumpArrays();
  202             process();
  203         } /* while */
  204 
  205     return EXIT_SUCCESS;
  206 } /* main */
  207 
  208