当前位置:编程学习 > C/C++ >>

c语言 数组以及指针问题:3. 编写一个程序实现在一个一维数组中,删除指定的数据,删除数据用函数实现。

头疼……还有个菜鸟问题:用for(i=0;i<=80;i++)scanf("%d",&a[i]);给数组赋值的时候打回车就是不进行下面的语句,而用while赋值就会把80个全赋成乱码……下面是我的程序 //3. 编写一个程序实现在一个一维数组中,删除指定的数据,删除数据用函数实现。 //如数组中的数据是2,4,5,12,6,4,删除"4"后的数组为2,5,12,6。 #include<stdio.h> int main() { int i,a[80],n,x; void cancle(int p[ ],int n); printf("Enter the number of your data:\n"); x=getchar(); printf("Enter your data:\n"); for(i=0;i<x;i++) scanf("%d",&a[i]); printf("Enter the number you manna cancle:\n"); scanf("%d",&n); cancle(a,n); for(i=0;i<=80;i++) printf("%d,",a[i]); return 0; } void cancle(int p[ ],int n) { int i,j; for(i=0;i<=80;i++){ if(p[i]==n){ p[i]=p[i+1]; } } }
答案:#include<stdio.h>
#define N  10
int main()
{
	int i,a[N]={ 0 },n,x;//这里,把数组初始化一下
	void cancle(int p[ ],int n);
	printf("Enter the number of your data:\n");
	x=getchar()-'0';//getchar()读取的是字符,要转化成整数要减去'0'也就是48
	printf("Enter your data:\n");
	for(i=0;i<x;i++)
	{
                 printf("Please put in a[%d] element",i);//加一个这个好看些。
		scanf("%d",&a[i]);
		getchar(  ); //这里要添加一个getchar,用来吃掉多余的'\n'
	}
	printf("Enter the number you manna cancle:\n");
	scanf("%d",&n);
	cancle(a,n);
	for(i=0;i<N;i++)
		printf("%d,",a[i]);
	return 0;
}
void cancle(int p[ ],int n)
{
	int i,j;
	for(i=0;i<N;i++){//这里是小于,不是小于等于,因为索引是从0开始的,所以数组是a[0]到a[N-1]
		if(p[i]==n){
			j=i; 
			while( j<N -1)
			{
				p[j]=p[j+1];
				j++; 
			}//这个while循环用来把数组剩下的元素往前挪。
		}
	}

}
其他:呵呵,我用过,我用在人脸识别的计算上了,因为效率要求比较高需要在25ms秒内完成较为复杂的图像检测和图像识别,所以用了非安全代码,不过后来水平提高了,我就把非安全代码修改为安全代码了,而且效率比以前的还略高一些,这个反正就是通过工具可以提高效率,通过思想也可以提高效率,

呵呵,希望我的经历对你有用。
 帮你改下了下,可以运行了:但你编的函数达不到效果:
#include<stdio.h>
int main()
{
	int i,a[80],n,x;
	void cancle(int p[ ],int n,int x);
	printf("Enter the number of your data:\n");
	scanf("%d",&x);
	printf("Enter your data:\n");
	for(i=0;i<x;i++)
		scanf("%d",&a[i]);
	printf("Enter the number you manna cancle:\n");
	scanf("%d",&n);
	cancle(a,x,n);
	for(i=0;i<x-1;i++)
	printf("%d,",a[i]);
	return 0;
}
void cancle(int p[ ],int n,int x)
{
	int i,j;
	for(i=0;i<x;i++){
		if(p[i]==n){
			p[i]=p[i+1];
	}
	}
	
} 

上一个:跪求电脑高手解决C语言编程问题
下一个:求C语言高手解答error C267: 'Tdelay': requires ANSI-style prototype问题!

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,