BeautifulMan

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  16 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
这一章感觉好难啊!!!
学习笔记:(关于指针和多维数组)
// 多维数组和指针
#include <stdio.h>
int main(void)
{
    int zippo[4][2] = {{2, 4}, {6, 8}, {1, 3}, {5, 7}};
    /*
       zippo[0]是一个整数大小对象的地址,而zippo是两个整数大小对象的地址。
       因为(一个)整数和两个整数组成的数组开始于同一个地址,因此zippo和zippo[0]具有相同的数值。
       验证:
       输出也显示出二维数组zippo的地址和一维数组zippo[0]的地址是相同的,均为相应的数组
       首元素的地址,它的值是和&zippo[0][0]相同的;
       而且,*zippo也是一个指针,它与一维数组zippo[0](也是一个指针)的地址相同,证明了我的猜想!
    
*/
    printf("===========首先验证第一条结论===========\n");
    printf("zippo: \t\t%p\n&zippo[0]: \t%p\n", zippo, &zippo[0]);
    printf("zippo[0]: \t%p\n&zippo[0][0]: \t%p\n",zippo[0],&zippo[0][0]);
    printf("*zippo: \t%p\n&*zippo: \t%p\n", *zippo, &*zippo);
    printf("\n");
    /*
       zippo所指向对象的大小是两个int,而zippo[0]所指向对象的大小是一个int
       验证:
       zippo[0]指向4字节长的数据对象。对zippo[0]加1导致它的值增加4。数组名zippo是包含
       两个int数的数组的地址,因此它指向8字节长的数据对象。所以,对zippo加1导致它的值增加8。
    
*/
    printf("===========然后验证第二条结论===========\n");
    printf("zippo: \t\t%p\nzippo+1: \t%p\n", zippo, zippo+1);
    printf("zippo[0]: \t%p\nzippo[0]+1: \t%p\n", zippo[0], zippo[0]+1);
    printf("\n");
    /*
       *zippo也是一个指针,它与一维数组zippo[0](也是一个指针)的地址相同,它们指向同一个int变量
       即zippo[0][0]
       *zippo[0] = zippo[0][0]
       **zippo = *zippo[0] = zippo[0][0](得证)
       ------------------------------------------------
       分析*(*(zippo+2)+1)
       zippo------------------第1个大小为2个int的元素的地址
       zippo+2----------------第3个大小为2个int的元素的地址
       *(zippo+2)-------------第3个元素,即包含2个int值的数组,因此也是其第1个元素的(int值)的地址
       *(zippo+2)+1-----------包含2个int值的数组的第2个元素(int值)的地址
       *(*(zippo+2)+1)--------数组第3行第2列int(zippo[2][1])的值

       总结:更一般地,要表示单个元素,可以使用数组符号或指针符号;并且在这两种表示中即可以使用
       数组名,也可以使用指针:
       zippo[m][n] == *(*(zippo+m)+n)
    
*/
    printf("===========最后验证第三条结论===========\n");
    printf("*zippo: \t%p\nzippo[0]: \t%p\n", zippo, zippo[0]);
    printf("*(zippo+1): \t%p\nzippo[1]: \t%p\n", *(zippo+1), zippo[1]);
    printf("**zippo: \t%d\nzippo[0][0]: \t%d\n", **zippo, zippo[0][0]);
    printf("*(*(zippo+2)+1)\t%d\nzippo[2][1]: \t%d\n", *(*(zippo+2)+1), zippo[2][1]);
    return 0;
}
// 指针的兼容性
#include <stdio.h>
int main(void)
{
    /*
       指针之间的赋值规则比数值类型的赋值更严格
       举例说明:
    
*/
    int n = 5;
    double x;
    int * pi = &n;
    double * pd = &x;
    x = n;  // 不需要进行类型转换就直接把一个int数值赋给一个double类型的变量(隐藏的类型转换)
    pd = pi // 编译时错误,原因:pd指向一个double类型的数值,pi指向一个int类型的数值

    int * pt;
    int (* pa) [3];
    int ar1[2][3];
    int ar2[3][2];
    int **p2; // (指向int指针)的指针
    pt = &ar1[0][0];   // pt为指向一个int数值的指针,ar1[0][0]是一个int数值的变量
    pt = ar1[0];       // pt为指向一个int数值的指针,ar1[0]也为指向一个int数值的指针
    pt = ar1;          // pt为指向一个int数值的指针,ar1指向由3个int值构成的指针(非法)
    pa = ar1;          // pa指向由3个int值构成的数组,ar1也指向由3个int值构成的数组
    pa = ar2;          // pa指向由3个int值构成的数组,ar2指向由2个int值构成的数组(非法)
    p2 = &pt;          // p2是(指向int指针)的指针,&pt(头一次见,得记下来)也是(指向int指针)的指针
    *p2 = ar2[0];      // *p2为指向int的指针,ar2[0]也是指向int的指针
    p2 = ar2;          // p2是(指向int指针)的指针,ar2是指向由2个int值构成的数组(非法)
    return 0;
}
复习题
1、下面程序将打印什么?
#include <stdio.h>
int main(void)
{
    int ref[] = {8, 4, 0, 2};
    int *ptr;
    int index;

    for(index = 0, ptr = ref; index < 4; index++, ptr++)
        printf("%d %d\n", ref[index], *ptr);
    return 0;
}
答:
8 8 
4 4
0 0
2 2
2、在第1题中,数组ref包含多少个元素?
答:4
3、在第1题中,ref是哪些数据的地址?ref+1呢?++ref指向什么?
答:
数组名ref指向数组的第一个元素(整数8),表达式ref+1指向第二个元素(整数4)。++ref不是合法的C表达式,因为ref是常量而不是变量。ref == &ref[0]
4、下面每种情况中*ptr和*(ptr+2)的值分别是什么?
    a.
    int *ptr;
    int torf[2][2] = {12, 14, 16};
    ptr = torf[0];
    b.
    int *ptr;
    int fort[2][2] = { {12}, {14, 16} };
    ptr = fort[0];
答:
a.
*ptr = 12
*(ptr+2) = 16 注意:ptr+2指向第三个元素,它是第二行的第一个元素,而不是不确定的。
b.
*ptr = 12
*(ptr+2) = 14 同上
5、下面每种情况中**ptr和**(ptr+1)的值分别是什么?
    a.
    int (*ptr) [2];
    int torf[2][2] = {12, 14, 16};
    ptr = torf;
    b.
    int (*ptr) [2];
    int fort[2][2] = { {12}, {14, 16} };
    ptr = fort;
答:
a.
**ptr = 12
**(ptr + 1) = 16
b.
**ptr = 12
**(ptr + 1) = 14
6、假设有如下定义:
int grid[30][100];
a.用1种方法表示grid[22][56]的地址。
b.用2种方法表示grid[22][0]的地址。
c.用3种方法表示grid[0][0]的地址。
答:
a.
&grid[22][56]
b.
&grid[22][0] or grid[22](不是&grid[22])
c.
&grid[0][0] or grid[0] or int * grid(这里grid[0]是整数元素grid[0][0]的地址,grid是具有100个元素的数组grid[0]的地址。这两个地址具有相同的数值但是类型不同,类型指派可以使他们的类型相同)。
7、用适当的方法声明下面每个变量:
a.digits:一个包含10个int值的数组
b.rates:一个包含6个float值的数组
c.mat:一个包含3个元素的数组,其中每个元素是一个包含5个整数的数组
d.psa:一个包含20个指向char的指针的数组
e.pstr:一个指向数组的指针,其中数组由20个char值构成
答:
a.
int digits[10];
b.
float rates[6];
c.
int mat[3][5];
d.
char *(psa[20]); (psa是指针数组而不是指向数组的指针。具体地,psa会指向一个单个char(数组的第一个元素),psa+1会指向下一个字节)
e.
char (*pstr) [20];
8、
a.定义一个包含6个int值的数组,并且用数值1、2、4、8、16和32进行初始化。
b.用数组符号表示a部分中数组的第3个元素(数值为4的那个元素)。
c.假设系统支持C99规则,定义一个包含100个int值的数组并且初始化它,使它的末元素为-1,其他元素的值不考虑。
答:
a.
int array[6] = {1, 2, 4, 8, 16, 32};
b.
array[2];
c.
int ar[100] = { [99] = -1 };
9、包含10个元素的数组的索引范围是什么?
答:0~9
10、假设有如下声明:
float rootbeer[10], things[10][5], *pf, value = 2.2;
int i = 3;
则下列语句中哪些是正确的,哪些是错误的?
a.rootbeer[2] = value;
b.scanf("%f", &rootbeer);
c.rootbeer = value;
d.printf("%f", rootbeer);
e.things[4][4] = rootbeer[3];
f.things[5] = rootbeer;
g.pf = value;
h.pf = rootbeer;
答:
a------正确
b------错误(注意:rootbeer不是一个float变量
c------错误
d------错误
e------正确
f ------错误(注意:不能使用数组赋值)
g------错误
h------正确
11、声明一个800x600的int数组。
答:
int array[800][600];
12、以下是3个数组声明:
double trots[20];
short clops[10][30];
long shots[5][10][15];
a.以传统的void函数方式写出处理数组trots的函数原型和函数调用;然后以变长数组方式,写出处理数组trots的函数原型和函数调用。
b.以传统的void函数方式写出处理数组clops的函数原型和函数调用;然后以变长数组方式,写出处理数组clops的函数原型和函数调用。
c.以传统的void函数方式写出处理数组shots的函数原型和函数调用;然后以变长数组方式,写出处理数组shots的函数原型和函数调用。
答:
a.
void sum(double *pt, int n);
sum(trots, 20);
-------------------------------------
void sum(int n, double ar[n]);
sum(20, trots);
b.
void sum(short (*pt) [30], int n);
sum(clops, 10);
-------------------------------------
void sum(int n, int m, short ar[n][m]);
sum(10, 30, clops);
c.
void sum(long ar[][10][15], int n);
sum(shots, 5);
-------------------------------------
void sum(int n, int m, int q, long ar[n][m][q]);
sum(5, 10, 15, shots);
13、下面是两个函数原型:
void show(double ar[], int n);                     //n是元素数
void show2(double ar2[][3], int n);           //n是行数 
a.编写一个函数调用,把包含数值8、3、9和2的复合文字传递给函数shows()。
b.编写一个函数调用,把包含2行3列数值的复合文字传递给函数show2(),其中第一行为8、3、9;第二行为5、4、1。
答:
a.
show((double [4]) {8, 3, 9, 2}, 4);
b.
show2((double [][3]) { {8, 3, 9}, {5, 4, 1} }, 2);
编程练习(哈哈哈!!!题目感觉越来越简单了呢!除了最后一题外,好高兴!!!)
1、
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
int main(void)
{
    const float rain[YEARS][MONTHS] = {
        {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 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.3},
        {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
        {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
        {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
    };
    int year, month;
    float subtot, total;
    const float (*po) [MONTHS] = rain;

    printf(" YEAR   RAINFALL (inches) \n");
    for(year = 0, total = 0; year < YEARS; year++)
    {
        for(month = 0, subtot = 0; month < MONTHS; month++)
            subtot += *(*(po + year) + month);
        printf("%5d %15.1f\n", 2000 + year, subtot);
        total += subtot;
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
    printf("MONTHLY AVERAGES: \n\n");
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
    printf("Nov Dec\n");
    for(month = 0; month < MONTHS; month++)
    {
        for(year = 0, subtot = 0; year < YEARS; year++)
            subtot += *(*(po + year) + month);
        printf("%4.1f ", subtot/YEARS);
    }
    printf("\n");
    return 0;
}
2、
#include <stdio.h>
void copy_arr(const double sou[], double tar[], int n);
void copy_ptr(const double *sou, double *tar, int n);
int main(void)
{
    int i;
    const double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    copy_arr(source, target1, 5);
    copy_ptr(source, target2, 5);

    printf("--------------输出验证----------------\n");
    for(i = 0; i < 5; i++)
        printf("target1[%d] = %.1f\ttarget2[%d] = %.1f\n", i, target1[i], i, target2[i]);
    return 0;
}
void copy_arr(const double sou[], double tar[], int n)
{
    int i;

    for(i = 0; i < n; i++)
        tar[i] = sou[i];
}
void copy_ptr(const double *sou, double *tar, int n)
{
    int i;

    for(i = 0; i < n; i++)
        *(tar + i) = *(sou + i);
}
3、
#include <stdio.h>
int get_max(const int *ar, int n);
int main(void)
{
    const int source[5] = {16, 2, 78, 990, 123};

    printf("--------------输出验证----------------\n");
    printf("max is %d\n", get_max(source, 5));
    return 0;
}
int get_max(const int *ar, int n)
{
    int i, max;
    max = *ar;
    for(i = 1; i < n; i++)
    {
        max = *(ar + i) > max ? *(ar + i) : max;
    }
    return max;
}
4、(没必要做两次循环,一次循环就够了,真是写的罗里吧嗦的!)
#include <stdio.h>
int get_max_index(const double *ar, int n);
int main(void)
{
    const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};

    printf("--------------输出验证----------------\n");
    printf("index is %d\n", get_max_index(source, 5));
    return 0;
}
int get_max_index(const double *ar, int n)
{
    int i, index;
    double max = *ar;
    for(i = 1; i < n; i++)
    {
        max = *(ar + i) > max ? *(ar + i) : max;
    }
    for(i = 0; i < n; i++)
    {
        if(*(ar + i) == max)
        {
            index = i;
            break;
        }
    }
    return index;
}
改进之后的程序代码:
#include <stdio.h>
#define SIZE 5
int max(double arr[], int n);
int main(void)
{
    double source[SIZE] = {1.89, 90.00, 56.78, 789.78, 23.34};

    printf("The max index is %d\n", max(source, SIZE));
    return 0;
}
int max(double arr[], int n)
{
    int i = 0, index;
    int max = arr[i];
    for(i = 1; i < n; i++)
        if(arr[i] > max)
        {
            max = arr[i];
            index = i;
        }
    return index;
}
5、
#include <stdio.h>
double get_max_min(const double *ar, int n);
int main(void)
{
    const double source[5] = {16.3, 2.2, 78.78, 990.99, 123};

    printf("--------------输出验证----------------\n");
    printf("max - min = %.2f\n", get_max_min(source, 5));
    return 0;
}
double get_max_min(const double *ar, int n)
{
    int i;
    double max = *ar;
    double min = *ar;
    for(i = 1; i < n; i++)
    {
        max = *(ar + i) > max ? *(ar + i) : max;
        min = *(ar + i) < min ? *(ar + i) : min;
    }
    return max - min;
}
6、
#include <stdio.h>
#define ROWS 3
#define COLS 4
void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows);
int main(void)
{
    int i;
    int j;
    double source[ROWS][COLS] = {
        {2.1, 3.4, 78.9, 23.3},
        {231.1, 45.5, 34, 12},
        {23.7, 567.8, 56.5, 32}
    };
    double target[ROWS][COLS];
    copy_ptr(source, target, ROWS);
    printf("--------------Output verification----------------\n");
    for(i = 0; i < ROWS; i++)
    {
        for(j = 0; j < COLS; j++)
            printf("%.1f\t", *(*(target + i) + j));
        printf("\n");
    }
    return 0;
}
void copy_ptr(double (*sou) [COLS], double (*tar) [COLS], int rows)
{
    int i;
    int j;
    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < COLS; j++)
            *(*(tar + i) + j) = *(*(sou + i) + j);
    }
}
7、
#include <stdio.h>
void copy_ptr(double *sou, double *tar, int n);
int main(void)
{
    int i;
    double source[7] = {12.12, 23.4, 34.23, 1, 1.2, 5.6, 67.78};
    double target[3];

    copy_ptr(source, target, 3);
    printf("--------------Output verification----------------\n");
    for(i = 0; i < 3; i++)
        printf("%.2f\n", *(target + i));
    return 0;
}
void copy_ptr(double *sou, double *tar, int n)
{
    int i;

    for(i = 0; i < n; i++)
        *(tar + i) = *(sou + i + n - 1);
}
8、
#include <stdio.h>
#define ROWS 3
#define COLS 5
void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m]);
void show_arr(int n, int m, double ar[n][m]);
int main(void)
{
    double source[ROWS][COLS] = {
        {23.12, 45.66, 45.0, 89.9, 77.6},
        {11.1, 22.22, 4.45, 34.3, 4},
        {22.1, 789.99, 34.23, 12.12, 56}
    };
    double target[ROWS][COLS];

    copy_ptr(source, ROWS, COLS, target);
    printf("--------------show array source----------------\n");
    show_arr(ROWS, COLS, source);
    printf("--------------show array target----------------\n");
    show_arr(ROWS, COLS, target);
    return 0;
}
void copy_ptr(double (*sou)[COLS], int n, int m, double tar[n][m])
{
    int r;
    int c;

    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
            *(*(tar + r) + c) = *(*(sou + r) + c);
    }
}
void show_arr(int n, int m, double ar[n][m])
{
    int r;
    int c;

    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
            printf("%.2f\t", ar[r][c]);
        printf("\n");
    }
}
9、
#include <stdio.h>
void sum_array(int *ar1, int *ar2, int *ar3, int n);
int main(void)
{
    int i;
    int array1[4] = {2, 4, 5, 8};
    int array2[4] = {1, 0, 4, 6};
    int array3[4];

    sum_array(array1, array2, array3, 4);
    printf("--------------Output verification----------------\n");
    for(i = 0; i < 4; i++)
        printf("%d\t", *(array3 + i));
    return 0;
}
void sum_array(int *ar1, int *ar2, int *ar3, int n)
{
    int i;

    for(i = 0; i < n; i++)
        *(ar3 + i) = *(ar1 + i) + *(ar2 + i);
}
10、
#include <stdio.h>
#define ROWS 3
#define COLS 5
void show_array(int (*ar)[COLS], int rows);
void double_array(int (*ar)[COLS], int rows);
int main(void)
{
    int source[ROWS][COLS] = {
        {1, 2, 3, 4, 5},
        {2, 3, 4, 5, 6},
        {3, 4, 5, 6, 7}
    };

    printf("--------------show array source----------------\n");
    show_array(source, ROWS);
    double_array(source, ROWS);
    printf("--------------again show array source----------------\n");
    show_array(source, ROWS);
    return 0;
}
void show_array(int (*ar)[COLS], int rows)
{
    int r;
    int c;

    for(r = 0; r < rows; r++)
    {
        for(c = 0; c < COLS; c++)
            printf("%d\t", *(*(ar + r) + c));
        printf("\n");
    }
}
void double_array(int (*ar)[COLS], int rows)
{
    int r;
    int c;

    for(r = 0; r < rows; r++)
    {
        for(c = 0; c < COLS; c++)
            *(*(ar + r) + c) *= 2;
    }
}
11、(感觉代码越写越多了,与之前没简便到哪儿去)
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
//对于每一年,计算各月的总降水量并把各个值存储在一个数组中
void fun1(float (*ye)[MONTHS], float * yea);
// 对于每一年,显示各月的总降水量
void show_array1(float *ar);
// 计算年降水平均量
float sum1(float *ar);
// 对于每个月,计算月降水平均量并把各个值存储在一个数组中
void fun2(float (*ye)[MONTHS], float * mon);
// 对于每个月,显示月降水平均量
void show_array2(float *ar);
int main(void)
{
    const float rain[YEARS][MONTHS] = {
        {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 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.3},
        {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
        {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
        {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
    };
    float year_rain[YEARS];
    float month_rain[MONTHS];

    fun1(rain, year_rain);
    fun2(rain, month_rain);
    printf(" YEAR   RAINFALL (inches) \n");
    show_array1(year_rain);
    printf("\nThe yearly average is %.1f inches.\n\n", sum1(year_rain));

    printf("MONTHLY AVERAGES: \n\n");
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
    printf("Nov Dec\n");
    show_array2(month_rain);
    printf("\n");
    return 0;
}
void fun1(float (*ye)[MONTHS], float * yea)
{
    float subtot;
    int year, month;

    for(year = 0 ; year < YEARS; year++)
    {
        for(month = 0, subtot = 0; month < MONTHS; month++)
            subtot += *(*(ye + year) + month);
        *(yea + year) = subtot;
    }
}
void show_array1(float *ar)
{
    int i;

    for(i = 0; i < YEARS; i++)
        printf("%5d %15.1f\n", 2000 + i, *(ar + i));
}
float sum1(float *ar)
{
    int i;
    float total;

    for(i = 0; i < YEARS; i++)
        total += *(ar + i);
    return total / YEARS;
}
void fun2(float (*ye)[MONTHS], float * mon)
{
    int year, month;
    float subtot;

    for(month = 0; month < MONTHS; month++)
    {
        for(year = 0, subtot = 0; year < YEARS; year++)
            subtot += *(*(ye + year) + month);
        *(mon + month) = subtot / YEARS;
    }
}
void show_array2(float *ar)
{
    int i;

    for(i = 0; i < MONTHS; i++)
        printf("%4.1f", *(ar + i));
}
有必要写那么多的函数吗?只须写两个函数就可以搞定的,非得写那么多,改进之后程序如下:
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
// 计算年降水总量与所有年度的总降水量
double calculate1(const float arr[][MONTHS], int y);
// 计算各年该月份的总降水量
void calculate2(const float arr[][MONTHS], int y);
int main(void)
{
    const float rain[YEARS][MONTHS] = {
        {4.3, 4.3, 4.3, 3.0, 2.0, 1.2, 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.3},
        {9.1, 8.5, 6.7, 4.3, 2.1, 0.8, 0.2, 0.2, 1.1, 2.3, 6.1, 8.4},
        {7.2, 9.9, 8.4, 3.3, 1.2, 0.8, 0.4, 0.0, 0.6, 1.7, 4.3, 6.2},
        {7.6, 5.6, 3.8, 2.8, 3.8, 0.2, 0.0, 0.0, 0.0, 1.3, 2.6, 5.2}
    };
    float total;

    printf(" YEAR   RAINFALL (inches) \n");
    total = calculate1(rain, YEARS);
    printf("\nThe yearly average is %.1f inches.\n\n", total/YEARS);
    printf("MONTHLY AVERAGES: \n\n");
    printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct ");
    printf("Nov Dec\n");
    calculate2(rain, YEARS);
    printf("\n");
    return 0;
}
double calculate1(const float arr[][MONTHS], int y)
{
    int year, month;
    double total, subtot;

    for(year = 0, total = 0; year < y; year++)
    {
        for(month = 0, subtot = 0; month < MONTHS; month++)
            subtot += arr[year][month];
        printf("%5d %15.1f\n", 2000 + year, subtot);
        total += subtot;
    }
    return total;
}
void calculate2(const float arr[][MONTHS], int y)
{
    int year, month;
    double subtot;

    for(month = 0; month < MONTHS; month++)
    {
        for(year = 0, subtot = 0; year < y; year++)
            subtot += arr[year][month];
        printf("%4.1f ", subtot/YEARS);
    }
}
12、(关于如何输入数字,没搞明白,还是借鉴CSDN----vs9841前辈的做法,不过后面都是自己写了
#include <stdio.h>
#define ROWS 3
#define COLS 5
// 从键盘获取一个double数
double get_double(void);
// 向source[ROWS][COLS]中输入数值
void input_double(int n, int m, double (*dou)[COLS]);
// 计算每个数集的平均值
void get_average(int n, int m, double (*dou)[COLS]);
// 计算所有数值的平均值
double get_all_average(int n, int m, double (*dou)[COLS]);
// 找出所有数中的最大值
double get_max(int n, int m, double (*dou)[COLS]);
int main(void)
{
    double source[ROWS][COLS];
    input_double(ROWS, COLS, source);
    printf("----------------------------------------------------\n");
    get_average(ROWS, COLS, source);
    printf("----------------------------------------------------\n");
    printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
    printf("----------------------------------------------------\n");
    printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
    return 0;
}
double get_double(void)
{
    double input;
    char ch;

    while(scanf("%lf", &input) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not a double.\nPlease enter a ");
        printf("double value, such as 23.3, -12.1, or 3: \n");
    }
    return input;
}
void input_double(int n, int m, double (*dou)[COLS])
{
    int i, j;

    printf("Please enter data of %dx%d two dimensional array\n", n, m);
    for(i = 0; i < n; i++)
    {
        printf("Start with %d sets of numbers: \n", i+1);
        for(j = 0; j < m; j++)
        {
            printf("%d number: ", j+1); // 记住每次只能处理输入一个数
            dou[i][j] = get_double();
        }
    }
    printf("Data entry is complete, as shown below: \n");
    for(i = 0; i < n; i++)
    {
        printf("%d sets of numbers: \n", i+1);
        for(j = 0; j < m; j++)
            printf("%5.2f\t", dou[i][j]);
        printf("\n");
    }
}
void get_average(int n, int m, double (*dou)[COLS])
{
    int r;
    int c;
    double total = 0;

    for(r = 0; r < n; r++)
    {
        printf("average of the %d sets of numbers: ", r + 1);
        for(c = 0, total = 0; c < m; c++)
            total += dou[r][c];
        printf("%5.2f\n", total / m);
    }
}
double get_all_average(int n, int m, double (*dou)[COLS])
{
    int r;
    int c;
    double total = 0;

    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
            total += dou[r][c];
    }
    return total / (n * m);
}
double get_max(int n, int m, double (*dou)[COLS])
{
    int r;
    int c;
    double max = dou[0][0];


    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
        {
            max = max > dou[r][c] ? max : dou[r][c];
        }
    }
    return max;
}
第二次更新如下,完全自己手写:(可能对自己好理解一点)
#include <stdio.h>
void task_a(double arr[][5], int n);
void task_b(double arr[][5], int n);
double task_c(double arr[][5], int n);
double task_d(double arr[][5], int n);
void task_e(double arr[][5], int n);
int main(void)
{
    double array[3][5];

    task_a(array, 3);
    task_b(array, 3);
    printf("所有数值的平均数为:%.2f\n", task_c(array, 3));
    printf("这15个数中的最大值为:%.2f\n", task_d(array, 3));
    printf("该3x5数组为:\n");
    task_e(array, 3);
    printf("\n");
    return 0;
}
void task_a(double arr[][5], int n)
{
    int i = 0;
    int count;
    double num;

    printf("请输入3个数集\n");
    while(i < n)
    {
        printf("请输入第%d个数集:\n", i + 1);
        count = 0;
        printf("请输入第%d个数:", count + 1);
        while(scanf("%lf", &num) == 1 && count < 5)
        {
            arr[i][count] = num;
            if(count == 4)
                break;
            count++;
            printf("请输入第%d个数:", count + 1);
        }
        i++;
    }
}
void task_b(double arr[][5], int n)
{
    double tot;

    for(int r = 0; r < n; r++)
    {
        tot = 0;
        for(int c = 0; c < 5; c++)
            tot += arr[r][c];
        printf("第%d个数集的平均值为: %.2f\n", r + 1, tot / 5);
    }
}
double task_c(double arr[][5], int n)
{
    double total = 0;

    for(int r = 0; r < n; r++)
        for(int c = 0; c < 5; c++)
            total += arr[r][c];
    return total / 15;
}
double task_d(double arr[][5], int n)
{
    double max;
    max = arr[0][0];

    for(int r = 0; r < n; r++)
        for(int c = 0; c < 5; c++)
            if(arr[r][c] > max)
                max = arr[r][c];
    return max;
}
void task_e(double arr[][5], int n)
{
    for(int r = 0; r < n; r++)
    {
        for(int c = 0; c < 5; c++)
            printf("%.2f ", arr[r][c]);
        printf("\n");
    }
}
13、
同上,第二次更新如下:
#include <stdio.h>
void task_a(int n, int m, double arr[n][m]);
void task_b(int n, int m, double arr[n][m]);
double task_c(int n, int m, double arr[n][m]);
double task_d(int n, int m, double arr[n][m]);
void task_e(int n, int m, double arr[n][m]);
int main(void)
{
    double array[3][5];

    task_a(3, 5, array);
    task_b(3, 5, array);
    printf("所有数值的平均数为:%.2f\n", task_c(3, 5, array));
    printf("这15个数中的最大值为:%.2f\n", task_d(3, 5, array));
    printf("该3x5数组为:\n");
    task_e(3, 5, array);
    printf("\n");
    return 0;
}
void task_a(int n, int m, double arr[n][m])
{
    int i = 0;
    int count;
    double num;

    printf("请输入3个数集\n");
    while(i < n)
    {
        printf("请输入第%d个数集:\n", i + 1);
        count = 0;
        printf("请输入第%d个数:", count + 1);
        while(scanf("%lf", &num) == 1 && count < m)
        {
            arr[i][count] = num;
            if(count == 4)
                break;
            count++;
            printf("请输入第%d个数:", count + 1);
        }
        i++;
    }
}
void task_b(int n, int m, double arr[n][m])
{
    double tot;

    for(int r = 0; r < n; r++)
    {
        tot = 0;
        for(int c = 0; c < m; c++)
            tot += arr[r][c];
        printf("第%d个数集的平均值为: %.2f\n", r + 1, tot / 5);
    }
}
double task_c(int n, int m, double arr[n][m])
{
    double total = 0;

    for(int r = 0; r < n; r++)
        for(int c = 0; c < m; c++)
            total += arr[r][c];
    return total / 15;
}
double task_d(int n, int m, double arr[n][m])
{
    double max;
    max = arr[0][0];

    for(int r = 0; r < n; r++)
        for(int c = 0; c < m; c++)
            if(arr[r][c] > max)
                max = arr[r][c];
    return max;
}
void task_e(int n, int m, double arr[n][m])
{
    for(int r = 0; r < n; r++)
    {
        for(int c = 0; c < m; c++)
            printf("%.2f ", arr[r][c]);
        printf("\n");
    }
}
首次做的如下:
#include <stdio.h>
#define ROWS 3
#define COLS 5
// 从键盘获取一个double数
double get_double(void);
// 向source[ROWS][COLS]中输入数值
void input_double(int n, int m, double dou[n][m]);
// 计算每个数集的平均值
void get_average(int n, int m, double dou[n][m]);
// 计算所有数值的平均值
double get_all_average(int n, int m, double dou[n][m]);
// 找出所有数中的最大值
double get_max(int n, int m, double dou[n][m]);
int main(void)
{
    double source[ROWS][COLS];
    input_double(ROWS, COLS, source);
    printf("----------------------------------------------------\n");
    get_average(ROWS, COLS, source);
    printf("----------------------------------------------------\n");
    printf("Mean values of all the numbers: %5.2f\n", get_all_average(ROWS, COLS, source));
    printf("----------------------------------------------------\n");
    printf("Maximum value for all: %5.2f\n", get_max(ROWS, COLS, source));
    return 0;
}
double get_double(void)
{
    double input;
    char ch;
    while(scanf("%lf", &input) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not a double.\nPlease enter a ");
        printf("double value, such as 23.3, -12.1, or 3: \n");
    }
    return input;
}
void input_double(int n, int m, double dou[n][m])
{
    int i, j;
    printf("Please enter data of %dx%d two dimensional array\n", n, m);
    for(i = 0; i < n; i++)
    {
        printf("Start with %d sets of numbers: \n", i+1);
        for(j = 0; j < m; j++)
        {
            printf("%d number: ", j+1); // 记住每次只能处理输入一个数
            dou[i][j] = get_double();
        }
    }
    printf("Data entry is complete, as shown below: \n");
    for(i = 0; i < n; i++)
    {
        printf("%d sets of numbers: \n", i+1);
        for(j = 0; j < m; j++)
            printf("%5.2f\t", dou[i][j]);
        printf("\n");
    }
}
void get_average(int n, int m, double dou[n][m])
{
    int r;
    int c;
    double total = 0;
    for(r = 0; r < n; r++)
    {
        printf("average of the %d sets of numbers: ", r + 1);
        for(c = 0, total = 0; c < m; c++)
            total += dou[r][c];
        printf("%5.2f\n", total / m);
    }
}
double get_all_average(int n, int m, double dou[n][m])
{
    int r;
    int c;
    double total = 0;
    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
            total += dou[r][c];
    }
    return total / (n * m);
}
double get_max(int n, int m, double dou[n][m])
{
    int r;
    int c;
    double max = dou[0][0];
    for(r = 0; r < n; r++)
    {
        for(c = 0; c < m; c++)
        {
            max = max > dou[r][c] ? max : dou[r][c];
        }
    }
    return max;
}
posted on 2015-11-24 22:31 李阿昀 阅读(820) 评论(0)  编辑  收藏 所属分类: C Primer Plus 复习题与编程练习

只有注册用户登录后才能发表评论。


网站导航: