下列给定程序中,函数fun()的功能是:从s所指字符串中,找出t所指字符串的个数作为函数值返回。例如,当s所指字符串中的内容为abcdabfab,t所指字符串的内容为ab,则函数返回整数3。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构.
试题程序:
include <conio.h>
include <stdio.h>
include <string.h>
int fun (char *s, char *t)
{int n; char *p, *r;
n=0;
while(*s)
{p=s;
r=t;
while (*r)
/**************found**************/
if(*r==*p) {r++; p++}
else break;
/*************found**************/
if(r=='\0')
n++;
s++;
}
return n;
}
main()
{char s[100], t[100]; int m;
clrscr();
printf("\nPlease enter string s: ");
scanf ("%s",s);
printf("\nPlease enter substring t: ");
scanf ("%s",t);
m=fun (s,t);
printf("\nThe result is: m=%d\n", m);
}
第1题
使数组左下半三角元素中的值全部置成0。例如a数组中的值为
a=1 9 7
2 3 8
4 5 6
则返回主程序后a数组中的值应为
0 9 7
0 0 8
0 0 0
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <conio.h>
include <stdio.h>
include <stdlib.h>
define N 5
int fun (int a[] [N])
{
}
main()
{
int a[N] [N],i,j;
clrscr();
printf("*****The array*****\n");
for(i=0;i<N;i++)
/*产生—个随机的5*5矩阵*/
{ for(j=0;j<N;j++)
{a[i][j]=rand()%10;
printf("%4d", a[i][j]);
}
printf("\n");
}
fun (a);
printf("THE RESULT\n");
for(i=0;i<N;i++)
{ for(j=0;j<N;j++)
printf("%4d",a[i][j));
printf("\n");
}
}
第2题
到大的顺序保存在数组b中。矩阵的维数在主函数中输入,并赋予随机数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
include<stdlib.h>
define N 20
void fun(【 】)
{
int i j;
int t;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(【 】)
b[i]=a[i][j];
for(i=0;i<n;i++)
{
for(j=0;i<n;j++)
if(【 】)
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
main()
{
int a[N][N];
int b[N];
int n;
int i,j;
clrscr();
printf("*****Input the dimension of array N*****\n");
scanf("%d",&n);
printf("*****The array *****\n");
for(i=0;i<n;i++)
{
for(j=0;i<n;j++)
{
a[i][j]=rand()%20;
while(a[i][j]==0)
a[i][j]=rand()%30;
printf("%4d",a[i][j]);
}
printf(”\n\n”);
}
for(i=0;i<n;i++)
b[i]=0;
fun(a,b,n);
printf("***** THE RESULT *****\n");
for(i=0;i<n;i++)
printf(“%d”,b[i]);
}
第3题
的元素。 N中存放的是数列中元素的个数。
请改正程序中的错误,使它能够得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include<stdio.h>
define N 20
fun (int *a,int n,int x)
{int p=0,i;
a[n]=x;
while (x!=a[p])
p=p+1;
if(p==n) return -1;
else
{for (i=p;i<n;i++)
/*************found**************/
a[i+1]=a[i];
return n-1;
}
}
main()
{int w[N]={-3,0,1,5,7,99,10,15,30,90},
x,n,i;
n=10;
printf("The original data :\n");
for (i=0;i<n;i++) printf("%5d",w[i]);
printf("\nInput x (to delete ): ");
scanf("%d",&x);
printf("Delete : %d\n",x);
n=fun(w,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",
w[i]);printf("\n\n");
}
}
第5题
的功能是:求出平均分,由函数值返回。
例如,若学生的成绩是85,76,69,91,72,64,87,则平均分应当是78.625。
注童:部分源程序给出如下.
请勿改动主函数main和其他函数中的任何内容,仅在函数6m的花括号中填入所编写的若干语句。
试题程序:
include<stdlib.h>
include<stdio. h>
define N 8
struct slist
{ double s;
struct slist *next;
};
typedef struct slist STREC;
double fun(STREC *h)
{
}
STREC *creat(double *s) /*创建链表*/
{
STREC *h,*p, *q;
int i=0;
h=p= ( STREC*)malloc (sizeof (STREC));
p->s=0;
while (i<N)
{q= ( STREC*)malloc (sizeof (STREC));
q->s=s Ii]; i++;p->next=q;p=q;
}
p->next=0;
return h;
}
outlist (STREC *h)
{
STREC *p;
p=h->next;
printf ("head ");
do
{printf ("->%4. if ",p->s);
p=p->next; /*输出各成绩*/
}
while (p !=0)
}
main ()
{
double s [N] ={ 85,76, 69, 85, 91,72, 64,
87},ave;
STREC *h;
h=creat (s);
outlist (h);
ave=fun (h);
printf ("ave=%6.3f\n ", ave);
}
第6题
的平方和并作为函数值返回给主函数中的S。例如:若a数组中的值为
a=0 1 2 7 9
1 11 21 5 5
2 21 6 11 1
9 7 9 10 2
5 4 1 4 1
则返回主程序后s的值应为310。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<stdio.h>
include<conio.h>
include<stdlib.h>
define N 5
int fun (int w[][N])
{
}
main()
{
int a[N)[N]={0,1,2,7,9,1,1l,2l,5,5,2, 21,6,11,1,9,7,9,10,2,5,4,1,4,1};
int i,j;
int S;
clrscr( );
printf("*****The array***+*\n");
for (i=0; i<N; i++)
{for(j=0;j<N;i++)
{printf(“%4d”,a[i][j]);}
printf("\n");
}
s=fun(a);
printf(“*****THE RESULT*****\n”);
printf("The sum is:%d\n",s);
}
第7题
下面程序的运行结果是______。
include<stdio.h>
main()
{ int y,a;
y=2,a=1;
while(y--!=-1)
{do{a*=y;a++;}while(y--);}
printf("%d,%d",a,y);}
第8题
(若该位置上不是字母,则不转换)。
例如,若输入abc4Efg,则应输出AbC4EfG。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<conio. h>
include<stdio. h>
void fun ( char *ss)
{
}
main ( )
{
char tt[51];
clrscr ();
printf("Please enter an character string
within 50 characters:\n");
gets (tt);
printf ("\n\nAfter changing, the string\n
%s", tt);
fun (tt);
printf("\nbecomes\n \%s",tt);
}
第9题
请补充函数fun(),该函数的功能是把数组aa中的奇数元素按原来的先后顺序放在原数组后面。
例如,输入“33,67,42,58,25,76,85,16,41,56”,输出结果“42,58,76,16,56,33,67,25,25,41。
注意:部分源程序给出如下。
请勿改动主函数main和具他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 10
void fun(int aa[])
{
int i, j=0,k 0;
int bb IN];
for (i 0; i<N; i++)
{
if (【 】)
bb[k++] aa[i];
else
aa [j++] =aa Ii];
for(i=0; i<k;【 】)
aa[j] bb[i];
}
main ( )
{
int i;
int aa[N]={33, 67, 42,58,25, 76, 85, 16,
41,56};
clrscr ();
printf("\n*** original list ***\n");
for (i=0; i<N; i++)
printf("%4d", aa[i]);
fun (aa);
printf("\n*** new list ***\n");
for (i=0; i<N; i++)
printf("%4d", aa[i]);
}
第10题
请补充main函数,该函数的功能是:求1+21+3!+…+N!的和。
例如, 1+2!+3!+...+5!+6!的和为873。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
main ( )
{
int i, n;
long s=0, t=l;
clrscr ();
printf ("\nInput n: \n");
scanf ("%d",【 】);
for (i=l; i<=n; i++)
{
t=【 】;
s=【 】;
}
printf ("1 ! +2 ! +3 !... +%d! =%ld\n", n, s);
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!