c语言程序设计实验题答案 (9),C语言程序设计及实验指导练习及习题参考答案(8--10)...

int main(void) {

int i, n;

double math_ave, english_ave, computer_ave; struct student s1; /* 定义结构变量 */ printf(\scanf(\

printf(\\\n”); math_ave = english_ave = computer_ave = 0; for(i = 1; i <= n; i++){

printf(\

scanf(\math_ave = s1.math; english_ave = s1.english; computer_ave = s1.computer; }

printf(\computer_ave); return 0; }

9-6 定义一个包含5 名学生信息的结构数组,并对该结构数组的所有元素进行则始化。 解答:

struct student{

int num;

char name[10];

int computer, english, math; double average; };

struct student s[5]={{30101, \张一\李二\王三\赵四\刘五\9-7 参考例9-3,输入并保存10 个学生的成绩信息,分别输出平均成绩最高和最低的学生信息。 解答:

#include

struct student{ /*学生信息结构定义*/

int num; /* 学号 */

char name[10]; /* 姓名 */

int computer, english, math ; /* 三门课程成绩 */ double average; /* 个人平均成绩 */ };

struct student students[10]; /* 定义结构数组 */ int main(void) {

int i, max,min;

/* 输入10 个学生的记录*/ for(i = 0; i < 10; i++){

printf(\

scanf(\&students[i].english, &students[i].computer);

students[i].average=(students[i].math+students[i].english+students[i].computer)/3.0; }

/* 查找平均成绩最高、最低分学生的数组下标值 */ max=min=0;

for( i = 1; i < 10; ++i ){

if(students[i].average > students[max].average) max=i; if(students[i].average < students[min].average) min=i; }

/*输出平均成绩最高和最低的学生信息*/

printf(\最高分学生信息:学号:%d,姓名:%s,数学:%d,英语:%d,计算机:%d,平均分:%.2lf \\n\students[max].computer, students[max].average);

printf(\最低分学生信息:学号:%d,姓名:%s,数学:%d,英语:%d,计算机:%d,平均分:%.2lf \\n\students[min].computer, students[min].average);

return 0; }

9-8 定义一个struct student 类型的结构指针,用其实现一个学生信息的输入和输出。 解答:

struct student{ /*学生信息结构定义*/

int num; /* 学号 */

char name[10]; /* 姓名 */

int computer, english, math ; /* 三门课程成绩 */ double average; /* 个人平均成绩 */ }s, *p; p = &s;

scanf(\9-9 改写例9-4 中的函数update_score(),将第一个形参改为结构数组形式。 解答:

int update_score(struct student s[ ], int n, int num, int course, int score) {

int i,pos;

for(i = 0; i < n; i++) /* 按学号查找 */ if(s[i].num == num)

break;

if(i < n) /* 找到,修改成绩 */ {

switch(course){

case 1: s[i].math = score; break;

case 2: s[i].english = score; break; case 3: s[i].computer = score; break; }

pos = i; /* 被修改学生在数组中的下标 */ }

else /* 无此学号 */

pos = -1; return pos; }

2 习题参考答案

一、选择题

1.下面定义结构变量的语句中错误的是 D 。

A.struct student{ int num; char name[20]; } s; B.struct { int num; char name[20]; } s;

C.struct student{ int num; char name[20]; }; struct student s; D.struct student{ int num; char name[20]; }; student s; 2.如果有定义语句:

struct {int x, y; } s[2] = { { 1, 3 }, { 2, 7 } };

则语句:printf(“%d\\n”, s[0].y/s[1].x ); 输出结果为 B 。 A.0 B.1 C.2 D.3

3.根据下面的定义,能打印出字母M 的语句是 C 。 struct person{ char name[10];

int age; } c[10] = { “John”, 17, “Paul”, 19, “Mary”, 18, “Adam”, 16 }; A.printf(“%c”, c[3].name); B.printf(“%c”, c[3].name[1]); C.printf(“%c”, c[2].name[0]); D.printf(“%c”, c[2].name[1]); 4.设有如下定义,则对data 中的a 成员的正确引用是 B 。 struct sk{ int a; float b; } data, *p=&data; A.(*p).data.a B.(*p).a C.p->data.a D.p.data.a

5.对于以下结构定义,(*p)->str++中的++加在 D 。 struct { int len; char *str; } *p; A.指针str 上 B.指针p 上 C.str 指向的内容上 D.语法错误

二、填空题

1.“.”称为 成员(分量) 运算符,“->”称为 指向 运算符。 2.完成下列程序,该程序计算10 名学生的平均成绩。 #include #include struct student { int num;

char name[20]; int score;

};

struct student stud[10]; int main(void) { int i , sum = 0 ;

for(i = 0; i < 10; i++){

scanf(\&stud[i].num, stud[i].name , &stud[i].score); sum += stud[i].score; }

printf(\return 0;

}

3.下列程序读入时间数值,将其加1 秒后输出,时间格式为:hh: mm: ss,即小时:分钟: 秒,当小时等于24 小时,置为0。 #includestruct {

int hour, minute, second; } time;

int main(void)

{ scanf(\time.second++;

if( time.second == 60){ time.minute++ ; time.second = 0;

if(time.minute == 60){ time.hour++; time.minute = 0; if( time.hour == 24 ) time.hour = 0; } }

printf (\return 0; }

4.写出下面程序的运行结果 1 2 A B struct s1{ char c1, c2; int n; };

struct s2{ int n;

struct s1 m;

} m = {1, {?A?, ?B?, 2} }; int main(void)

{ printf(“%d\\t%d\\t%c\\t%c\\n”, m.n, m.m.n, m.m.c1, m.m.c2);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值