3i Infotech Placement: Sample Questions 1091 - 1091 of 1245

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 1091

Edit

Describe in Detail

Essay▾

How would you dynamically allocate a one-dimensional and two-dimensional arrays of integer?

Explanation

  • We can create both statically and dynamically allocated array which can be both one-dimensional or multiple dimensional.
  • Statically allocated have to specify size before compilation.
  • However sometimes programmer doesn՚t know the size of the array ahead of time.
  • Dynamically allocated array solve this problem by allocating space on heap at runtime.
  • Provides library function to request heap memory at runtime.

One Dimensional (Dynamically Allocated)

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. intmain(intargc, char ⚹argv[])
  4. {
  5.     int ⚹piBuffer =NULL;
  6.     intnBlock =0;
  7.     intiLoop =0;
  8.     printf("Enter the number of blocks =");
  9.     scanf("%d",&nBlock);
  10.     piBuffer =(int ⚹)malloc(nBlock ⚹sizeof(int));
  11.     if(piBuffer == NULL)
  12.     {
  13.         return1;
  14.     }
  15.     for (iLoop =0 ; iLoop <nBlock ; iLoop++)
  16.     {
  17.         piBuffer[iLoop] =iLoop;
  18.     }
  19.     for (iLoop =0 ; iLoop <nBlock ; iLoop++)
  20.     {
  21.         printf("pcBuffer[%d] =%d", iLoop,piBuffer[iLoop]);
  22.     }
  23.     free(piBuffer);
  24.     return0;
  25. }

OUTPUT

Enter the number of block = 7

pcBuffer [0] = 0

pcBuffer [1] = 1

pcBuffer [2] = 2

pcBuffer [3] = 3

pcBuffer [4] = 4

pcBuffer [5] = 5

peBuffer [6] = 6

Two Dimensional Dynamically Allocated

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. intmain(intargc, char ⚹argv[])
  4. {
  5.     int ⚹⚹piBuffer =NULL;
  6.     intnRow =0;
  7.     intnColumn =0;
  8.     intiRow =0;
  9.     intiCol =0;
  10.     printf("Enter the number of Row =");
  11.     scanf("%d",&nRow);
  12.     printf("Enter the number of Column =");
  13.     scanf("%d",&nColumn);
  14.     piBuffer =(int ⚹⚹)malloc(nRow ⚹sizeof(int⚹));
  15.     if(piBuffer == NULL)
  16.     {
  17.         return1;
  18.     }
  19.     for (iRow =0 ; iRow <nRow ; iRow++)
  20.     {
  21.         piBuffer[iRow] =(int ⚹)malloc(nColumn ⚹sizeof(int));
  22.         if(piBuffer[iRow] == NULL)
  23.         {
  24.             return1;
  25.         }
  26.     }
  27.     for (iRow =0 ; iRow <nRow ; iRow++)
  28.     {
  29.         for (iCol =0 ; iCol <nColumn ; iCol++)
  30.         {
  31.             piBuffer[iRow][iCol] =3;
  32.         }
  33.     }
  34.     for (iRow =0 ; iRow <nRow ; iRow++)
  35.     {
  36.         for (iCol =0 ; iCol <nColumn ; iCol++)
  37.         {
  38.             printf("piBuffer[%d][%d] =%d",iRow, iCol,piBuffer[iRow][iCol]);
  39.         }
  40.     }
  41.     for (iRow =0 ; iRow <nRow ; iRow++)
  42.     {
  43.         free(piBuffer[iRow]);
  44.     }
  45.     free(piBuffer);
  46.     return0;
  47. }

OUTPUT

Enter the number of Row = 2

Enter the number of column = 2

piBuffer [0] [0] = 3

piBuffer [0] [1] = 3

piBuffer [1] [0] = 3

piBuffer [1] [1] = 3

Developed by: