Skip to main content

Find a Value in Array and Remove It

Language


C Programming exercise with array.
Find a value (number) in array and remove it.
Compilation is Dev-C ++.

Code:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. int nhapmang(int a[100], int n)
  4. {
  5. int i;
  6. for(i=0;i<n;i++)
  7. {
  8. printf("Element %d: ",i);
  9. scanf("%d",&a[i]);
  10. }
  11. }
  12. int inmang(int a[100], int n)
  13. {
  14. int i;
  15. printf("Array: ");
  16. for(i=0;i<n;i++)
  17. {
  18. printf("%4d",a[i]);
  19. }
  20. }
  21. int demsophantu(int x, int a[100], int n)
  22. {
  23. int d=0, i;
  24. for(i=0;i<n;i++)
  25. {
  26. if(a[i]==x)
  27. {
  28. d++;
  29. }
  30. }
  31. return d;
  32. }
  33. int main()
  34. {
  35. int a[100],d,x,n,i,j;
  36. A:
  37. printf("\n\nNumber of array elements: ");
  38. scanf("%d",&n);
  39. nhapmang(a,n);
  40. inmang(a,n);
  41. printf("\nEnter a value wants count and delete: ");
  42. scanf("%d",&x);
  43. d=demsophantu(x,a,n);
  44. printf("%d have %d in array.\n",x,d);
  45. for(i=0;i<n;i++)
  46. {
  47. if(a[i]==x)
  48. {
  49. for(j=i;j<n;j++)
  50. {
  51. a[j]=a[j+1];
  52. }
  53. i=i-1;
  54. }
  55. }
  56. printf("Array after deleteing %d: ",x);
  57. for(i=0;i<n-d;i++)
  58. {
  59. printf("%4d",a[i]);
  60. }
  61. getch();
  62. goto A;
  63. }

Author
Huynh Mai Anh Kiet
www.mangbinhdinh.info - www.phimviet.us
http://www.facebook.com/huynhmaianh.kiet

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.