BijonDurjoy

Problem 1

Sep 24th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. /* Write a program to insert new value in the array at desired position.
  2. And print the array. Follow the sample input and output to get your concept cleared.
  3. Input: 5
  4. 10 20 30 40 50
  5. 77 3
  6. Output: 10 20 77 30 40 50
  7. */
  8. #include<stdio.h>
  9. int main()
  10. {
  11. int n,i;
  12. scanf("%d", &n);
  13. int ara[n];
  14.  
  15. ///INPUTING ARRAY
  16.  
  17. for(i=0; i<n; i++)
  18. {
  19. scanf("%d", ara[i]);
  20. }
  21.  
  22. ///ADD AN ELEMENT
  23.  
  24. int index, numb;
  25. scanf("%d %d", &index, &numb);
  26. int ara2[n+1];
  27.  
  28. for(i=0; i<index; i++)
  29. {
  30. ara2[i]= ara[i];
  31. }
  32. ara2[index] = numb;
  33. for(i= index+1; i<n+1; i++)
  34. {
  35. ara2[i] = ara[i-1];
  36. }
  37.  
  38. ///PRINTING ARRAY
  39. for(i=0; i<n+1; i++)
  40. {
  41. printf("%d ", ara2[i]);
  42. }
  43. printf("\n");
  44.  
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment