hanreev

Dynamic size 1D, 2D & 3D array

Apr 13th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. /*
  2.  * Demo initializing dynamic size array in C
  3.  * C99 standard
  4.  * Author: Rifai M. Hanif
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10.  
  11. // Max supported array size: 1000
  12. #define MAX_SIZE 1000
  13.  
  14. // clear console screen
  15. void clear()
  16. {
  17.     #ifdef _WIN32
  18.         system("cls");
  19.     #else
  20.         system("clear");
  21.     #endif
  22.    
  23.     puts("\n\t===== DYNAMIC SIZE ARRAY =====");
  24. }
  25.  
  26. // pause
  27. void pause()
  28. {
  29.     printf("\n\tPress enter/return to continue... ");
  30.     getchar();
  31. }
  32.  
  33. // 1D array
  34. void oneD()
  35. {
  36.     clear();
  37.     puts("\n\t=== 1D ARRAY ===");
  38.    
  39.     int x = 0;
  40.     bool invalid = false;
  41.     char buffer[30];
  42.    
  43.     while(x < 1 || x > MAX_SIZE)
  44.     {
  45.         if(invalid)
  46.             puts("\n\t[Error]: Size not supported!");
  47.        
  48.         printf("\n\tArray A size: ");
  49.         fgets(buffer, sizeof(buffer), stdin);
  50.         sscanf(buffer, "%d", &x);
  51.        
  52.         invalid = x < 1 || x > MAX_SIZE;
  53.     }
  54.    
  55.     int * array = malloc(x * sizeof(int));;
  56.    
  57.     for(int i = 0; i < x; ++i)
  58.     {
  59.         array[i] = i + 1;
  60.     }
  61.    
  62.     printf("\n\tA[%d] = [", x);
  63.     for(int i = 0; i < x; ++i) {
  64.         if(i > 0)
  65.             printf(", ");
  66.        
  67.         printf("%d", array[i]);
  68.     }
  69.     printf("]\n");
  70.    
  71.     pause();
  72. }
  73.  
  74. // 2D array
  75. void twoD()
  76. {
  77.     clear();
  78.     puts("\n\t=== 2D ARRAY ===");
  79.    
  80.     int x = 0, y = 0;
  81.     bool invalid = false;
  82.     char buffer[30];
  83.    
  84.     while(x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE) {
  85.         if(invalid)
  86.             puts("\n\t[Error]: Size not supported!");
  87.        
  88.         printf("\n\tArray A rows count: ");
  89.         fgets(buffer, sizeof(buffer), stdin);
  90.         sscanf(buffer, "%d", &x);
  91.        
  92.         printf("\tArray A columns count: ");
  93.         fgets(buffer, sizeof(buffer), stdin);
  94.         sscanf(buffer, "%d", &y);
  95.        
  96.         invalid = x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE;
  97.     }
  98.    
  99.     int ** array = (int *) malloc(x * sizeof(int *));
  100.    
  101.     for(int i = 0; i < x; ++i)
  102.         array[i] = malloc(y * sizeof(int));
  103.    
  104.     for(int i = 0; i < x; ++i) {
  105.         for(int j = 0; j < y; ++j)
  106.             array[i][j] = (i + 1) * 10 + j + 1;
  107.     }
  108.    
  109.     printf("\n\tA[%d][%d] =\t[", x, y);
  110.     for(int i = 0; i < x; ++i) {
  111.         if(i > 0)
  112.             printf(";\n\t\t\t\t ");
  113.        
  114.         for(int j = 0; j < y; ++j) {
  115.             if(j > 0)
  116.                 printf(", ");
  117.        
  118.             printf("%d", array[i][j]);
  119.         }
  120.     }
  121.     printf("]\n");
  122.    
  123.     pause();
  124. }
  125.  
  126. // 3D array
  127. void threeD()
  128. {
  129.     clear();
  130.     puts("\n\t=== 3D ARRAY ===");
  131.    
  132.     int x = 0, y = 0, z = 0;
  133.     bool invalid = false;
  134.     char buffer[30];
  135.    
  136.     while(x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE || z < 1 || z > MAX_SIZE) {
  137.         if(invalid)
  138.             puts("\n\t[Error]: Size not supported!");
  139.        
  140.         printf("\n\tArray A rows count: ");
  141.         fgets(buffer, sizeof(buffer), stdin);
  142.         sscanf(buffer, "%d", &x);
  143.        
  144.         printf("\tArray A columns count: ");
  145.         fgets(buffer, sizeof(buffer), stdin);
  146.         sscanf(buffer, "%d", &y);
  147.        
  148.         printf("\tArray A depth count: ");
  149.         fgets(buffer, sizeof(buffer), stdin);
  150.         sscanf(buffer, "%d", &z);
  151.        
  152.         invalid = x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE || z < 1 || z > MAX_SIZE;
  153.     }
  154.    
  155.     int *** array = (int **) malloc(x * sizeof(int **));
  156.    
  157.     for(int i = 0; i < x; ++i) {
  158.         array[i] = (int *) malloc(y * sizeof(int *));
  159.        
  160.         for(int j = 0; j < y; ++j)
  161.             array [i][j] = malloc(z * sizeof(int));
  162.     }
  163.    
  164.     for(int i = 0; i < x; ++i) {
  165.         for(int j = 0; j < y; ++j) {
  166.             for(int k =0; k < z; ++k)
  167.                 array[i][j][k] = (i + 1) * 100 + (j + 1) * 10 + k + 1;
  168.         }
  169.     }
  170.    
  171.     printf("\n\tA[%d][%d][%d] =\t[", x, y, z);
  172.     for(int i = 0; i < x; ++i) {
  173.         if(i > 0)
  174.             printf(";\n\t\t\t\t\t ");
  175.        
  176.         for(int j = 0; j < y; ++j) {
  177.             if(j > 0)
  178.                 printf(", ");
  179.            
  180.             printf("[");
  181.             for(int k = 0; k < z;++k)
  182.             {
  183.                 if(k > 0)
  184.                     printf(", ");
  185.                
  186.                 printf("%d", array[i][j][k]);
  187.             }
  188.             printf("]");
  189.         }
  190.     }
  191.     printf("]\n");
  192.    
  193.     pause();
  194. }
  195.  
  196. int main()
  197. {
  198.     int opt;
  199.     char buffer[30];
  200.    
  201.     while(true) {
  202.         clear();
  203.    
  204.         puts("\n\tArray type option:");
  205.         puts("\t1 - 1D");
  206.         puts("\t2 - 2D");
  207.         puts("\t3 - 3D");
  208.         puts("\t4 - Exit");
  209.         printf("\n\tYour choice: ");
  210.         fgets(buffer, sizeof(buffer), stdin);
  211.         sscanf(buffer, "%d", &opt);
  212.        
  213.         switch(opt)
  214.         {
  215.             case 1:
  216.                 oneD();
  217.                 break;
  218.        
  219.             case 2:
  220.                 twoD();
  221.                 break;
  222.            
  223.             case 3:
  224.                 threeD();
  225.                 break;
  226.        
  227.             case 4:
  228.                 puts("\n\tSee you next time...");
  229.                 exit(0);
  230.                 break;
  231.        
  232.             default:
  233.                 puts("\n\t[Error]: Invalid choice");
  234.                 break;
  235.         }
  236.     }
  237.    
  238.     return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment