ZOJ Problem Set - 1722
The Cybersoft Computer Company (a leader in programming languages) has hired you to work on a new programming language named A--. Your task is to work on the array mapping tasks of the language. You will take an array reference such as x[5,6] and map it to an actual physical address. In preparation for doing this, you will write a program that will read in several array declarations and references and give the physical address of each reference. The physical address output by the program should be an integer number in base 10.
The physical address of an array reference A[i1 , i2 , . . ., iD ] is calculated from the formula C0 + C1i1 + C2i2 + . . . + CDiD, where the constants C0 . . . CD are calculated as specified below.
B = Base address of the array
The first line of the input file contains two positive integers. The first integer specifies N, the number of arrays defined in the data file, and the second integer specifies R, the number of array references for which addresses should be calculated. The next N lines each define an array, one per line, and the following R lines contain one array reference per line for which an address should be calculated.
Each line which defines an array contains, in the following order, the name of the array (which is limited to 10 characters), a positive integer which specifies the base address of the array, a positive integer which specifies the size in bytes of each array element, and D, the number of dimensions in the array (no array will have fewer than 1 or more than 10 dimensions). This is followed on the same line by D pairs of integers which represent the lower and upper bounds, respectively, of dimensions 1 . . . D of the array.
Each line which specifies an array reference contains the name of the array followed by the integer indexes i1, i2, . . ., iD where D is the dimension of the array.
The output file should contain the array references and the physical addresses. There should be one array reference and physical address per line. The formatting guidelines below must be adhered to.
For each line of output:
1. Output the name of the array
2. Output a left square bracket
3. Output each index value (each pair of indexes should have a single comma and space between them)
4. Output a right square bracket, a space, an equal sign, and another space
5. Output the physical address
ONE[2, 4] = 1526
Source: Mid-Central USA 1996