-------------------------------------------------------------------------------
编写一个函数。函数的三个参数是一个字符和两个整数。字符参数是需要输出的字符。第1个整数说明了在每行中该字符输出的个数,而第二个整数指的是需要输出的行数。编写一个调用该函数的程序。要求:输入字符为回车时,作为未输入字符处理;输入行列值时若不是数字,直接结束程序。
编程考点:循环基本语法;break和continue
#include
void chLineRow(char ch, int c, int r); int main(void) { }
void chLineRow(char ch, int c, int r) { }
for (row = 0; row < r; row++) { } return;
for (col = 0; col < c; col++)
putchar(ch); putchar('\\n'); int col, row; return 0;
printf(\); while ( (ch = getchar()) != '#') { }
printf(\);
if (ch == '\\n')
continue;
printf(\); if (scanf(\, &col, &row) != 2)
break;
chLineRow(ch, col, row);
printf(\); char ch; int col, row;
编写一个函数计算double类型数的某个整数次幂(不得调用系统的函数pow), 注意0的任何次幂和任何数值的0次幂以及负数次幂 并编写测试程序
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
编程考点:if-else的嵌套
#include
double power(double a, int b); /* ANSI prototype */ int main(void) { }
double power(double a, int b) /* function definition */ {
if (b == 0) {
if (a == 0)
printf(\); pow = 1.0; pow = 0.0;
for (i = 1; i <= b; i++)
pow *= a;
// // }
double pow = 1; int i;
printf(\);
printf(\); printf(\);
while (scanf(\, &x, &n) == 2) { }
printf(\); return 0;
xpow = power(x, n); /* function call */ printf(\, x, n, xpow); printf(\); double x, xpow; int n;
} else if (a == 0) else if (b > 0)
else
/* b < 0 */
pow = 1.0 / power(a, -b);
return pow; /* return the value of pow */
写一个计算降水量的程序,给定一个数组,记录的每年每月的降水量
const float rain[YRS][MONTHS] = {
{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.4, 7.2} };
使用指针而不是使用下标进行计算,每年的降水总值、每年平均值,以及每月平均量
编程考点:如何用指针定位二维数组的元素
#include
#define MONTHS 12 /* number of months in a year */ #define YRS 5 /* number of years of data */ int main(void) {
for (month = 0; month < MONTHS; month++) { /* for each month, sum }
printf(\); return 0;
for (year = 0, subtot =0; year < YRS; year++)
subtot += *(*(rain + year) + month); printf(\, subtot/YRS);
rainfall over years */
printf(\);
for (year = 0, total = 0; year < YRS; year++) { /* for each year, sum }
printf(\, total/YRS); printf(\);
printf(\); printf(\);
for (month = 0, subtot = 0; month < MONTHS; month++)
subtot += *(*(rain + year) + month); printf(\, 1990 + year, subtot); total += subtot; /* total for all years */ /* initializing rainfall data for 1990 - 1994 */
const float rain[5][12] = { { 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.4, 7.2 } };
int year, month; float subtot, total;
rainfall for each month */
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
}
编写一个程序,提示用户输入3个数据集,每个数据集包括5个double值。程序应当实现下列所有功能:
A. 把输入信息存储到一个3*5的数组中 B. 计算出每个数集(包含5个值)的平均值 C. 计算所有数值的平均数 D. 找出这15个数中的最大值 打印出结果
每个任务需要用一个单独的函数来实现。对于任务B需要编写计算并返回一维数组平均值的函数,循环三次调用该函数来实现任务B。对于任务C、D,函数应当把整个数组做为参数,并却完成任务B、C和D的函数应该向他的调用函数返回答案 (推荐使用变长数组,参考程序为变长数组实现)
编程考点:循环和二维数组的熟练应用,以及数组作为参数的传递
#include
void store(double ar[], int n);
double average2d(int rows, int cols, double ar[rows][cols]); double max2d(int rows, int cols, double ar[rows][cols]); void showarr2(int rows, int cols, double ar[rows][cols]); double average(const double ar[], int n);
int main(void) {
for (row = 0; row < ROWS; row++)
printf(\, row + 1,
COLS));
printf(\); showarr2(ROWS, COLS, stuff); for (row = 0; row < ROWS; row++) { }
printf(\, COLS, row + 1); store(stuff[row], COLS); double stuff[ROWS][COLS]; int row;
average(stuff[row],
printf(\, average2d(ROWS, COLS,
stuff));
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
printf(\, max2d(ROWS, COLS, stuff)); printf(\); return 0;
}
void store(double ar[], int n) { int i;
for (i = 0; i < n; i++) { printf(\, i + 1); scanf(\, &ar[i]);
}
}
double average2d(int rows, int cols, double ar[rows][cols]) { int r, c;
double sum = 0.0; for (r = 0; r < rows; r++) for (c = 0; c < cols; c++)
sum += ar[r][c];
if (rows * cols > 0)
return sum / (rows * cols); else
return 0.0; }
double max2d(int rows, int cols, double ar[rows][cols]) { int r, c;
double max = ar[0][0]; for (r = 0; r < rows; r++) for (c = 0; c < cols; c++) if (max < ar[r][c])
max = ar[r][c];
return max;
}
void showarr2(int rows, int cols, double ar[rows][cols]) { int row, col;
for (row = 0; row < rows; row++) { for (col = 0; col < cols; col++)
printf(\, ar[row][col]);
-------------------------------------------------------------------------------