实验7 综合练习

一、填空:阅读下列程序说明和程序,在可选答案中,挑选一个正确答案。填补(1) (2) (3) (4)处空白,并注释说明为什么。
程序说明
求 1 + 2/3 + 3/5 + 4/7 + 5/9 + … 的前15项之和。 
运行示例:
sum = 8.667936

/*求 1 + 2/3 + 3/5 + 4/7 + 5/9 + … 的前15项之和。*/
#include<stdio.h>
void main()
{
    int i,b=1;
    double s;
    s=0;                   /*  把s 赋值为0  */         
    for(i=1;i<=15;i++)
    {
        s=s+double(i)/double(b);    /*输出不为整数,需用double定义*/
        b=b+2;        /*每次循环b都比上一次大2*/
    }
    printf("sum=%f
",s);    /*输出结果*/
}

二、填空:阅读下列程序说明和程序,在可选答案中,挑选一个正确答案。填补(1) (2) (3) (4)处空白,并注释说明为什么。。
【程序说明】
输入10个整数,将它们从大到小排序后输出。 
运行示例:
Enter 10 integers: 1 4 -9 99 100 87 0 6 5 34
After sorted: 100 99 87 34 6 5 4 1 0 -9

#include<stdio.h>
void main()
{
    int i, j, t, a[10];                  /*定义一个数组a*/
    printf("Enter 10 integers:");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);   /*将输入的数依次赋给数组的10个元素*/
    for(i=1;i<10;i++)
        for(j=0;j<10-i;j++)    /*下标从0开始,因此初始给j赋值为0,一共有10个数,因此j<10-i*/
            if(a[j]<a[j+1])   /*判断下标为j和j+1的数的大小,若a[j]<a[j+1]则进行交换*/
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
            printf("After sorted:");
                for(i=0;i<=10;i++)
                    printf("%d ",a[i]);
                printf("
");
}

三、编程,输入x后,根据下式计算并输出y值。

#include<stdio.h>                   
#include<math.h>                  /*程序中调用了数学函数,需包含头文件math.h*/
int main(void)
{
    double x,y;                      /*定义两个双精度浮点型变量*/
    printf("Enter x:");
    scanf("%lf",&x);
    if(x<-2){                       /*分断计算函数*/
        y=x*x;
    }
    else if(x>2){
        y=sqrt(x*x+x+1);              /*调用平方根函数*/
    }           
    else{
        y=x+2;
    }

    printf("y=%.2f
",y);

    return 0;
}

四、编写程序,输入一批学生的成绩,遇0或负数则输入结束,要求统计并输出优秀(大于85)、通过(60~84)和不及格(小于60)的学生人数。

运行示例:

Enter scores: 88 71 68 70 59 81 91 42 66 77 83 0

>=85:2

60-84:7

<60   : 2

/*输入一批学生的成绩,遇0或负数则输入结束,
要求统计并输出优秀(大于85)、通过(60~84)和不及格(小于60)的学生人数。*/

#include<stdio.h>
int main(void)
{
    int good,pass,failure;         /*定义good(大于85)为优秀,通过为pass(60~84),
                                   不及格为failure(小于60)*/
    double scores;                 

    good=0;                       /*计数清空为0*/
    pass=0;
    failure=0;

    printf("Enter scores:");      /*输入成绩*/
    scanf("%lf",&scores);

    while(scores>0){              /*当输入成绩大于0时,执行循环*/
        if(scores<60)
            failure++;              /*计数*/
        else if(scores>=85)
            good++;
        else
            pass++;
        scanf("%lf",&scores);        
    }
                                      /*输出结果*/
    printf(">=85:%d
",good);
    printf("60-84:%d
",pass);
    printf("<60:%d
",failure);

    return 0;
}

 

原文地址:https://www.cnblogs.com/simple9495/p/3405744.html