Recent Posts
None | 32 sec ago
None | 2 min ago
HTML | 3 min ago
None | 3 min ago
None | 4 min ago
None | 6 min ago
ASM (NASM) | 7 min ago
ASM (NASM) | 8 min ago
PHP | 8 min ago
Bash | 10 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 4th of Jul 2009 08:29:00 PM Download | Raw | Embed | Report
  1. /*
  2.  * A program to print a spiral matrix of user-defined dimension:
  3.  * Ex: For dimension 5, produce
  4.  *
  5.  *              1       2       3       4       5
  6.  *              16      17      18      19      6
  7.  *              15      24      25      20      7
  8.  *              14      23      22      21      8
  9.  *              13      12      11      10      9
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. int **alloc_2D_int_array(int rows, int columns);
  16. void free_2D_int_array(int **array);
  17.  
  18. int main(void)
  19. {
  20.         int **a;                                                /* to store the array */
  21.         int size;                                               /* the dimension of the array */
  22.         int top, bottom, left, right;   /* to traverse the 2D-array */
  23.  
  24.         int i, j, value = 1;
  25.  
  26.         printf("Enter the matrix dimension: ");
  27.         scanf("%d", &size);
  28.  
  29.         /* allocate the 2D-array and test against failure */
  30.         if ((a = alloc_2D_int_array(size, size)) == NULL)
  31.         {
  32.                 fprintf(stderr, "error: mamory not allocated\n");
  33.                 return -1;
  34.         }
  35.  
  36.         top = left = 0;
  37.         bottom = right = size - 1;
  38.  
  39.         while (value <= size * size)
  40.         {
  41.                 for (i = left; i <= right; ++i)         /* process top row */
  42.                         a[top][i] = value++;
  43.                 ++top;
  44.  
  45.                 for (i = top; i <= bottom; ++i)         /* process right column */
  46.                         a[i][right] = value++;
  47.                 --right;
  48.  
  49.                 for (i = right; i >= left; --i)         /* process bottom row */
  50.                         a[bottom][i] = value++;
  51.                 --bottom;
  52.  
  53.                 for (i = bottom; i >= top; --i)         /* process left column */
  54.                         a[i][left] = value++;
  55.                 ++left;
  56.         }
  57.  
  58.         /* print the array generated */
  59.         for (i = 0; i < size; ++i)
  60.         {
  61.                 for (j = 0; j < size; ++j)
  62.                         printf("%-4d", a[i][j]);
  63.                 putchar('\n');
  64.         }
  65.  
  66.         free_2D_int_array(a);           /* free the allocated array */
  67.         return 0;
  68. }
  69.  
  70. /* alloc_2D_int_array:  routine to allocate a 2D array of dimensions
  71.  *                                              rows * columns. Internally, the 2D array is
  72.  * implemented in a one-dimensional array for efficiency. Returns the
  73.  * allocated array or NULL, if fails. The efficiency results from
  74.  * avoiding multiple calls to malloc() (& free() later)
  75.  */
  76. int **alloc_2D_int_array(int rows, int columns)
  77. {
  78.         int **array, *temp;
  79.         int i;
  80.  
  81.         /* allocating an int * array for storing rows' addresses */
  82.         if ((array = (int **) malloc(rows * sizeof(int *))) == NULL)
  83.                 return NULL;
  84.  
  85.         /* allocating memory for the entire array in a 1D array */
  86.         if ((temp = (int *) malloc(rows * columns * sizeof(int))) == NULL)
  87.         {
  88.                 free(array);    /* free the array of pointers if failed */
  89.                 return NULL;
  90.         }
  91.  
  92.         /* pointing the 2D-array pointers to right places in 1D-array */
  93.         for (i = 0; i < rows; ++i)
  94.         {
  95.                 array[i] = temp;
  96.                 temp += columns;
  97.         }
  98.  
  99.         return array;
  100. }
  101.  
  102. /* free_2D_int_array: routine to deallocate an array allocated using
  103.  *                                        alloc_2D_int_array()
  104.  */
  105. void free_2D_int_array(int **array)
  106. {
  107.         free(*array);   /* free the internal 1D-array */
  108.         free(array);    /* free the "2D-array" */
  109. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: