简答题 C语言
题号 #2257052

假如整数数列中的数不重复,并存放在数组中。下列给定的程序中,函数proc()的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。
请修改程序中的错误,使它能够得出正确的结果。
注意:不要改动maill()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#define M 20
int proc(int*arr,int n,int x)
{
int p=0,i;
arr[n]=x:
while(x!=arr[1,])
p=p+1;
if(p==n)return-1;
else
{
for(i=P;i<n;i++)
//****found****
arr[i+1]=arr[i];
return n-1:
}
}
void main()
{
int arr[M]={-3,0,1,5,7,99,l0,15,30,90),
x,n,i;
n=10:
printf("The original data:\n");
for(i=0;i<n;i++)printf("%5d",arr[i]);
printf("\nInput x(to delete):");
scanf("%d",&x);
printf("Delete:%d\n",x);
n=proc(arr,n,x);
if(n==-1)
printf("***No be found!***\n\n");
else
{
printf("The data after deleted:\n");
for(i=0;i<n;i++)
printf("%5d",arr[i]);
printf("\n\n");
}
}

进入在线模考

参考答案

暂无

答案解析

错误:arr[i+1]=arr[i];
正确:arr[i]=arr[i+1];
【解析】题目中要求删除数列中值为x的元素,需要将数列中的每一个元素与指定数值x相比较。如果比较结果为第i个元素与x相等,则把第i个位置以后的元素前移一个位置,因此,“arr[i+1]=arr[i];”应改为“arr[i]=arr[i+1]”。

你可能感兴趣的试题

2

下列给定程序中,函数proc()的功能是:对M名学生的学习成绩,按从高到低的顺序找出前m(m≤l0)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填人所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#define M 10
typedef struct SS
{
char num[10];
int order;
}
STU:
STU*proc(STU a[],int m)
{
STU b[M],*tt;
int i,j,k;
【1】 ;
for(i=0:i<M:i++)
b[i]=a[i];
for(k=0;k<m;k++)
{
for(i=j=0;i<M;i++)
if(【2】 }
j=i;
tt[k]=b[j]:
b[j].order=0;
}
return 【3】 ;
}
void outresult(STU a[],FILE*pf)
{
int i;
for(i=0;i<M;i++)
printf(pf,"No=%S Mark=%d\n",
a[i].hum,a[i].order);
fprintf(pf,"\n\n");
}
void main()
{
STU stu[M]={{"A01",60),{"A02",90},
{"A03",88),{"A04",76),{"A05",63},
{"A06",69),{"A07",100),{"A08",87),
{"A09",66),{"A10",95));
STU*porder;
int i,m;
system("CLS");
printf("***The Origial data***\n");
outresult(stu,stdout);
print{("\nGive the numeber of the
students who have better score:");
scanf("%d",&m);
while(m>10)
{
printf("\nGive the number of the
studets who have better score:");
scan{("%d",&m);
}
porder=proc(stu,m);
print{("***THE RESULT***\n");
print{("***The top students***\n");
for(i=0;i<m;i++)
printf("%S%d\n",
porder[i].num,porder[i].order);
free(porder);
}

找试题 · 找答案 · 找资料 在线模考