Mapmaker
InterviewTime limit1sMemory limit128 MB
Given array declarations with bounds and element sizes, compute each reference's physical address with the row-major address formula.
- Level
Medium4 of 10
- Topics
- Array, Math, Implementation, Simulation
- Solved
- No attempts yet
Problem
The Cybersoft Computer Company (a leader in programming languages) has hired you to work on a new programming language named A--. Your job is the array-mapping part of the language: given an array reference such as x[5, 6], you must map it to an actual physical address. To do this, write a program that reads several array declarations and references and prints the physical address of each reference. Every physical address must be printed as a base-10 integer.
The physical address of an array reference is given by
where the constants are defined as follows:
- = base address of the array
- = number of dimensions of the array
- = lower bound of dimension
- = upper bound of dimension
- = size of one array element, in bytes
- for
Input
The first line contains two positive integers and : is the number of arrays that are declared, and is the number of array references whose addresses must be computed. The next lines each declare one array, and the following lines each contain one array reference.
Each array-declaration line contains, in order: the array name (at most 10 characters), a positive integer giving the base address of the array, a positive integer giving the size in bytes of each array element, and , the number of dimensions (). These are followed, on the same line, by pairs of integers giving the lower and upper bounds of dimensions , respectively.
Each array-reference line contains the array name followed by the integer indices , where is the number of dimensions of that array.
Output
For each reference, print one line containing the reference together with its physical address, formatted exactly as follows:
- Print the array name.
- Print a left square bracket
[. - Print the index values, separating each consecutive pair of indices by a comma and a single space.
- Print a right square bracket, a space, an equals sign, and another space (
] =). - Print the physical address.