zoukankan      html  css  js  c++  java
  • c primer plus 10 编程练习

    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;
        const float (* ptr)[MONTHS];
        ptr = rain;
        
        float subtot, total;
        printf(" YEAR  RAINFALL (inches)
    ");
        for(year = 0, total = 0; year < YEARS; year++)
        {
            for(month = 0, subtot = 0; month < MONTHS; month++)
            {
                subtot += *(*(ptr + year) + month);
            }    
            printf("%5d  %15.1f
    ", 2010 + year, subtot);
            total += subtot; 
        }
        printf("
    The yearly average is %.1f inches.
    
    ", total / YEARS);
        
        printf("MONTHLY AVERAGE:
    
    ");
        printf("  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov Dec
    ");
    
        
        for(month = 0; month < MONTHS; month++)
        {
            for(year = 0, subtot = 0; year < YEARS; year++)
            {
                subtot += *(*(ptr + year) + month);
            }
            printf("%4.1f ", subtot / YEARS);    
        } 
        printf("
    ");
        
        return 0;
    }

    2、

    #include <stdio.h>
    
    #define SIZE 5
    
    void copy_arr(double son1[], double mother[], int n);
    void copy_ptr(double son2[], double mother[], int n);
    void copy_ptrs(double son3[], double mother[], double * ptr);
    
    int main(void)
    {
        double data[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5};
        double target1[SIZE];
        double target2[SIZE];
        double target3[SIZE];
        
        double * ptr = data; 
        copy_arr(target1, data, SIZE);
        copy_ptr(target2, data, SIZE);
        copy_ptrs(target3, data, ptr + SIZE);
        
        int i;
        for(i = 0; i < SIZE; i++)
        {
            printf("target1_%d: %.1f
    ", i, target1[i]);
            printf("target2_%d: %.1f
    ", i, target2[i]);
            printf("target3_%d: %.1f
    ", i, target1[i]);
        }
        
        return 0;
    }
    
    void copy_arr(double son1[], double mother[], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            son1[i] = mother[i];
        }
    }
    
    void copy_ptr(double son2[], double mother[], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(son2 + i) = *(mother + i);
        }
    }
    
    void copy_ptrs(double son3[], double mother[], double * ptr)
    {
        double * ptrtem = mother;
        
        while(ptrtem < ptr)
        {
            *son3++ = *ptrtem++;
        }    
    }

    #include <stdio.h>
    
    void copy_arr(double t[], double s[], int n);
    void copy_ptr(double *t, double *s, int n);
    void copy_ptrs(double *t, double *s_first, double *s_last);
    
    int main(void)
    {
        double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
        double target1[5];
        double target2[5];
        double target3[5];
        
        copy_arr(target1, source, 5);
        copy_ptr(target2, source, 5);
        copy_ptrs(target3, source, source + 5);
        
        int i;
        for(i = 0; i < 5; i++)
        {
            printf("target1: %d : %.1f.
    ", i, target1[i]);
            printf("target2: %d : %.1f.
    ", i, target2[i]);
            printf("target3: %d : %.1f.
    ", i, target3[i]);
        }
        
        return 5;    
    } 
    
    void copy_arr(double t[], double s[], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            t[i] = s[i];
        }
    }
    
    void copy_ptr(double *t, double *s, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }
    
    void copy_ptrs(double *t, double *s_first, double *s_last)
    {
        int i;
        for(i = 0; (s_last - s_first) > i; i++)
        {
            *(t + i) = *(s_first + i);
        }
    }

    3、

    #include <stdio.h>
    
    #define SIZE 6
    
    int get_max(int ar[], int n);
    
    int main(void)
    {
        int data[SIZE] = {3, 8, 19, -3, 13, 7};
        
        printf("the max is: %d.
    ", get_max(data, SIZE));
        
        return 0;
    }
    
    int get_max(int ar[], int n)
    {
        int i;
    
        int max = ar[0];
        
        for(i = 1; i < n; i++)
        {
            if(ar[i] > max)
            {
                max = ar[i];
            }
        }
        return max;
    }

    #include <stdio.h>
    
    #define SIZE 7
    
    int max_get(int * ptr, int n);
    
    int main(void)
    {
        int data[SIZE] = {3, 87, 7, 8, 3, 900, 22};
        
        printf("the max is: %d.
    ", max_get(data, SIZE));
        
        return 0;
    }
    
    int max_get(int * ptr, int n)
    {
        int i;
        int max = *ptr;
        
        for(i = 1; i < n; i++)
        {
            if(max < *(ptr + i))
            {
                max = *(ptr + i);
            }
        }
        return max;
    }

    #include <stdio.h>
    
    int get_max(int number[], int n);
    
    int main(void)
    {
        int source[100] = {1, 8, 2, 9, 30, 4};
        
        printf("the max: %d.
    ", get_max(source, 100));
        
        return 0;
    }
    
    int get_max(int number[], int n)
    {
        int i;
        int max = number[0];
        
        for(i = 0; i < n; i++)
        {
            if(max < number[i])
            {
                max = number[i];
            }
        }
        return max;
    }

    4、

    #include <stdio.h>
    
    #define SIZE 6
    
    int get_index(double ar[], int n);
    
    int main(void)
    {
        double data[SIZE] = {2.8, 1.7, 8.7, 19.1, 9.3, 6.5 };
        
        printf("the index of max element: %d.
    ", get_index(data, SIZE));
        
        return 0;
    }
    
    int get_index(double ar[], int n)
    {
        int i;
        int index;
        
        double max = ar[0];
        
        for(i = 0; i < n; i++)
        {
            if(max <= ar[i])
            {
                max = ar[i];
                index = i;
            }
        }
        
        return index;
    }

    #include <stdio.h>
    
    #define SIZE 6
    
    int max_index(double * ptr, int n);
    
    int main(void)
    {
        double data[SIZE] = {3.8, 11.2, 8.2, 2.3, 7.6, 4.5};
            
        printf("the max_index: %d.
    ", max_index(data, SIZE));
        
        return 0;
    }
    
    int max_index(double * ptr, int n)
    {
        int i, index;
        double max = *ptr;
        
        for(i = 0; i < n; i++)
        {
            if(max <= *(ptr + i))
            {
                max = *(ptr + i);
                index = i;    
            }    
        } 
        return index;
    }

    #include <stdio.h>
    
    int get_max_index(double number[], int n);
    
    int main(void)
    {
        double source[100] = {2.5, 3.2, 1.2, 31.6, 2.4, 0.0, 7.3};
        printf("the max_index: %d.
    ", get_max_index(source, 100));
        
        return 0;
    }
    
    int get_max_index(double number[], int n)
    {
        double max = number[0];
        int index = 0;
        int i;
        
        for(i = 0; i < n; i++)
        {
            if(max < number[i])
            {
                max = number[i];
                index = i;
            }
        }
        return index;
    }

    5、

    #include <stdio.h>
    
    #define SIZE 6
    
    double diff(double arg[], int n);
    
    int main(void)
    {
        double data[SIZE] = {2.8, 0.2, 3.2, 1.2, 8.8, 2.2};
        
        printf("diff: %.1f.
    ", diff(data, SIZE));
        
        return 0;
    }
    
    double diff(double arg[], int n)
    {
        int i;
        double min = arg[0];
        double max = arg[0];
        
        for(i = 0; i < n; i++)
        {
            if(min > arg[i])
            {
                min = arg[i];
            }
            if(max < arg[i])
            {
                max = arg[i];
            }
        }
        return max - min;
    }

    #include <stdio.h>
    
    #define SIZE 6
    
    double get_diff(double * ptr, int n);
    
    int main(void)
    {
        double data[SIZE] = {2.1, 3.3, 1.0, 7.8, 3.2, 4.4};
        
        printf("the diff: %.1f.
    ", get_diff(data, SIZE));
        
        return 0;
    }
    
    double get_diff(double * ptr, int n)
    {
        int i;
        double min = *ptr;
        double max = *ptr;
        
        for(i = 0; i < n; i++)
        {
            if(min > *(ptr + i))
            {
                min = *(ptr + i);
            }
            
            if(max < *(ptr + i))
            {
                max = *(ptr + i);
            }
        }
        
        return max - min;
    }

    #include <stdio.h>
    
    double get_range(double number[], int n);
    
    int main(void)
    {
        double source[6] = {2.5, 0.9, 0.3, 0.9, 1.4, 7.9};
        
        printf("the diff: %.1f.
    ", get_range(source, 6));
        
        return 0;
    }
    
    double get_range(double number[], int n)
    {
        double max = number[0];
        double min = number[0];
        int i;
        
        for(i = 0; i < n; i++)
        {
            if(max < number[i])
            {
                max = number[i];
            }
            if(min > number[i])
            {
                min = number[i];
            }
        }
        return max - min;
    }

    6、

    #include <stdio.h>
    
    #define SIZE 5
    
    void reverse(double arg[], int n);
    
    int main(void)
    {
        double data[SIZE] = {3.8, 1.8, 8.7, 9.2, 7.7};
        
        reverse(data, SIZE);
        
        return 0;
    }
    
    void reverse(double arg[], int n)
    {
        int i;
        
        for(i = 0; i < n; i++)
        {
            printf("%.1f ", arg[SIZE - 1 - i]);
        }
        putchar('
    ');
    }

    #include <stdio.h>
    
    #define SIZE 5
    
    void invert(double * ptr, int n);
    
    int main(void)
    {
        double data[SIZE] = {3.3, 4.4, 7.7, 9.9, 5.5};
        
        invert(data, SIZE);
        
        return 0;    
    } 
    
    void invert(double * ptr, int n)
    {
        int i;
        
        for(i = 0; i < n; i++)
        {
            printf("%.1f  ", *(ptr + n - 1 - i));
        }
    }

    #include <stdio.h>
    
    #define SIZE 5
    
    void inverse(double arg[], int n);
    
    int main(void)
    {
        double data[SIZE] = {88, 77, 33, 99, 44};
        
        inverse(data, SIZE);
        
        return 0;
    }
    
    void inverse(double arg[], int n)
    {
        int i;
        double temp;
        
        for(i = 0; i < n/2; i++)
        {
            temp = arg[i];
            arg[i] = arg[n - 1 - i];
            arg[n - 1 - i] = temp;
        }
        
        for(i = 0; i < n; i++)
        {
            printf("%.1f  ", arg[i]);
        }
    }

    #include <stdio.h>
    
    #define SIZE 6
    
    void reverse(double * ptr, int n);
    
    int main(void)
    {
        double data[SIZE] = {22, 11, 77, 44, 88, 99};
        
        reverse(data, SIZE);
        
        return 0;
    }
    
    void reverse(double * ptr, int n)
    {
        int i;
        double temp;
        
        for(i = 0; i < n/2; i++)
        {
            temp = *(ptr + i);
            *(ptr + i) = *(ptr + n - 1 - i);
            *(ptr + n - 1 - i) = temp;
        }
        
        for(i = 0; i < n; i++)
        {
            printf("%.1f  ", *(ptr + i));
        }
    }

    #include <stdio.h>
    
    void r_sort(double number[], int n);
    
    int main(void)
    {
        double source[12] = {2.5, 3.2, 1.2, 1.6, 2.4, 0.1, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3};    
        
        int i;
        for(i = 0; i < 12; i++)
        {
            printf("%g ", source[i]);
        }
        putchar('
    ');
        
        r_sort(source, 12);
        for(i = 0; i < 12; i++)
        {
            printf("%g ", source[i]);
        }
        putchar('
    ');
        
        return 0;
    }
    
    void r_sort(double number[], int n)
    {
        int i, j; double temp;
        
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n; j++)
            {
                if(number[j] < number[j + 1])
                {
                    temp = number[j];
                    number[j] = number[j + 1];
                    number[j + 1] = temp;
                }
            }
        }
    }

    #include <stdio.h>
    
    #define SIZE 6
    
    void sort(double * ptr, int n);
    
    int main(void)
    {
        double data[SIZE] = {3, 7, 2, 48, 99, 6};
        int i;
        
        sort(data, SIZE);
        
        for(i = 0; i < SIZE; i++)
        {
            printf("%g ", data[i]);
        }
        
        return 0;
    }
    
    void sort(double * ptr, int n)
    {
        int i, j;
        double temp;
        
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n; j++)
            {
                if(*(ptr + j) < *(ptr + j + 1))
                {
                    temp = *(ptr + j);
                    *(ptr + j)  = *(ptr + j + 1);
                    *(ptr + j + 1) = temp;
                }
            }
        }
    }

    #include <stdio.h>
    
    void r_sort(double number[], int n);
    
    int main(void)
    {
        double source[12] = {2.5, 3.2, 1.2, 1.6, 2.4, 0.1, 5.2, 0.9, 0.3, 0.9, 1.4, 7.3};
        int i;
        
        for(i = 0; i < 12; i++)
        {
            printf("%g ", source[i]);
        }
        putchar('
    ');
        
        r_sort(source, 12);
        for(i = 0; i < 12; i++)
        {
            printf("%g ", source[i]);
        }
        return 0;
    }
    
    void r_sort(double number[], int n)
    {
        double temp;
        int i, j;
        
        for( i = 0; i < n - 1; i++)
        {
            for(j = 0; j < n - 1 - i; j++)
            {
                if(number[j] < number[j + 1])
                {
                    temp = number[j];
                    number[j] = number[j + 1];
                    number[j + 1] = temp;
                }
            }
        }
    }

    7、

    #include <stdio.h>
    
    #define ROWS 4
    #define COLS 2
    
    void copy_ptr(double *t, double *s, int n);
    void copy_2d_ptr(double (*t)[ROWS], double (*s)[ROWS], int n);
    
    int main(void)
    {
        double target[COLS][ROWS];
        double source[COLS][ROWS] = {{4.3, 4.3, 4.3, 3.0},{8.5, 8.2, 1.2, 1.6}};
        copy_2d_ptr(target, source, COLS);
        
        int i, j;
        for( i = 0; i < COLS; i++)
        {
            for(j = 0; j < ROWS; j++)
            {
                printf("%.1f  ", target[i][j]);
            }
            putchar('
    ');
        }
        return 0;
    }
    
    void copy_ptr(double *t, double *s, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }
    
    void copy_2d_ptr(double (*t)[ROWS], double (*s)[ROWS], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            copy_ptr(*(t + i), *(s + i), ROWS);
        }
    }

    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 4
    
    void copy_array_1d(double s[], double t[], int n);
    void copy_array_2d(double s[][COLS], double t[][COLS], int n);
    
    int main(void)
    {
        double data[ROWS][COLS] = {{3, 2, 7, 9},{1, 4, 5, 8}};
        double target[ROWS][COLS];
        
        int i;
        copy_array_2d(data, target, ROWS);
        
        printf("show the copy result: 
    ");
        int j;
        for(i = 0; i < ROWS; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("%.1f  ", target[i][j]);
            }
            putchar('
    ');
        }
        
        return 0;
    }
    
    void copy_array_1d(double s[], double t[], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            t[i] = s[i];
        }
    }
    
    void copy_array_2d(double s[][COLS], double t[][COLS], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            copy_array_1d(s[i], t[i], COLS);
        }
    }

    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 4
    
    void array1_copy(double * s, double * t, int n);
    void array2_copy(double (* s)[COLS], double (* t)[COLS], int n);
    
    int main(void)
    {
        double data[ROWS][COLS] = {{2, 3, 7, 6},{1, 5, 4, 9}};
        double target[ROWS][COLS];
        
        array2_copy(data, target, ROWS);
        
        int i, j;
        for(i = 0; i < ROWS; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("%.1f  ", target[i][j]);
            }
            putchar('
    ');
        }
        
        return 0;
    }
    
    void array1_copy(double * s, double * t, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }
    
    void array2_copy(double (* s)[COLS], double (* t)[COLS], int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            array1_copy(*(s + i), *(t + i), COLS);
        }
    }

    8、

    #include <stdio.h>
    
    #define SIZE 7
    
    void copy_array(double *s, double *t, int n);
    
    int main(void)
    {
        double data[SIZE] = {3, 7, 2, 8, 4, 6, 9};
        double target[3];
        
        double * ptr = data + 2;
        copy_array(ptr, target, 3);
        
        int i;
        for(i = 0; i < 3; i++)
        {
            printf("target[%d]: %.1f.
    ", i, target[i]);
        }
        
        return 0;
    } 
    
    void copy_array(double *s, double *t, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }

    #include <stdio.h>
    #include <stdlib.h>
    
    void copy_ptr(double *t, double *s, int n);
    
    int main(void)
    {
        double src[] = {1, 2, 3, 4, 5, 6, 7};
        double targ[3];
        
        copy_ptr(targ, src + 2, 3);
        
        int i;
        for(i = 0; i < 3; i++)
        {
            printf("targ[%d]: %.1f.
    ", i, targ[i]);
        }
        
        return 0;
    }
    
    void copy_ptr(double *t, double *s, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }

    #include <stdio.h>
    
    #define SIZE 7
    
    void array_copy(double *s, double *t, int n);
    
    int main(void)
    {
        double data[SIZE] = {1, 2, 3, 4, 5, 6, 7};
        double target[3];
        
        array_copy(data + 2, target, 3);
        
        int i;
        for(i = 0; i < 3; i++)
        {
            printf("%.1f  ", target[i]);
        }
        printf("
    ");
        
        return 0;
    }
    
    void array_copy(double *s, double *t, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(t + i) = *(s + i);
        }
    }

     9、

    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 6
    
    void copy_array(int rows, int cols, double s[rows][cols], double t[rows][cols]);
    void show_array(int rows, int cols, double s[rows][cols]);
    
    int main(void)
    {
        double source[ROWS][COLS] = {{3, 2, 8, 5, 9, 4},{3, 6, 2, 1, 7, 9}};
        double target[ROWS][COLS];
        show_array(ROWS, COLS, source);
        
        puts("===========================
    ");
        copy_array(ROWS, COLS, source, target);
        show_array(ROWS, COLS, target);
        
        return 0;
    }
    
    void copy_array(int rows, int cols, double s[rows][cols], double t[rows][cols])
    {
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                t[i][j] = s[i][j];
            }
        }
    }
    
    void show_array(int rows, int cols, double s[rows][cols])
    {
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("%5.1f  ", s[i][j]);
            }
            putchar('
    ');
        }
    }

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void copy_array(int n, int m, double target[n][m], double source[n][m]);
    void show_array(int n, int m, double array[n][m]);
    
    int main(void)
    {
        double target[ROWS][COLS];
        double source[ROWS][COLS] = {
        {0.2, 0.4, 2.4, 3.5, 6.6},
        {8.5, 8.2, 1.2, 1600, 2.4},
        {9.1, 8.5, 2.3, 6.1, 8.4}
        };
        
        copy_array(ROWS, COLS, target, source);
        show_array(ROWS, COLS, target);
        
        return 0;
    }
    
    void copy_array(int n, int m, double target[n][m], double source[n][m])
    {
        int i, j;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < m; j++)
            {
                target[i][j] = source[i][j];
            }
        }
    }
    
    void show_array(int n, int m,double array[n][m])
    {
        int i, j;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < m; j++)
            {
                printf("%.1f  ", array[i][j]);
            }
            putchar('
    ');
        }
    }

    10、

    #include <stdio.h>
    
    #define SIZE 4
    
    void plus(int array1[], int array2[], int array[3], int n);
    
    int main(void)
    {
        int data1[SIZE] = {2, 4, 5, 8};
        int data2[SIZE] = {1, 0, 4, 6};
        int data3[SIZE] = {0};
        int i;
        
        plus(data1, data2, data3, SIZE);
        
        for(i = 0; i < SIZE; i++)
        {
            printf("%d  ", data3[i]);
        }
        putchar('
    ');
        
        return 0;
    }
    
    void plus(int array1[], int array2[], int array3[], int n)
    {
        int i;
        
        for(i = 0; i < n; i++)
        {
            array3[i] = array1[i] + array2[i];
        }
    }

    #include <stdio.h>
    
    #define SIZE 4
    
    void add(int * ptr1, int * ptr2, int * ptr3, int n);
    
    int main(void)
    {
        int array1[SIZE] = {20, 4, 500, 8};
        int array2[SIZE] = {1, 100, 4, 60};
        int array3[SIZE] = {0};
        
        add(array1, array2, array3, SIZE);
        
        int i;
        for(i = 0; i < SIZE; i++)
        {
            printf("%d  ", array3[i]);
        }
        
        return 0;
    }
    
    void add(int *ptr1, int *ptr2, int *ptr3, int n)
    {
        int i;
        for(i = 0; i < n; i++)
        {
            *(ptr3 + i) = *(ptr1 + i) + *(ptr2 + i);
        }
    }

    #include <stdio.h>
    
    #define INDEX 4
    
    void add_array(int n, int t[], int s1[], int s2[]);
    
    int main(void)
    {
        int sum[INDEX], s1[INDEX] = {2, 4, 5, 8}, s2[INDEX] = {1, 200, 4, 6};
        
        add_array(INDEX, sum, s1, s2);
        
        int i;
        for(i = 0; i < INDEX; i++)
        {
            printf("%d ", sum[i]);
        }
        putchar('
    ');
        
        return 0;
    }
    
    void add_array(int n, int t[], int s1[], int s2[])
    {
        int i;
        for(i = 0; i < n; i++)
        {
            t[i] = s1[i] + s2[i];
        }
    }

    11、

    #include <stdio.h>
    
    #define ROWS  2
    #define COLS  4
    
    void show(int arg[][COLS], int n);
    void dup(int arg[][COLS], int n);
    
    int main(void)
    {
        int data[2][4] = {{20, 34, 13, 26},{17, 28, 19, 40}};
        
        show(data, ROWS);
        dup(data, ROWS);
        show(data, ROWS);
        
        return 0;
    }
    
    void show(int arg[][COLS], int n)
    {
        int r, c;
        for(r = 0; r < n; r++)
        {
            for(c = 0; c < COLS; c++)
            {
                printf("%d  ", arg[r][c]);
            }
        }
        putchar('
    ');
    }
    
    void dup(int arg[][COLS], int n)
    {
        int r, c;
        for(r = 0; r < n; r++)
        {
            for(c = 0; c < COLS; c++)
            {
                arg[r][c] *= 2;
            }
        }
    }

    #include <stdio.h>
    
    #define ROWS 2
    #define COLS 4
    
    void exhibit(int (* arg)[4], int n);
    void dup2(int (* arg)[4], int n);
    
    int main(void)
    {
        int data[ROWS][COLS] = {{22, 34, 23, 11},{30, 13, 43, 26}};
        
        exhibit(data, ROWS);
        dup2(data, ROWS);
        exhibit(data, ROWS);
        
        return 0;
    }
    
    void exhibit(int (* arg)[4], int n)
    {
        int i, j;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("%d  ", *(*(arg + i) + j));
            }
        }
        putchar('
    ');
    }
    
    void dup2(int (* arg)[4], int n)
    {
        int i, j;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                *(*(arg + i) + j) *= 2;
            }
        }
    }

    #include <stdio.h>
    
    #define COLS 5
    #define ROWS 3
    
    void show_element(int rows, int cols, int t[rows][cols]);
    void double_element(int rows, int cols, int t[rows][cols]);
    
    int main(void)
    {
        int arr[ROWS][COLS] = {{1, 0, 4, 6, 9},{2, 5, 6, 8, 3},{5, 3, 21, 1, 6}};
        show_element(ROWS,COLS, arr);
        double_element(ROWS, COLS, arr);
        printf("
    ");
        show_element(ROWS, COLS, arr);
        
        return 0;
    }
    
    void show_element(int rows, int cols, int t[rows][cols])
    {
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("%3d ", t[i][j]);    
            }    
        }    
    } 
    
    void double_element(int rows, int cols, int t[rows][cols])
    {
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                t[i][j] *= 2;
            }
        }
        
    }

    12、

    #include <stdio.h>
    
    #define MONTHS 12
    #define YEARS 5
    
    void year(double ar[][MONTHS], int rows, int cols);
    void sum(double ar[][MONTHS], int rows, int cols);
    void mean_month(double ar[][MONTHS], int rows, int cols);
    
    int main(void)
    {
        double 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}
        };
        
        year(rain, YEARS, MONTHS);
        sum(rain, YEARS, MONTHS);
        mean_month(rain, YEARS, MONTHS);
        
        return 0;
    }
    
    void year(double ar[][MONTHS], int rows, int cols)
    {
        int i, j;
        double temp;
        for(i = 0; i < rows; i++)
        {
            for(temp = 0, j = 0; j < cols; j++)
            {
                temp += ar[i][j];
            }
            printf("%.1f.
    ", temp);
        }
        putchar('
    ');
    }
    
    void sum(double ar[][MONTHS], int rows, int cols)
    {
        int i, j;
        double sum = 0;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                sum += ar[i][j];
            }
        }
        printf("AVERAGE_year: %.1f.
    
    ", sum/rows);
    }
    
    void mean_month(double ar[][MONTHS], int rows, int cols)
    {
        int i, j;
        double temp;
        for(i = 0; i < cols; i++)
        {
            for(temp = 0, j = 0; j < rows; j++)
            {
                temp += ar[j][i];
            }
            printf("%.1f  ", temp/rows);
        }
        putchar('
    ');
    }

    #include <stdio.h>
    
    #define MONTHS 12
    #define YEARS 5
    void year_average(int years, int months, float t[years][months]);
    void month_average(int years, int months, float t[years][months]);
    
    int main(void)
    {
        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}
        };
        year_average(YEARS, MONTHS, rain);
        month_average(YEARS, MONTHS, rain);
        
        return 0;
    }
    
    void year_average(int years, int months, float t[years][months])
    {
        int i, j;
        float subtot, total;
        for(i = 0, total = 0; i < years; i++)
        {
            for(j = 0, subtot = 0; j < months; j++)
            {
                subtot += t[i][j];
            }
            printf("%d: %.1f
    ", 2010 + i, subtot);
            total += subtot;
        }
        printf("year_average: %.1f.
    ", total/years);
    }
    
    void month_average(int years, int months, float t[years][months])
    {
        int i, j;
        float subtot;
        for(i = 0; i < months; i++)
        {
            for(subtot = 0, j = 0; j < years; j++)
            {
                subtot += t[j][i];
            }
            printf("%.1f  ", subtot/years);
        }
    }

    13、

    #include <stdio.h>
    #define ROWS 3
    #define COLS 5
    
    void get_row_average(double ar[], int n);
    double get_all_average(double ar[][COLS], int n);
    double get_max(double ar[][COLS], int n);
    
    int main(void)
    {
        int i, j;
        double data[ROWS][COLS];
        
        printf("please input 15 double number.
    ");
        for(i = 0; i < ROWS; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("data[%d][%d] = ", i, j);
                scanf("%lf", &data[i][j]);    
            }    
        } 
        
        for(i = 0; i < ROWS; i++)
        {
            printf("line %d: ", i + 1);
            get_row_average(data[i], COLS);
        }
        
        printf("all_average: %.1f.
    ", get_all_average(data, ROWS));
        
        printf("the max: %.1f.
    ", get_max(data, ROWS));
        
        return 0;
    }
    
    void get_row_average(double ar[], int n)
    {
        int i;
        double temp = 0;
        for(i = 0; i < n; i++)
        {
            temp += ar[i];
        }
        printf("average: %.1f.
    ", temp/n);
    }
    
    double get_all_average(double ar[][COLS], int n)
    {
        int i, j;
        double sum = 0;
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                sum += ar[i][j];    
            }    
        }    
        return sum/(n * COLS);
    }
    
    double get_max(double ar[][COLS], int n)
    {
        int i, j;
        double max = ar[0][0];
        
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                if(max < ar[i][j])
                {
                    max = ar[i][j];
                }
            }
        }
        return max;
    }

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void input_array(int rows, double arr[][COLS]);
    
    double col_average(int cols, double arr[]);
    double array_average(int rows, double arr[][COLS]);
    double array_max_number(int rows, double arr[][COLS]);
    void show_result(int rows, double arr[][COLS]);
    
    int main(void)
    {
        double array[ROWS][COLS];
        input_array(ROWS, array);
        show_result(ROWS, array);
        printf("
    ");
        
        
        
        return 0;
    }
    
    void input_array(int rows, double arr[][COLS])
    {
        printf("Enter the array number.
    ");
        int i, j;
        for(i = 0; i < rows; i++)
        {
            printf("Enter five double number by enter:
    ");
            for(j = 0; j < COLS; j++)
            {
                scanf("%lf", &arr[i][j]);
            }
        }
    }
    
    double col_average(int cols, double arr[])
    {
        double sum = 0;
        int i;
        for(i = 0; i < cols; i++)
        {
            sum += arr[i];
        }
        return sum/cols;
    }
    
    double array_average(int rows, double arr[][COLS])
    {
        double sum = 0;
        int i;
        for(i = 0; i < rows; i++)
        {
            sum += col_average(COLS, arr[i]);
        }
        return sum/rows;
    }
    
    double array_max_number(int rows, double arr[][COLS])
    {
        double max = arr[0][0];
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                if(max < arr[i][j])
                {
                    max = arr[i][j];    
                }    
            }    
        } 
        return max;
    }
    
    void show_result(int rows, double arr[][COLS])
    {
        printf("Now, let's check the array!
    ");
        printf("The array you input is:
    ");
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("%5g", arr[i][j]);
            }
        }
        putchar('
    ');
        
        printf("The avrage of every colum is. 
    ");
        
        for(i = 0; i < rows; i++)
        {
            printf("The average is %g.
    ", col_average(COLS, arr[i]));
        }
        
        printf("The array's data average is %g
    ", array_average(ROWS, arr));
        printf("The max datum in the array is %g
    ", array_max_number(ROWS, arr));
    }

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void get_array(int rows, double ar[][COLS]);
    double row_mean(int num, double ar[]);
    double all_mean(int rows, double ar[][COLS]);
    double get_max(int rows, double ar[][COLS]);
    void show_outcome(int rows, double ar[][COLS]);
    
    int main(void)
    {
        double data[ROWS][COLS];
        get_array(ROWS, data);
        show_outcome(ROWS, data);
        
        return 0;
    }
    
    void get_array(int rows, double ar[][COLS])
    {
        int i, j;
        for(i = 0; i < rows; i++)
        {
            printf("input five double num(then enter): ");
            for(j = 0; j < COLS; j++)
            {
                scanf("%lf", &ar[i][j]);
            }
        }
    }
    
    double row_mean(int num, double ar[])
    {
        int i;
        double sum = 0;
        for(i = 0; i < num; i++)
        {
            sum += ar[i];
        }
        return sum/num;
    }
    
    double all_mean(int rows, double ar[][COLS])
    {
        int i;
        double sum = 0;
        for(i = 0; i < rows; i++)
        {
            sum += row_mean(COLS, ar[i]);
        }
        return sum/rows;
    }
    
    double get_max(int rows, double ar[][COLS])
    {
        int i, j;
        double max = ar[0][0];
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                if(max < ar[i][j])
                {
                    max = ar[i][j];
                }
            }
        }
        return max;
    }
    
    void show_outcome(int rows, double ar[][COLS])
    {
        printf("Now let us check the result!
    ");
        
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("%.1f  ", ar[i][j]);
            }
        }
        putchar('
    ');
        
        for(i = 0; i < rows; i++)
        {
            printf("lines %d average: %.1f.
    ", i + 1, row_mean(COLS, ar[i]));
            
        }
        
        printf("all mean: %.1f   
    ", all_mean(rows, ar));
        printf("the  max: %.1f   
    ", get_max(rows, ar)); 
    }

    14、

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void get_line_average(int num, double ar[num]);
    
    double get_all_average(int rows, int cols, double ar[rows][cols]);
    
    double get_max(int rows, int cols, double ar[rows][cols]);
    
    int main(void)
    {
        double data[ROWS][COLS];
        int i, j;
        
        printf("please input 15 double numbers.
    ");
        for(i = 0; i < ROWS; i++)
        {
            for(j = 0; j < COLS; j++)
            {
                printf("data[%d][%d]: ", i, j);
                scanf("%lf", &data[i][j]);
            }
        }
        
        for(i = 0; i < ROWS; i++)
        {
            get_line_average(COLS, data[i]);
        }
        
        printf("all_average: %.1f.
    ", get_all_average(ROWS, COLS, data));
        
        printf("the max: %.1f.
    ", get_max(ROWS, COLS, data));
        
        return 0;
    }
    
    void get_line_average(int num, double ar[num])
    {
        int i;
        double temp = 0;
        for(i = 0; i < num; i++)
        {
            temp += ar[i];
        }
        printf("average: %.1f.
    ", temp/num);
    }
    
    double get_all_average(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double sum = 0;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                sum += ar[i][j];
            }
        }
        return sum/(rows * cols);
    }
    
    double get_max(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double max = ar[0][0];
        
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                if(max < ar[i][j])
                {
                    max = ar[i][j];
                }
            }
        }
        return max;
    }

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void get_array(int rows, int cols, double ar[rows][cols]);
    
    void get_line_average(int num, double ar[num]);
    
    double get_all_average(int rows, int cols, double ar[rows][cols]);
    
    double get_max(int rows, int cols, double ar[rows][cols]);
    
    int main(void)
    {
        double data[ROWS][COLS];
        int i, j;
        
        get_array(ROWS, COLS, data);
        
        for(i = 0; i < ROWS; i++)
        {
            get_line_average(COLS, data[i]);
        }
        
        printf("all_average: %.1f.
    ", get_all_average(ROWS, COLS, data));
        
        printf("the max: %.1f.
    ", get_max(ROWS, COLS, data));
        
        return 0;
    }
    
    void get_array(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        printf("please input 15 double num.
    "); 
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("ar[%d][%d]: ", i, j);
                scanf("%lf", &ar[i][j]);
            }
        }
    }
    
    
    void get_line_average(int num, double ar[num])
    {
        int i;
        double temp = 0;
        for(i = 0; i < num; i++)
        {
            temp += ar[i];
        }
        printf("average: %.1f.
    ", temp/num);
    }
    
    double get_all_average(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double sum = 0;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                sum += ar[i][j];
            }
        }
        return sum/(rows * cols);
    }
    
    double get_max(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double max = ar[0][0];
        
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                if(max < ar[i][j])
                {
                    max = ar[i][j];
                }
            }
        }
        return max;
    }

    #include <stdio.h>
    
    #define ROWS 3
    #define COLS 5
    
    void get_array(int rows, int cols, double ar[rows][cols]);
    
    double get_line_mean(int num, double ar[num]);
    
    double get_all_mean(int rows, int cols, double ar[rows][cols]);
    
    double get_all_max(int rows, int cols, double ar[rows][cols]);
    
    void show_result(int rows, int cols, double ar[rows][cols]);
    
    int main(void)
    {
        double data[ROWS][COLS];
        
        get_array(ROWS, COLS, data);
        
        show_result(ROWS, COLS, data);
        
        return 0;
    }
    
    void get_array(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        printf("please input 15 double number.
    ");
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("data[%d][%d]: ", i, j);
                scanf("%lf", &ar[i][j]);
            }
        }
    }
    
    double get_line_mean(int num, double ar[num])
    {
        int i;
        double temp = 0;
        for(i = 0; i < num; i++)
        {
            temp += ar[i];
        }
        return temp/num;
    }
    
    double get_all_mean(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double sum;
        for(i = 0; i < rows; i++)
        {
            sum += get_line_mean(cols, ar[i]);
        }
        return sum/rows;
    }
    
    double get_all_max(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        double max = ar[0][0];
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                if(max < ar[i][j])
                {
                    max = ar[i][j];
                }
            }
        }
        return max;
    }
    
    void show_result(int rows, int cols, double ar[rows][cols])
    {
        int i, j;
        printf("Let us check the result!
    ");
        
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("%.1f  ", ar[i][j]);
            }
            putchar('
    ');
        }
        putchar('
    ');
        
        printf("Show the lien mean!
    ");
        
        for(i = 0; i < rows; i++)
        {
            printf("lines %d mean: %.1f
    ", i + 1, get_line_mean(cols, ar[i]));
        }
        
        printf("all mean: %.1f.
    ", get_all_mean(rows, cols, ar));
        
        printf("all max:  %.1f.
    ", get_all_max(rows, cols, ar));
    }

    #include <stdio.h>
    
    void input_array(int rows, int cols, double arr[rows][cols]);
    double col_average(int cols, double arr[cols]);
    double array_average(int rows, int cols, double arr[rows][cols]);
    double array_max_number(int rows, int cols, double arr[rows][cols]);
    void show_result(int rows, int cols, double arr[rows][cols]);
    
    int main(void)
    {
        int rows = 3;
        int cols = 5;
        double array[rows][cols];
        input_array(rows, cols, array);
        show_result(rows, cols, array);
        return 0;    
    } 
    
    void input_array(int rows, int cols, double arr[rows][cols])
    {
        int i, j;
        printf("Enter the array number.
    ");
        for(i = 0; i < rows; i++)
        {
            printf("Enter five double number seperate by enter:
    ");
            for(j = 0; j < cols; j++)
            {
                scanf("%lf", &arr[i][j]);
            }
        }
    }
    
    double col_average(int cols, double arr[cols])
    {
        double sum = 0;
        int i;
        for(i = 0; i < cols; i++)
        {
            sum += arr[i];
        }
        return sum/cols;
    }
    
    double array_average(int rows, int cols, double arr[rows][cols])
    {
        double sum = 0;
        int i;
        for(i = 0; i < rows; i++)
        {
            sum += col_average(cols, arr[i]);
        }
        return sum/rows;
    }
    
    double array_max_number(int rows, int cols, double arr[rows][cols])
    {
        double max = arr[0][0];
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                if(max < arr[i][j])
                {
                    max = arr[i][j];
                }
            }
        }
        return max;
    }
    
    void show_result(int rows, int cols, double arr[rows][cols])
    {
        printf("Now, let us check the result!
    ");
        printf("The array you input is!
     ");
        int i, j;
        for(i = 0; i < rows; i++)
        {
            for(j = 0; j < cols; j++)
            {
                printf("%5g", arr[i][j]);
            }
        }
        putchar('
    ');
        
        printf("The average of every line is!
    ");
        for(i = 0; i < rows; i++)
        {
            printf("the %d line's average is %g .
    ", i + 1, col_average(cols, arr[i]));
        }
        
        printf("The array data average is  %g  
    ", array_average(rows, cols, arr));
        printf("The max datum in the array is %g 
    ", array_max_number(rows, cols, arr));
    }

  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/15248352.html
Copyright © 2011-2022 走看看