Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Start by creating a dynamic array of size 5. Whenever the user enters too many numbers, grow the size of the array by 2

0 votes
asked Feb 28, 2020 by ch

when the 6th number is entered:

Create a new dynamic array of size 10

Transfer the existing 5 numbers

Delete the old array

2 Answers

0 votes
answered Mar 6, 2020 by FreeKill
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
  int *first = (int*) malloc(sizeof(int)*5);
  int *last ;
  int count=0,fact=5;
  while(1){
      scanf("%d",&first[count]);
      if((count+1)==fact){
          last = (int*) malloc(sizeof(int)*fact);
          memcpy(last,first,sizeof(int)*fact);
          fact*=2;
          free(first);
          first = (int*) malloc(sizeof(int)*fact);
          memcpy(first,last,sizeof(int)*fact);
          free(last);
      }
  }
}
0 votes
answered Mar 6, 2020 by Freekill

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main() {

int *first = (int*) malloc(sizeof(int)*5);

int *last ;

int count=0,fact=5;

while(1){

scanf("%d",&first[count++]);

if((count+1)==fact){

last = (int*) malloc(sizeof(int)*fact);

memcpy(last,first,sizeof(int)*fact);

fact*=2;

free(first);

first = (int*) malloc(sizeof(int)*fact);

memcpy(first,last,sizeof(int)*fact);

free(last);

}

}

}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...