c语言实验报告五华水,华水《C语言程序设计》实验报告 实验五.doc

华水《C语言程序设计》实验报告 实验五

《C语言程序设计》实验报告

实验五 函数与编译预处理

学号 姓名

一、实验目的

掌握函数定义及调用的方法,正确理解函数调用时实参和形参的对应关系;

掌握并正确使用数组作为函数参数;

掌握函数的嵌套调用和递归调用的方法;

理解变量的作用域和生存期;

掌握宏定义、文件包含。

二、实验内容

1、分析下面的程序并上机运行程序,要求写出3组以上程序的运行结果。

#include

int fun(int k)

{

int result=1,i;

for(i=1;i<=k;i++)

{

result=result*i;

}

return result;

}

void main()

{

int n;

printf("Input n please(1<=n<=10)");

scanf("%d",&n);

printf("%d!=%d\n",n,fun(n));

}

『运行结果:』取n=2:

取n=5:

取n=6:

2、编写程序,输出2~n之间的所有素数,n由键盘输入。要求判断素数的过程要写在函数中,由主函数调用实现,并写出2组以上程序运行结果。

程序代码:

#include

#include

#include

void prime()

{ int i,j,k,n,m=0;

printf("input n please!\n");

scanf("%d",&n);

for(i=2;i<=n;i++)

{ j=sqrt(i);

for(k=2;k<=j;k++)

if(i%k==0)

break;

if(k>j)

{ m++;

printf("%3d",i);

if(m%10==0)

printf("\n");

}

}

}

void main()

{ prime();

}

『运行结果:』

3、编程实现输入8个学生某门功课的成绩,用函数求出平均分,并在主函数中输出平均分以及高于平均分的同学的成绩。并写出2组以上程序运行结果。

程序代码:

#include

#define NUM 8

float average(float x[])

{float sum=0.0;

int i;

for(i=0;i

sum=sum+x[i];

return(sum/NUM);

}

void main()

int j,k;

float aver,cj[NUM];

printf("input score of student:\n");

for(j=0;j

scanf("%f",&cj[j]);

aver=average(cj);

for(k=0;k

if(cj[k]>aver)

printf("average=%6.2f\nk=%6.2f\n",aver,cj[k]);

运行结果:』

第一组:

第二组:

daxie, food and extorting money. In June, Yu Qing Zhi get Japanese lived with three areas of intelligence troops quietly to Mao Ta Xiang, scattered between lurking in the mulberry House. This evening, the puppet of the boats docked in Sheng Tang Kong, over more than 50 landing elves, went to Grand Rapids bridge at dusk, all exposed in the pale moonlight. Yu gave the order, the machine gun, rifle volley, elves, rattled off the bridge back, and

daxie, food and extorting money. In June, Yu Qing Zhi get Japanese lived with three areas of intelligence troops quietly to Mao Ta Xiang, scattered between lurking in the mulberry House. This evening, the puppet of the boats docked in Sheng Tan

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值