Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Demo initializing dynamic size array in C
- * C99 standard
- * Author: Rifai M. Hanif
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- // Max supported array size: 1000
- #define MAX_SIZE 1000
- // clear console screen
- void clear()
- {
- #ifdef _WIN32
- system("cls");
- #else
- system("clear");
- #endif
- puts("\n\t===== DYNAMIC SIZE ARRAY =====");
- }
- // pause
- void pause()
- {
- printf("\n\tPress enter/return to continue... ");
- getchar();
- }
- // 1D array
- void oneD()
- {
- clear();
- puts("\n\t=== 1D ARRAY ===");
- int x = 0;
- bool invalid = false;
- char buffer[30];
- while(x < 1 || x > MAX_SIZE)
- {
- if(invalid)
- puts("\n\t[Error]: Size not supported!");
- printf("\n\tArray A size: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &x);
- invalid = x < 1 || x > MAX_SIZE;
- }
- int * array = malloc(x * sizeof(int));;
- for(int i = 0; i < x; ++i)
- {
- array[i] = i + 1;
- }
- printf("\n\tA[%d] = [", x);
- for(int i = 0; i < x; ++i) {
- if(i > 0)
- printf(", ");
- printf("%d", array[i]);
- }
- printf("]\n");
- pause();
- }
- // 2D array
- void twoD()
- {
- clear();
- puts("\n\t=== 2D ARRAY ===");
- int x = 0, y = 0;
- bool invalid = false;
- char buffer[30];
- while(x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE) {
- if(invalid)
- puts("\n\t[Error]: Size not supported!");
- printf("\n\tArray A rows count: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &x);
- printf("\tArray A columns count: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &y);
- invalid = x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE;
- }
- int ** array = (int *) malloc(x * sizeof(int *));
- for(int i = 0; i < x; ++i)
- array[i] = malloc(y * sizeof(int));
- for(int i = 0; i < x; ++i) {
- for(int j = 0; j < y; ++j)
- array[i][j] = (i + 1) * 10 + j + 1;
- }
- printf("\n\tA[%d][%d] =\t[", x, y);
- for(int i = 0; i < x; ++i) {
- if(i > 0)
- printf(";\n\t\t\t\t ");
- for(int j = 0; j < y; ++j) {
- if(j > 0)
- printf(", ");
- printf("%d", array[i][j]);
- }
- }
- printf("]\n");
- pause();
- }
- // 3D array
- void threeD()
- {
- clear();
- puts("\n\t=== 3D ARRAY ===");
- int x = 0, y = 0, z = 0;
- bool invalid = false;
- char buffer[30];
- while(x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE || z < 1 || z > MAX_SIZE) {
- if(invalid)
- puts("\n\t[Error]: Size not supported!");
- printf("\n\tArray A rows count: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &x);
- printf("\tArray A columns count: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &y);
- printf("\tArray A depth count: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &z);
- invalid = x < 1 || x > MAX_SIZE || y < 1 || y > MAX_SIZE || z < 1 || z > MAX_SIZE;
- }
- int *** array = (int **) malloc(x * sizeof(int **));
- for(int i = 0; i < x; ++i) {
- array[i] = (int *) malloc(y * sizeof(int *));
- for(int j = 0; j < y; ++j)
- array [i][j] = malloc(z * sizeof(int));
- }
- for(int i = 0; i < x; ++i) {
- for(int j = 0; j < y; ++j) {
- for(int k =0; k < z; ++k)
- array[i][j][k] = (i + 1) * 100 + (j + 1) * 10 + k + 1;
- }
- }
- printf("\n\tA[%d][%d][%d] =\t[", x, y, z);
- for(int i = 0; i < x; ++i) {
- if(i > 0)
- printf(";\n\t\t\t\t\t ");
- for(int j = 0; j < y; ++j) {
- if(j > 0)
- printf(", ");
- printf("[");
- for(int k = 0; k < z;++k)
- {
- if(k > 0)
- printf(", ");
- printf("%d", array[i][j][k]);
- }
- printf("]");
- }
- }
- printf("]\n");
- pause();
- }
- int main()
- {
- int opt;
- char buffer[30];
- while(true) {
- clear();
- puts("\n\tArray type option:");
- puts("\t1 - 1D");
- puts("\t2 - 2D");
- puts("\t3 - 3D");
- puts("\t4 - Exit");
- printf("\n\tYour choice: ");
- fgets(buffer, sizeof(buffer), stdin);
- sscanf(buffer, "%d", &opt);
- switch(opt)
- {
- case 1:
- oneD();
- break;
- case 2:
- twoD();
- break;
- case 3:
- threeD();
- break;
- case 4:
- puts("\n\tSee you next time...");
- exit(0);
- break;
- default:
- puts("\n\t[Error]: Invalid choice");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment