#include <stdio.h>
#include <stdlib.h>

typedef int (*FN)(int *, int);

void print_array(int *array, int stride)
{
  int i, j;
  for (i = 0; i < stride; i++)
  {
    for (j = 0; j < stride; j++)
    {
      int index = i * stride + j;
      printf("%3i ", array[index]);
    }
    printf("\n");
  }
  printf("\n");
}

int row_major(int *array, int stride)
{
  int i, j, index = 0, count = 0;
  
  printf("Row major:\n");
  for (i = 0; i < stride; i++)
  {
    for (j = 0; j < stride; j++)
    {
      index = i * stride + j;
      array[index] = count++;
    }
  }
  return index;
}

int col_major(int *array, int stride)
{
  int i, j, index = 0, count = 0;
  
  printf("Column major:\n");
  for (i = 0; i < stride; i++)
  {
    for (j = 0; j < stride; j++)
    {
      index = j * stride + i;
      array[index] = count++;
    }
  }
  return index;
}

int main(int argc, char **argv)
{
  int i, j;
  int stride = 1000;
  int size = stride * stride;
  int *array;
  int total = size;
  FN funcs[] = {row_major, col_major};
  int fnnum = 0;
  int print = 0;
  FN fn = funcs[fnnum];
  
  if (argc > 1)
  {
    stride = atoi(argv[1]);
    total = stride * stride;
  }
  
  if (argc > 2)
  {
    fnnum = atoi(argv[2]);
    fn = funcs[fnnum];
  }
    
  if (argc > 3)
    print = atoi(argv[3]);

  printf("stride = %i, total = %i, fn = %i\n", stride, total, fnnum);
      
  array = (int *) malloc(total * sizeof(int));
  
  fn(array, stride);

  if (print)
  {
    printf("\n");
    print_array(array, stride);
  }
  
  free(array);
  return 0;
}