fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int SIZE,LEN=0;
  5.  
  6. int * InsertAfterMatchedValue(int *a,int x,int y)
  7. {
  8. for(int i=0; i<LEN; i++)
  9. {
  10. if(a[i]==x)
  11. {
  12. for(int j=LEN-1; j>=i; j--)a[j+1]=a[j];
  13. a[i+1]=y;
  14. LEN++;
  15. i++;
  16.  
  17. }
  18. if(LEN>SIZE){
  19. SIZE*=2;
  20. a=(int*)realloc(a,SIZE*sizeof(int));
  21. printf("array size doubled\n");
  22. }
  23. } return a;
  24. }
  25.  
  26. void printDynamicArray(int a[])
  27. {
  28. for(int i=0; i<LEN; i++)
  29. {
  30. printf("%d ",a[i]);
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. SIZE=9;
  37. int* a = (int*)malloc(sizeof(int)*SIZE);
  38. a[0]=3;
  39. a[1]=2;
  40. a[2]=2;
  41. a[3]=3;
  42. a[4]=2;
  43. a[5]=1;
  44. a[6]=3;
  45. a[7]=3;
  46. a[8]=3; ///{3,2,2,3,2,1,3,3,3}
  47. LEN=SIZE;
  48.  
  49. int Val1,Val2;
  50. scanf("%d%d",&Val1,&Val2);
  51. a=InsertAfterMatchedValue(a,Val1,Val2);
  52.  
  53. printDynamicArray(a);
  54. }
  55.  
  56.  
  57.  
  58.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
3 2 2 3 2 1 3 3 3