#include <stdio.h>
void input (int arr[], int n) { /*输入数组*/
int i;
printf ("请为数组x输入%d个元素:\n",n);
for (i=0; i<n; i++)
scanf ("%d", &arr[i]);
printf ("数组x输入完毕!\n");
}
int del (int arr[], int n, int delInfo[], int delIndex[]) { /*删除数组元素*/
int i,j,k=0;
for (i=0; i<n; i++) {
if ( (arr[i]>10 && arr[i]<20) || arr[i]==30) {
delIndex[k]=i; /*将欲删除的元素和位置存在二维数组中*/
delInfo[k]=arr[i];
k++;
for (j=i; j<n-k; j++) /*将欲删除元素之后元素全部向前移动一位*/
arr[j] = arr[j+1];
}
}
return k; /*返回删除的元素个数*/
}
void copy (int arr[],int arrTar[], int n) { /*复制数组*/
int i;
for (i=0; i<n; i++)
arrTar[i]=arr[i];
}
void print (int arr[], int n) { /*打印数组*/
int i;
for (i=0; i<n; i++)
printf ("%d\t", arr[i]);
printf ("\n");
}
int main (void) {
int x[20],y[20],delInfo[20],delIndex[20],dealCount;
input (x,20);
printf ("======================================================\n\n");
dealCount = del (x,20,delInfo,delIndex);
copy (x,y,20-dealCount);
printf ("数组x(已删除指定元素):\n");
print (x,20-dealCount);
printf ("\n");
printf ("数组y:\n");
print (y,20-dealCount);
printf ("\n");
printf ("数组x被删除的元素:\n");
print (delInfo,dealCount);
printf ("数组x被删除元素的位置:\n");
print (delIndex,dealCount);
printf ("======================================================\n\n");
getch (); /*屏幕暂留*/
return 0;
}
运行结果