Advertisement
nguyenhappy92

Xóa tất cả các số âm trong mảng các số thực

Dec 8th, 2015
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // Xoa cac phan tu am trong mang so thuc
  2. // Khai bao cac ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5.  
  6. #define SIZE 100
  7.  
  8. void nhap(float a[],int &n)
  9. {
  10. scanf("%d",&n);
  11. float x;
  12. for(int i=0;i<n;i++)
  13. {
  14. scanf("%f",&x);
  15. a[i]=x;
  16. }
  17. }
  18. void xoaPT(float a[],int &n,int k)
  19. {
  20. // k la vi tri xoa
  21. for(int i=k;i<n;i++)
  22. {
  23. a[i]=a[i+1];
  24. }
  25. n--;
  26. }
  27. float ammang(float a[],int n)
  28. {
  29. for(int i=0;i<n;i++)
  30. {
  31. if(a[i]<0)
  32. return a[i];
  33. }
  34. return 0;
  35. }
  36. void xoaAm(float a[],int &n)
  37. {
  38. for(int i=0;i<n;i++)
  39. {
  40. if(a[i]<0)
  41. {
  42. xoaPT(a,n,i);
  43. i--;
  44. }
  45. }
  46. }
  47. void xuat(float a[],int n)
  48. {
  49. for(int i=0;i<n;i++)
  50. {
  51. printf("%.2f",a[i]);
  52. }
  53. }
  54. void main()
  55. {
  56. float a[SIZE];
  57. int n;
  58. nhap(a,n);
  59. xoaAm(a,n);
  60. xuat(a,n);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement