E N D
c语言常用数学函数math.h • int abs(int i) 返回整型参数i的绝对值double fabs(double x) 返回双精度参数x的绝对值long labs(long n) 返回长整型参数n的绝对值double exp(double x) 返回指数函数ex的值double log(double x) 返回logex的值double log10(double x) 返回log10x的值double pow(double x,double y) 返回xy的值double pow10(int p) 返回10p的值double sqrt(double x) 返回+√x的值double cos(double x) 返回x的余弦cos(x)值,x为弧度double sin(double x) 返回x的正弦sin(x)值,x为弧度double tan(double x) 返回x的正切tan(x)值,x为弧度double ceil(double x) 返回不小于x的最小整数double floor(double x) 返回不大于x的最大整数void srand(unsigned seed) 初始化随机数发生器int rand() 产生一个随机数并返回这个数double fmod(double x,double y) 返回x/y的余数
Functions and Arrays - Program 2 • Calculate the average score of the 10 students. • Read 10 scores into float array score. • Call function average to calculate the average score.
0 92 1 73.5 2 56 . …. …. . 88 9 stu score float average( float stu[ ], int n ) { float av, total=0; int i; for( i=0; i<n; i++ ) total = total + stu[i]; av = total/n; return ( av ); } main() { float score[10], av; int i; printf("Input 10 scores:\n"); for( i = 0; i < 10; i ++ ) scanf ( "%f", &score[i] ); av = average ( score, 10 ); printf("average is:%.2f", av); } float average( float stu[], int n ) { float av, total=0; int i; for( i=0; i<n; i++ ) total = total + stu[i]; av = total/n; return ( av ); } main() { float score[10], av; int i; printf("Input 10 scores:\n"); for( i = 0; i < 10; i ++ ) scanf ( "%f", &score[i] ); av = average ( score, 10 ); printf("average is:%.2f", av); } Functions and Arrays - Program 2 • stu = score (send the start address of array score to stu) • n = 10 In formal parameters, the array may be specified the size, but it has no any meaning. The compiler just transfers the address to the formal parameter. Therefore, we must transfer the size of array to formal parameter n
Programming Exercises • Define a function max, return the larger integer number of 3, and call it in main(). • Use function( 1*2*3*4*5*…*n) to calculate the value s= ( 1*2*3*4*5*…*a) + ( 1*2*3*4*5*…*b) • Define a function max, return the largest float number of an array. And call it in main(). • Define a function max_min to return the largest and smallest of an array from a function? Call it in main().
Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.
Write a program that inputs a series of integers and passes them one at a time to function even, which uses the remainder operator to determine if an integer is even. The function should take an integer argument and return 1 if the integer is even and 0 otherwise.