下列程序定义了N×N的二维数组,并在主函数中赋值。请编写一个函数fun(),函数的功能是:求数组周边元素的平方和并作为函数值返回给主函数。例如,若数组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, 11, 21, 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; j++)
{
printf("%4d", a[i][j]);
}
printf("\n');
}
s=fun(a);
printf("*****THE RESULT*****\n");
printf("The sum is: %d\n", s);
}
第1题
求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。
请改正程序指定部位的错误,使它能得到正确结果。
[注意] 不要改动main函数,不得增行或删行,也不得更改程序的结构。
[试题源程序]
include<stdio.h>
include<stdlib.h>
typedef struct aa
{
int data;
struct aa *next;
}NODE;
fun(NODE *h)
{
int max=-1;
NODE *p;
/***********found************/
p=h;
while(p)
{
if(p->data>max)
max=p->data;
/************found************/
p=h->next;
}
return max;
}
outresult(int s, FILE *Pf)
{
fprintf(pf, "\nThe max in link: %d\n", s);
}
NODE *creatlink(int n, int m)
{
NODE *h, *p, *s, *q;
int i, x;
h=p=(NODE *)malloc(sizeof(NODE));
h->data=9999;
for(i=1; i<=n; i++)
{
s=(NODE *)malloc(sizeof(NODE));
s->data=rand()%m; s->next=p->next;
p->next=s; p=p->next;
}
p->next=NULL;
return h;
}
outlink(NODE *h, FILE *pf)
{
NODE *p;
p=h->next;
fprintf(Pf, "\nTHE LIST:\n\n HEAD");
while(P)
{
fprintf(pf, "->%d", P->datA); p=p->next;
}
fprintf(pf, "\n");
}
main()
{
NODE *head; int m;
head=cteatlink(12,100);
outlink(head, stdout);
m=fun(head);
printf("\nTHE RESULT"\n");
outresult(m, stdout);
}
第2题
有以下程序:
include <stdio.h>
main()
{ char ch1,ch2;int n1,n2;
ch1=getchar();ch2=getehar();
n1=ch1-'0'; n2=n1*10+(ch2-'0');
printf("%d\n",n2);
}
程序运行时输入:12<回车>,执行后的输出结果是【 】。
第3题
和数。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构.
试题程序:
include <conio.h>
include <stdio.h>
define M 5
/**********************************/
iht fun(int n, int xx[ ][ ])
{ int i, j, sum=0;
printf("\nThe %d x %d matrix:\n",M,M);
for(i=0;i<M;i++)
{ for (j=0; j<M; j++)
/**********************************/
printf("%4f",xx[i][j]);
printf("\n");
}
for(i=0;i<n;i++)
sum+=xx[i][i]+xx[i][n-i-1];
return(sum);
}
main ()
{ int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0},
{6,7,8,9,0},{9,8,7,6,5},{3,4,5,6,7}};
clrscr();
printf("\nThe sum of all elements on 2
diagnals is %d",fun(M, aa));
}
第4题
到大的顺序排列,函数返回删除后数组中数据的个数。
例如,若一维数组中的数据是:
2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10
删除后,数组中的内容应该是:
2 3 4 5 6 7 8 9 10。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <stdio. h>
define N 80
int fun(int a[], int n)
{
}
main ( )
{
int a[N]={ 2,2,2,3,4,4,5,6,6,6,6,7,7,
8,9,9,10,10,10,10}, i, n=20;
printf ("The original data : \n");
for(i=0; i<n; i++)
printf ("%3d", a [i] );
n=fun (a, n);
printf("\n\nThe data after deleted
: \n");
for(i=0; i<n; i++)
printf ("%3d", a [i] );
printf ("\n\n");
}
第5题
下面程序的输出结果是【 】。
unsigned fun6(unsigned num)
{ unsigned k=1;
do { k*=hum%10;
num/=10;}while(num);
return(k); }
main()
{ unsigned n=26;
printf("%d\n",fun6(n));}
第6题
A,z转化为 a),其他字符不变。
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <stdio.h>
include <ctype.h>
include <conio.h>
void fun(char *s)
/*************found**************/
{ while(*s!='@')
{ if(*s>='A' &*s<='z'||*s>='a'&&*s<='z')
{if(*s=='Z') *S='A';
else if(*S=='z') *s='a';
else *s+=1;
}
/*************found**************/
(*s)++;
}
}
main()
{ char s[80];
clrscr();
printf("\n Enter a string with length <80:\n\n");gets(s);
printf("\n The string:\n\n");puts(s);
fun(s);
printf("\n\n The Cords:\n\n");puts(s);
}
第7题
一个字母,如果原来的字母为‘z’或‘Z’,则相应地转换成‘a’或‘A’,结果仍保存在原数组中。
例如,输入“StudentZz”,则输出“TuvefouAa”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
define N 80
void fun(char s[])
{
int i;
for(i=0;【 】;i++)
{
if(s[i]=='z'||s[i]='Z')
s[i]-=【 】;
else
s[i]+=【 】;
}
}
main()
{
char str[N];
clrscr();
printf("\n Input a string:\n");
gets(str);
printf("\n*** original string***\n");
puts(str);
fun(str);
printf("\n*** new string***\n");
puts(str);
}
第8题
值不大于100)。
例如:主函数从键盘给输入n的值为56,则输出为 sum=1113。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <stdio.h>
long fun(int n)
{
}
main()
{
int n;
long sum;
printf("Input n:");
scanf("%d",&n);
sum=fun(n);
printf("sum=%ld\n",sum);
}
第9题
全部删除,串中剩余字符所形成的一个新中放在t所指的数组中。
例如,若s所指字符串中的内容为ABCDEFG123456,其中字符A的ASCII码值为奇数,因此应当删除;其中字符B的 ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其他依此类推。最后t所指的数组中的内容应是246。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<conio.h>
include<stdio.h>
include<string.h>
void fun(char*s,char t[])
{
}
main()
{
char s[100],t[100];
clrscr();
printf("\nPlease enter string S:");
scanf("%s",s);
fun(s,t);
printf("\nThe result is:%s\n",t);
}
第10题
例如,若输入"Ab,cD",则输出"ab, cd"。
请改正函数fun()中的错误,使它能得出正确的结果。
注童:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <stdio.h>
include <string.h>
include <conio.h>
char *fun (char tt[])
{
int i;
for(i=0; tt[i]; i++)
/**************found**************/
{ if(('A' <=tt[i]||(tt[i]<='z' ))
tt[i]+=32; }
return(tt);
}
main()
{int i;
char tt[81];
clrscr();
printf("\nPlease enter a string:");
gets(tt);
printf("\nThe result string is:\n %s", fun(tt));
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!