Preprocessor Macros Exercises

  < Previous  Next >
  1. Write an "include guard" for a data.h header file.
  2. Replace each of the two const ints below with their own preprocessor directive.
    #include <iostream>
    using std::cout;
    using std::endl;
    
    const int NUM_ROWS = 512;
    const int NUM_COLS = 128;
    int fastArray[ NUM_ROWS * NUM_COLS]; // simulating a 2D array with a 1D array
    
    int main(){
        int counter = 0;
        for( int rowI = 0; rowI <= NUM_ROWS; rowI++){
            for( int colI = 0; colI <= NUM_COLS; colI++){
                ARRAY( rowI, colI) = counter++;
                cout << "(" << rowI << ", " << colI << "): " << ARRAY( rowI, colI) << endl;
            }
        }
        return 0;
    }
    
  3. Storing a two dimensional array as a one dimensional array can speed up your code. Add a preprocessor macro ARRAY that takes two arguments, row and col and calculates the one dimensional array index as row + (col * NUM_ROWS).

Last Modified: