C补强阶段作业

上传人:s9****2 文档编号:483620522 上传时间:2023-04-18 格式:DOC 页数:12 大小:75.50KB
返回 下载 相关 举报
C补强阶段作业_第1页
第1页 / 共12页
C补强阶段作业_第2页
第2页 / 共12页
C补强阶段作业_第3页
第3页 / 共12页
C补强阶段作业_第4页
第4页 / 共12页
C补强阶段作业_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《C补强阶段作业》由会员分享,可在线阅读,更多相关《C补强阶段作业(12页珍藏版)》请在金锄头文库上搜索。

1、精品文档,仅供学习与交流,如有侵权请联系网站删除编写一个函数。函数的三个参数是一个字符和两个整数。字符参数是需要输出的字符。第1个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。要求:输入字符为回车时,作为未输入字符处理;输入行列值时若不是数字,直接结束程序。编程考点:循环基本语法;break和continue#include void chLineRow(char ch, int c, int r);int main(void) char ch;int col, row;printf(Enter a character (# to quit):

2、 );while ( (ch = getchar() != #) if (ch = n)continue;printf(Enter number of columns and number of rows: );if (scanf(%d %d, &col, &row) != 2)break;chLineRow(ch, col, row);printf(nEnter next character (# to quit): );printf(Bye!n);return 0;void chLineRow(char ch, int c, int r) int col, row;for (row = 0

3、; row r; row+) for (col = 0; col c; col+)putchar(ch);putchar(n);return;编写一个函数计算double类型数的某个整数次幂(不得调用系统的函数pow), 注意0的任何次幂和任何数值的0次幂以及负数次幂并编写测试程序编程考点:if-else的嵌套#include double power(double a, int b); /* ANSI prototype */int main(void) double x, xpow;int n;printf(Enter a number and the integer power);pri

4、ntf( to whichnthe number will be raised. Enter q);printf( to quit.n);while (scanf(%lf%d, &x, &n) = 2) xpow = power(x, n); /* function call */printf(%.3g to the power %d is %.5gn, x, n, xpow);printf(Enter next pair of numbers or q to quit.n);printf(Hope you enjoyed this power trip - bye!n);return 0;d

5、ouble power(double a, int b) /* function definition */double pow = 1;int i;if (b = 0) /if (a = 0)/printf(0 to the 0 undefined; using 1 as the valuen);pow = 1.0; else if (a = 0)pow = 0.0;else if (b 0)for (i = 1; i = b; i+)pow *= a;else/* b 0 */pow = 1.0 / power(a, -b);return pow; /* return the value

6、of pow */写一个计算降水量的程序,给定一个数组,记录的每年每月的降水量const float rainYRSMONTHS = 10.2, 8.1, 6.8, 4.2, 2.1, 1.8, 0.2, 0.3, 1.1, 2.3, 6.1, 7.4, 9.2, 9.8, 4.4, 3.3, 2.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 5.2, 6.6, 5.5, 3.8, 2.8, 1.6, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 4.2, 4.3, 4.3, 4.3, 3.0, 2.0, 1.0, 0.2, 0.2, 0.4, 2.4, 3.

7、5, 6.6, 8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2, 0.9, 0.3, 0.9, 1.4, 7.2使用指针而不是使用下标进行计算,每年的降水总值、每年平均值,以及每月平均量编程考点:如何用指针定位二维数组的元素#include #define MONTHS 12 /* number of months in a year */#define YRS 5 /* number of years of data */int main(void) /* initializing rainfall data for 1990 - 1994 */const float r

8、ain512 = 10.2, 8.1, 6.8, 4.2, 2.1, 1.8, 0.2,0.3, 1.1, 2.3, 6.1, 7.4 , 9.2, 9.8, 4.4, 3.3, 2.2, 0.8, 0.4,0.0, 0.6, 1.7, 4.3, 5.2 , 6.6, 5.5, 3.8, 2.8, 1.6, 0.2, 0.0,0.0, 0.0, 1.3, 2.6, 4.2 , 4.3, 4.3, 4.3, 3.0, 2.0, 1.0, 0.2,0.2, 0.4, 2.4, 3.5, 6.6 , 8.5, 8.2, 1.2, 1.6, 2.4, 0.0, 5.2,0.9, 0.3, 0.9, 1

9、.4, 7.2 ;int year, month;float subtot, total;printf( YEAR RAINFALL (inches)n);for (year = 0, total = 0; year YRS; year+) /* for each year, sum rainfall for each month */for (month = 0, subtot = 0; month MONTHS; month+)subtot += *(*(rain + year) + month);printf(%5d %15.1fn, 1990 + year, subtot);total

10、 += subtot; /* total for all years */printf(nThe yearly average is %.1f inches.nn, total/YRS);printf(MONTHLY AVERAGES:nn);printf( Jan Feb Mar Apr May Jun Jul Aug Sep Oct );printf( Nov Decn);for (month = 0; month MONTHS; month+) /* for each month, sum rainfall over years */for (year = 0, subtot =0; y

11、ear YRS; year+)subtot += *(*(rain + year) + month);printf(%4.1f , subtot/YRS);printf(n);return 0;编写一个程序,提示用户输入3个数据集,每个数据集包括5个double值。程序应当实现下列所有功能:A 把输入信息存储到一个3*5的数组中B 计算出每个数集(包含5个值)的平均值C 计算所有数值的平均数D 找出这15个数中的最大值打印出结果每个任务需要用一个单独的函数来实现。对于任务B需要编写计算并返回一维数组平均值的函数,循环三次调用该函数来实现任务B。对于任务C、D,函数应当把整个数组做为参数,并却完

12、成任务B、C和D的函数应该向他的调用函数返回答案(推荐使用变长数组,参考程序为变长数组实现)编程考点:循环和二维数组的熟练应用,以及数组作为参数的传递#include #define ROWS 3#define COLS 5 void store(double ar, int n);double average2d(int rows, int cols, double arrowscols);double max2d(int rows, int cols, double arrowscols);void showarr2(int rows, int cols, double arrowscol

13、s);double average(const double ar, int n);int main(void) double stuffROWSCOLS;int row;for (row = 0; row ROWS; row+) printf(Enter %d numbers for row %dn, COLS, row + 1);store(stuffrow, COLS);printf(array contents:n);showarr2(ROWS, COLS, stuff);for (row = 0; row ROWS; row+)printf(average value of row

14、%d = %gn, row + 1, average(stuffrow,COLS);printf(average value of all rows = %gn, average2d(ROWS, COLS, stuff);printf(largest value = %gn, max2d(ROWS, COLS, stuff);printf(Bye!n);return 0;void store(double ar, int n) int i;for (i = 0; i n; i+) printf(Enter value #%d: , i + 1);scanf(%lf, &ari);double average2d(int rows, int cols, double arrowscols) int r, c;double sum = 0.0;fo

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题 > 初中试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号