设为首页 加入收藏

TOP

计算机二级C语言上机真题(50)【附详解】
2012-11-05 12:46:32 】 浏览:432
Tags:计算机 二级 语言 上机 真题 详解
【真题1】 给定程序中,函数fun 的功能是:计算出带有头结点的单向链表中各结
点数据域之和作为函数值返回。
#include < stdio.h >#include < stdlib.h >#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *);
void outlist(SLIST *);
int fun( SLIST *h)
{ SLIST *p; int s=0;
p=h- >next;
while(p)
{
/**********found**********/
s+= p- >___1___;
/**********found**********/
p=p- >___2___;
}
return s;
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48};
head=creatlist(a); outlist(head);
/**********found**********/
printf("\nsum=%d\n", fun(___3___));
}
SLIST *creatlist(int a[])
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i< N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q- >data=a[i]; p- >next=q; p=q;
}
p- >next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h- >next;
if (p==NULL) printf("The list is NULL!\n");
else
{ printf("\nHead ");
do
{ printf("- >%d", p- >data); p=p- >next; }
while(p!=NULL);
printf("- >End\n");
}
}
(2011年2月)
解析: 本题考查的是链表。
在函数fun 中,通过指针p遍历链表的各个结点,直到其为NULL,如果其不
为NULL,则将其data域的值累加到s 上去,并将p指向其后续结点,故第一
空应为data,第二空为next;第三空的目的是调用函数fun,由于函数fun的
形参为指向SLIST类型的指针,而head又是整个链表的头指针,故第三空应
填head。
答案:【1】data 【2】next 【3】head
【真题2】 给定程序modi.c 中函数fun的功能是:给一维数组a输入任意4 个整
数,并按下例的规律输出。例如输入1、2、3、4,程序运行后将输出以下方阵。
4 1 2 3
3 4 1 2
2 3 4 1
1 2 3 4
请改正函数fun 中指定部位的错误,使它能得出正确的结果。
注意:不要改动main 函数,
#include < stdio.h >#define M 4
/**************found**************/
void fun(int a)
{ int i,j,k,m;
printf("Enter 4 number : ");
for(i=0; i< M; i++) scanf("%d",&a[i]);
printf("\n\nThe result :\n\n");
for(i=M;i >0;i--)
{ k=a[M-1];
for(j=M-1;j >0;j--)
/**************found**************/
a[j]=a[j+1];
a[0]=k;
for(m=0; m< M; m++) printf("%d ",a[m]);
printf("\n");
}
}
main()
{ int a[M];
fun(a); printf("\n\n");
}
(2011年2月)
解析: 本题的考核点是用数组名作函数参数。
解题思路:本题通过双重循环逐行输出数组中的值。
程序中的第一个错误:本题考的是用数组名作函数参数,此时实参与形参都应
用数组名(或用数组指针),所以此处应当改为fun(int a[4])或fun(int *a)
等;
程序中的第二个错误:a[j]=a[j+1]是将数组中下标为j的下一个值赋给a[j],
而根据本题题意,是将前一个值赋给a[j],所以应当改为a[j]=a[j-1];。
【真题3】 某学生的记录由学号、8 门课程成绩和平均分组成,学号和8门课程的
成绩已在主函数中给出。请编写函数fun,它的功能是:求出该学生的平均分放在记
录的ave 成员中。请自己定义正确的形参。
例如,学生的成绩是:85.5,76,69.5,85,91,72,64.5,87.5,他的平均分应当是:
78.875。
注意:部分源程序存在文件prog.c 中。
请勿改动主函数main 和其他函数中的任何内容,仅在函数fun 的花括号中填入你编
写的若干语句。
#include < stdio.h >#define N 8
typedef struct
{ char num[10];
double s[N];
double ave;
}STREC;
void fun(STREC *p)
{……}
main()
{ STREC s={"GA005",85.5,76,69.5,85,91,72,64.5,87.5};
int i;
fun(&s);
printf("The %s's student data:\n",s.num);
for(i=0;i< N;i++)
printf("%4.1f\n",s.s[i]);
printf("\nave=%7.3f\n",s.ave);
}
(2011年2月)
解析: 本题的考核点是计算平均分算法。
提示思路:先统计8 门课程的总分数,然后再除8,求学生的平均分。
void fun(STREC *p)
{ int i;
double aver=0; /*将平均分赋初值0*/
for (i=0;i< N;i++)
{aver=aver+(*p).s[i]; } /*求和*/
(*p).ave=aver/N; /*计算平均值*/
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇计算机二级C语言上机真题(51)【.. 下一篇计算机二级C语言上机真题(49)【..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目