Find a Value in Array and Remove It
Submitted by huynhmaianhkiet on Monday, September 2, 2013 - 20:16.
Language
C Programming exercise with array.
Find a value (number) in array and remove it.
Compilation is Dev-C ++.
Code:
Author
Huynh Mai Anh Kiet
www.mangbinhdinh.info - www.phimviet.us
http://www.facebook.com/huynhmaianh.kiet
- #include <stdio.h>
- #include <conio.h>
- int nhapmang(int a[100], int n)
- {
- int i;
- for(i=0;i<n;i++)
- {
- printf("Element %d: ",i);
- scanf("%d",&a[i]);
- }
- }
- int inmang(int a[100], int n)
- {
- int i;
- printf("Array: ");
- for(i=0;i<n;i++)
- {
- printf("%4d",a[i]);
- }
- }
- int demsophantu(int x, int a[100], int n)
- {
- int d=0, i;
- for(i=0;i<n;i++)
- {
- if(a[i]==x)
- {
- d++;
- }
- }
- return d;
- }
- int main()
- {
- int a[100],d,x,n,i,j;
- A:
- printf("\n\nNumber of array elements: ");
- scanf("%d",&n);
- nhapmang(a,n);
- inmang(a,n);
- printf("\nEnter a value wants count and delete: ");
- scanf("%d",&x);
- d=demsophantu(x,a,n);
- printf("%d have %d in array.\n",x,d);
- for(i=0;i<n;i++)
- {
- if(a[i]==x)
- {
- for(j=i;j<n;j++)
- {
- a[j]=a[j+1];
- }
- i=i-1;
- }
- }
- printf("Array after deleteing %d: ",x);
- for(i=0;i<n-d;i++)
- {
- printf("%4d",a[i]);
- }
- getch();
- goto A;
- }
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
- 62 views