Advertisement
nguyenhappy92

Thêm một phần tử có giá trị x vào mảng tại vị trí k

Dec 7th, 2015
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. // Them vao gia tri x vao vi tri k
  2. // khai bao cac ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #define SIZE 100
  6.  
  7. void nhap(int a[],int &n)
  8. {
  9. scanf("%d",&n);
  10. for(int i=0;i<n;i++)
  11. {
  12. scanf("%d",&a[i]);
  13. }
  14. }
  15. void xuat(int a[],int n)
  16. {
  17. for(int i=0;i<n;i++)
  18. {
  19. printf("%4d",a[i]);
  20. }
  21. }
  22. void them(int a[],int &n,int k,int x)
  23. {
  24. // k la vi tri can them
  25. //x la gia tri can them vao vi tri k
  26. for(int i=n;i>k;i--)
  27. {
  28. a[i]=a[i-1];
  29. }
  30. a[i]=x;
  31. n++;
  32. }
  33. void main()
  34. {
  35. int a[SIZE],n;
  36. nhap(a,n);
  37. int x,k;
  38. scanf("%d%d",&x,&k);
  39. them(a,n,k,x);
  40. xuat(a,n);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement