当前位置:网站首页>C语言实验九 函数(一)
C语言实验九 函数(一)
2022-08-02 00:18:00 【Meteor.792】
一、实验目的
(一)掌握函数的定义、函数类型、函数参数、函数调用的基本概念;
(二)掌握变量名作函数参数的程序设计方法;
(三)掌握函数的嵌套调用的方法;
(四)掌握数组元素作函数参数;
(五)掌握数组名作函数参数的程序设计方法;
(六)掌握字符数组作函数参数的程序设计方法;
(七)了解全局变量、局部变量的概念和使用方法;
(八)使用功能键F7单步执行,使用Ctrl+F7观察变量的值,学会程序调试基本方法。
二、预习要求
(一)函数的定义、函数类型、函数参数、函数调用的基本概念;
(二)函数实参与形参的对应关系以及参数的传递;
(三)以变量名和数组名作函数参数时的使用方法;
(四)全局变量、局部变量的概念和使用方法。
三、实验内容
(一)下面程序的功能是:根据输入的整数x和n,利用函数fact实现求xn。
例如:输入:2,3 输出23=8
/* c9-1.c 利用函数fact实现求x的n次方*/
#include "stdio.h"
void main( )
{ long int fact(long x,long n) ; /*声明fact函数*/
long int x ;
long int n;
printf("please enter X and N(>=0): ");
scanf("%ld,%ld", &x, &n );
printf("%ld,%ld=%ld",x,n,fact(x,n)); /*调用fact函数 */
}
long int fact(long int x, long int n) /*定义fact函数求xn */
{ long int i,s; /*求累积变量的初始化*/
if(n==0) return 0;
for(i=1; i<=n; i++) /*用循环实现xn*/
s=s*x;
return(s); /*返回结果xn*/
}
(二) 下面程序的功能是:计算的值。
例如:输入:5,3 输出:
编程提示:
(1)定义求阶乘函数,在此基础上定义求组合数函数。
(2)主函数调用求组合数函数,求组合数函数再三次调用求阶乘函数。
/* c9-2.c 利用函数组合数*/
#include "stdio.h"
long int jf (int m,int n) /*定义求阶乘函数jf*/
{ int i,j;
long int t=1,u=1,r=1;
for(i=1; i<=m; i++)
t*=i;
for(i=1; i<=n; i++)
u*=i;
for(i=1; i<=(m-n); i++)
r*=i;
j=(t/(u*r));
return(j);
}
void main( )
{ int m,n,c;
printf("please enter m and n: ");
scanf("%d,%d", &m, &n );
c=jf(m,n);
printf("%d",c);
}
(三)下面程序的功能是:读入一个整数m,计算如下公式的值:
例如:若输入5,则应输出:“The result is 2.28333”。
/* c9-3.c 利用函数实现级数求和*/
#include "stdio.h"
void main()
{
double as(int a);
float b,c;
printf("请输入数值:");
scanf("%f",&b);
c=as(b);
printf("%f",c);
}
double as(int a)
{
float b=1.0;
int c;
for(c=2;c<=a;c++)
b=b+1.0/c;
printf("%f",b);
}
(四)下面程序的功能是:输入一个十进制整数,输出其对应的二进制数。
编程提示:
在main函数中定义一个变量并为其赋值,然后调用函数fun将该十进制数转换为二进制。
函数fun的形参即为被转换的整数,在for循环中每次求出m%k存放到数组aa中,同时将m/k的整数商赋给m继续判断,直止m的值为0。最后按反序输出数组aa的元素。
/* c9-4.c 通过函数调用实现数制转换*/
#include "stdio.h"
void main()
{
int as(int a);
int b;
printf("请输入数值:");
scanf("%d",&b);
printf("%d ",as(b));
}
int as(int a)
{
int b,c=2,aa[30];
for(b=0;a;b++)
{
aa[b]=a%c;
a=a/c;
}
b--;
for(;b;b--)
{
printf("%d ",aa[b]);
}
return 0;
}
如果将十进制数转换为八进制数,应对程序的哪个语句进行修改?怎样修改?
四、实验注意事项
(一)定义函数时,函数名后的圆括号后面不能加“;”。
(二)在函数体内,不能再对形参进行定义和说明。
(三)变量作实参时,只使用变量名,实参变量对形参变量的数据传递是“值传递”。
(四)一维数组作函数的实参时,只使用数组名如:fun(a); 。
边栏推荐
- PHP to read data from TXT file
- html+css+php+mysql实现注册+登录+修改密码(附完整代码)
- 请教一下本网站左下角的动漫人物是怎么做的?
- [21-Day Learning Challenge] A small summary of sequential search and binary search
- H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
- NodeJs, all kinds of path
- 辨析内存函数memset、memcmp、memmove以及memcpy
- 微信支付软件架构,这也太牛逼了!
- Constructor, this keyword, method overloading, local variables and member variables
- JSP built-in object out object function introduction
猜你喜欢
随机推荐
H5页面调用微信授权获取code
What is the function of the JSP Taglib directive?
Redis和MySQL数据一致性问题,有没有好的解决方案?
Pytorch seq2seq 模型架构实现英译法任务
Detailed explanation of JSP request object function
Markdown (CSDN) MD编辑器(四)- 漂亮表格(表格背景色、跨行、跨列)
Industrial control network intrusion detection based on automatic optimization of hyperparameters
go笔记——map
IDEA版Postman插件Restful Fast Request,细节到位,功能好用
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
ROS dynamic parameters
Mapped Statements collection does not contain value for的解决方法
Grid false data injection attacks detection based on coding strategy
管理基础知识17
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
Microsoft PC Manager V2.1 beta version officially released
严格模式,use strict
构造方法,this关键字,方法的重载,局部变量与成员变量
Kunpeng compile and debug plug-in actual combat
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core