C程序设计课程-第十一次实验任务

来源:互联网 发布:达内 培训班java 编辑:程序博客网 时间:2024/05/03 02:24

作业1:
下段程序循环体执行的次数是是多少,为什么

#include <stdio.h>int main(){int i=1,n=0,num = 0;while (n<=2*i) { n=i*i; i=i+1;num++;} return 0;}

    3次.第一次循环后n=1,i=2,num=1,1<4,继续;第二次后,n=4,i=3,num=2,4<6,继续;第三次后,n=9,i=4,num=3,9>8,终止。


----------------------分割线----------------------
作业2:
阅读下面程序,在???填上适当的语句,使程序完成指定的功能。并说明为什么? 
程序说明:是用公式 求π的近似值,直到最后一项的绝对值小于是10^-5 为止。 

#include "stdio.h"#include "math.h" main() {int s=1; float n=1,t=1,pi=0; while( ??? ) { pi=pi+ ???    ; n=     ???   ; s=-s; t=s/n; } pi=pi*4; printf("pi=%f\n",pi); } 

补完后:

#include "stdio.h"#include "math.h" void main() {int s=1; float n=1,t=1,pi=0; while( fabs(t)>=pow(10.0,-5) ) { pi=pi+t; n=n+2; s=-s; t=s/n; } pi=pi*4; printf("pi=%f\n",pi); } 



----------------------分割线----------------------
作业3:
阅读下面程序,在???填上适当的语句,使程序完成指定的功能。并说明为什么? 
程序说明:冒泡对十个数按升序排序程序

#include "stdio.h"main() { static int a[10]={12,23,14,5,6,1,0,10,9,7}; int i,j,t; for(j=0;j<???;j++) for(i=0;i<9-j;i++) if(???) { t=a[i];a[i]=a[i+1];a[i+1]=t;} for(i=0;i<10;???) printf("%5d ",???); } 

补完后:

#include "stdio.h"void main() { static int a[10]={12,23,14,5,6,1,0,10,9,7}; int i,j,t; for(j=0;j<9;j++) for(i=0;i<9-j;i++) if(a[i]>a[i+1]) { t=a[i];a[i]=a[i+1];a[i+1]=t;}for(i=0;i<10;i++) printf("%5d ",a[i]); } 



----------------------分割线----------------------
作业4:程序运行后的输出结果是什么,为什么?

#include  <string.h>#include <stdio.h>struct STU { char  name[10];int   num;};void f(char *name, int  num){ struct STU  s[2]={{"SunDan",20044},{"Penghua",20045}};num = s[0].num;strcpy(name, s[0].name);}main(){ struct STU  s[2]={{"YangSan",20041},{"LiSiGuo",20042}},*p;p=&s[1];   f(p->name, p->num);printf("%s  %d\n", p->name, p->num);}

 

SunDan    20042


----------------------分割线----------------------
作业5:程序运行后的输出结果是什么,为什么?

#include  <string.h>#include <stdio.h>struct STU { char  name[10];    int  num;    float  TotalScore;  };void f(struct STU  *p){ struct STU  s[2]={{"SunDan",20044,550},{"Penghua",20045,537}}, *q=s;++p ;  ++q;  *p=*q;}main(){ struct STU  s[3]={{"YangSan",20041,703},{"LiSiGuo",20042,580}};f(s);printf("%s  %d  %3.0f\n", s[1].name, s[1].num, s[1].TotalScore);}


 Penghua    20045    537