BeautifulMan

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            16 隨筆 :: 0 文章 :: 0 評(píng)論 :: 0 Trackbacks
          這一章感覺好難啊!!!
          學(xué)習(xí)筆記:(關(guān)于指針和多維數(shù)組)
          // 多維數(shù)組和指針
          #include <stdio.h>
          int main(void)
          {
              int zippo[4][2] = {{2, 4}, {6, 8}, {1, 3}, {5, 7}};
              /*
                 zippo[0]是一個(gè)整數(shù)大小對(duì)象的地址,而zippo是兩個(gè)整數(shù)大小對(duì)象的地址。
                 因?yàn)椋ㄒ粋€(gè))整數(shù)和兩個(gè)整數(shù)組成的數(shù)組開始于同一個(gè)地址,因此zippo和zippo[0]具有相同的數(shù)值。
                 驗(yàn)證:
                 輸出也顯示出二維數(shù)組zippo的地址和一維數(shù)組zippo[0]的地址是相同的,均為相應(yīng)的數(shù)組
                 首元素的地址,它的值是和&zippo[0][0]相同的;
                 而且,*zippo也是一個(gè)指針,它與一維數(shù)組zippo[0](也是一個(gè)指針)的地址相同,證明了我的猜想!
              
          */
              printf("===========首先驗(yàn)證第一條結(jié)論===========\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所指向?qū)ο蟮拇笮∈莾蓚€(gè)int,而zippo[0]所指向?qū)ο蟮拇笮∈且粋€(gè)int
                 驗(yàn)證:
                 zippo[0]指向4字節(jié)長的數(shù)據(jù)對(duì)象。對(duì)zippo[0]加1導(dǎo)致它的值增加4。數(shù)組名zippo是包含
                 兩個(gè)int數(shù)的數(shù)組的地址,因此它指向8字節(jié)長的數(shù)據(jù)對(duì)象。所以,對(duì)zippo加1導(dǎo)致它的值增加8。
              
          */
              printf("===========然后驗(yàn)證第二條結(jié)論===========\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也是一個(gè)指針,它與一維數(shù)組zippo[0](也是一個(gè)指針)的地址相同,它們指向同一個(gè)int變量
                 即zippo[0][0]
                 *zippo[0] = zippo[0][0]
                 **zippo = *zippo[0] = zippo[0][0](得證)
                 ------------------------------------------------
                 分析*(*(zippo+2)+1)
                 zippo------------------第1個(gè)大小為2個(gè)int的元素的地址
                 zippo+2----------------第3個(gè)大小為2個(gè)int的元素的地址
                 *(zippo+2)-------------第3個(gè)元素,即包含2個(gè)int值的數(shù)組,因此也是其第1個(gè)元素的(int值)的地址
                 *(zippo+2)+1-----------包含2個(gè)int值的數(shù)組的第2個(gè)元素(int值)的地址
                 *(*(zippo+2)+1)--------數(shù)組第3行第2列int(zippo[2][1])的值

                 總結(jié):更一般地,要表示單個(gè)元素,可以使用數(shù)組符號(hào)或指針符號(hào);并且在這兩種表示中即可以使用
                 數(shù)組名,也可以使用指針:
                 zippo[m][n] == *(*(zippo+m)+n)
              
          */
              printf("===========最后驗(yàn)證第三條結(jié)論===========\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)
          {
              /*
                 指針之間的賦值規(guī)則比數(shù)值類型的賦值更嚴(yán)格
                 舉例說明:
              
          */
              int n = 5;
              double x;
              int * pi = &n;
              double * pd = &x;
              x = n;  // 不需要進(jìn)行類型轉(zhuǎn)換就直接把一個(gè)int數(shù)值賦給一個(gè)double類型的變量(隱藏的類型轉(zhuǎn)換)
              pd = pi // 編譯時(shí)錯(cuò)誤,原因:pd指向一個(gè)double類型的數(shù)值,pi指向一個(gè)int類型的數(shù)值

              int * pt;
              int (* pa) [3];
              int ar1[2][3];
              int ar2[3][2];
              int **p2; // (指向int指針)的指針
              pt = &ar1[0][0];   // pt為指向一個(gè)int數(shù)值的指針,ar1[0][0]是一個(gè)int數(shù)值的變量
              pt = ar1[0];       // pt為指向一個(gè)int數(shù)值的指針,ar1[0]也為指向一個(gè)int數(shù)值的指針
              pt = ar1;          // pt為指向一個(gè)int數(shù)值的指針,ar1指向由3個(gè)int值構(gòu)成的指針(非法)
              pa = ar1;          // pa指向由3個(gè)int值構(gòu)成的數(shù)組,ar1也指向由3個(gè)int值構(gòu)成的數(shù)組
              pa = ar2;          // pa指向由3個(gè)int值構(gòu)成的數(shù)組,ar2指向由2個(gè)int值構(gòu)成的數(shù)組(非法)
              p2 = &pt;          // p2是(指向int指針)的指針,&pt(頭一次見,得記下來)也是(指向int指針)的指針
              *p2 = ar2[0];      // *p2為指向int的指針,ar2[0]也是指向int的指針
              p2 = ar2;          // p2是(指向int指針)的指針,ar2是指向由2個(gè)int值構(gòu)成的數(shù)組(非法)
              return 0;
          }
          復(fù)習(xí)題
          1、下面程序?qū)⒋蛴∈裁矗?br />
          #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題中,數(shù)組ref包含多少個(gè)元素?
          答:4
          3、在第1題中,ref是哪些數(shù)據(jù)的地址?ref+1呢?++ref指向什么?
          答:
          數(shù)組名ref指向數(shù)組的第一個(gè)元素(整數(shù)8),表達(dá)式ref+1指向第二個(gè)元素(整數(shù)4)。++ref不是合法的C表達(dá)式,因?yàn)閞ef是常量而不是變量。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指向第三個(gè)元素,它是第二行的第一個(gè)元素,而不是不確定的。
          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、假設(shè)有如下定義:
          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]是整數(shù)元素grid[0][0]的地址,grid是具有100個(gè)元素的數(shù)組grid[0]的地址。這兩個(gè)地址具有相同的數(shù)值但是類型不同,類型指派可以使他們的類型相同)。
          7、用適當(dāng)?shù)姆椒暶飨旅婷總€(gè)變量:
          a.digits:一個(gè)包含10個(gè)int值的數(shù)組
          b.rates:一個(gè)包含6個(gè)float值的數(shù)組
          c.mat:一個(gè)包含3個(gè)元素的數(shù)組,其中每個(gè)元素是一個(gè)包含5個(gè)整數(shù)的數(shù)組
          d.psa:一個(gè)包含20個(gè)指向char的指針的數(shù)組
          e.pstr:一個(gè)指向數(shù)組的指針,其中數(shù)組由20個(gè)char值構(gòu)成
          答:
          a.
          int digits[10];
          b.
          float rates[6];
          c.
          int mat[3][5];
          d.
          char *(psa[20]); (psa是指針數(shù)組而不是指向數(shù)組的指針。具體地,psa會(huì)指向一個(gè)單個(gè)char(數(shù)組的第一個(gè)元素),psa+1會(huì)指向下一個(gè)字節(jié))
          e.
          char (*pstr) [20];
          8、
          a.定義一個(gè)包含6個(gè)int值的數(shù)組,并且用數(shù)值1、2、4、8、16和32進(jìn)行初始化。
          b.用數(shù)組符號(hào)表示a部分中數(shù)組的第3個(gè)元素(數(shù)值為4的那個(gè)元素)。
          c.假設(shè)系統(tǒng)支持C99規(guī)則,定義一個(gè)包含100個(gè)int值的數(shù)組并且初始化它,使它的末元素為-1,其他元素的值不考慮。
          答:
          a.
          int array[6] = {1, 2, 4, 8, 16, 32};
          b.
          array[2];
          c.
          int ar[100] = { [99] = -1 };
          9、包含10個(gè)元素的數(shù)組的索引范圍是什么?
          答:0~9
          10、假設(shè)有如下聲明:
          float rootbeer[10], things[10][5], *pf, value = 2.2;
          int i = 3;
          則下列語句中哪些是正確的,哪些是錯(cuò)誤的?
          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------錯(cuò)誤(注意:rootbeer不是一個(gè)float變量
          c------錯(cuò)誤
          d------錯(cuò)誤
          e------正確
          f ------錯(cuò)誤(注意:不能使用數(shù)組賦值)
          g------錯(cuò)誤
          h------正確
          11、聲明一個(gè)800x600的int數(shù)組。
          答:
          int array[800][600];
          12、以下是3個(gè)數(shù)組聲明:
          double trots[20];
          short clops[10][30];
          long shots[5][10][15];
          a.以傳統(tǒng)的void函數(shù)方式寫出處理數(shù)組trots的函數(shù)原型和函數(shù)調(diào)用;然后以變長數(shù)組方式,寫出處理數(shù)組trots的函數(shù)原型和函數(shù)調(diào)用。
          b.以傳統(tǒng)的void函數(shù)方式寫出處理數(shù)組clops的函數(shù)原型和函數(shù)調(diào)用;然后以變長數(shù)組方式,寫出處理數(shù)組clops的函數(shù)原型和函數(shù)調(diào)用。
          c.以傳統(tǒng)的void函數(shù)方式寫出處理數(shù)組shots的函數(shù)原型和函數(shù)調(diào)用;然后以變長數(shù)組方式,寫出處理數(shù)組shots的函數(shù)原型和函數(shù)調(diào)用。
          答:
          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、下面是兩個(gè)函數(shù)原型:
          void show(double ar[], int n);                     //n是元素?cái)?shù)
          void show2(double ar2[][3], int n);           //n是行數(shù) 
          a.編寫一個(gè)函數(shù)調(diào)用,把包含數(shù)值8、3、9和2的復(fù)合文字傳遞給函數(shù)shows()。
          b.編寫一個(gè)函數(shù)調(diào)用,把包含2行3列數(shù)值的復(fù)合文字傳遞給函數(shù)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);
          編程練習(xí)(哈哈哈!!!題目感覺越來越簡單了呢!除了最后一題外,好高興!!!)
          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("--------------輸出驗(yàn)證----------------\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("--------------輸出驗(yàn)證----------------\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、(沒必要做兩次循環(huán),一次循環(huán)就夠了,真是寫的羅里吧嗦的!)
          #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("--------------輸出驗(yàn)證----------------\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;
          }
          改進(jìn)之后的程序代碼:
          #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("--------------輸出驗(yàn)證----------------\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
          //對(duì)于每一年,計(jì)算各月的總降水量并把各個(gè)值存儲(chǔ)在一個(gè)數(shù)組中
          void fun1(float (*ye)[MONTHS], float * yea);
          // 對(duì)于每一年,顯示各月的總降水量
          void show_array1(float *ar);
          // 計(jì)算年降水平均量
          float sum1(float *ar);
          // 對(duì)于每個(gè)月,計(jì)算月降水平均量并把各個(gè)值存儲(chǔ)在一個(gè)數(shù)組中
          void fun2(float (*ye)[MONTHS], float * mon);
          // 對(duì)于每個(gè)月,顯示月降水平均量
          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));
          }
          有必要寫那么多的函數(shù)嗎?只須寫兩個(gè)函數(shù)就可以搞定的,非得寫那么多,改進(jìn)之后程序如下:
          #include <stdio.h>
          #define MONTHS 12
          #define YEARS 5
          // 計(jì)算年降水總量與所有年度的總降水量
          double calculate1(const float arr[][MONTHS], int y);
          // 計(jì)算各年該月份的總降水量
          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、(關(guān)于如何輸入數(shù)字,沒搞明白,還是借鑒CSDN----vs9841前輩的做法,不過后面都是自己寫了
          #include <stdio.h>
          #define ROWS 3
          #define COLS 5
          // 從鍵盤獲取一個(gè)double數(shù)
          double get_double(void);
          // 向source[ROWS][COLS]中輸入數(shù)值
          void input_double(int n, int m, double (*dou)[COLS]);
          // 計(jì)算每個(gè)數(shù)集的平均值
          void get_average(int n, int m, double (*dou)[COLS]);
          // 計(jì)算所有數(shù)值的平均值
          double get_all_average(int n, int m, double (*dou)[COLS]);
          // 找出所有數(shù)中的最大值
          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); // 記住每次只能處理輸入一個(gè)數(shù)
                      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;
          }
          第二次更新如下,完全自己手寫:(可能對(duì)自己好理解一點(diǎn))
          #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("所有數(shù)值的平均數(shù)為:%.2f\n", task_c(array, 3));
              printf("這15個(gè)數(shù)中的最大值為:%.2f\n", task_d(array, 3));
              printf("該3x5數(shù)組為:\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個(gè)數(shù)集\n");
              while(i < n)
              {
                  printf("請輸入第%d個(gè)數(shù)集:\n", i + 1);
                  count = 0;
                  printf("請輸入第%d個(gè)數(shù):", count + 1);
                  while(scanf("%lf", &num) == 1 && count < 5)
                  {
                      arr[i][count] = num;
                      if(count == 4)
                          break;
                      count++;
                      printf("請輸入第%d個(gè)數(shù):", 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個(gè)數(shù)集的平均值為: %.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("所有數(shù)值的平均數(shù)為:%.2f\n", task_c(3, 5, array));
              printf("這15個(gè)數(shù)中的最大值為:%.2f\n", task_d(3, 5, array));
              printf("該3x5數(shù)組為:\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個(gè)數(shù)集\n");
              while(i < n)
              {
                  printf("請輸入第%d個(gè)數(shù)集:\n", i + 1);
                  count = 0;
                  printf("請輸入第%d個(gè)數(shù):", count + 1);
                  while(scanf("%lf", &num) == 1 && count < m)
                  {
                      arr[i][count] = num;
                      if(count == 4)
                          break;
                      count++;
                      printf("請輸入第%d個(gè)數(shù):", 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個(gè)數(shù)集的平均值為: %.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
          // 從鍵盤獲取一個(gè)double數(shù)
          double get_double(void);
          // 向source[ROWS][COLS]中輸入數(shù)值
          void input_double(int n, int m, double dou[n][m]);
          // 計(jì)算每個(gè)數(shù)集的平均值
          void get_average(int n, int m, double dou[n][m]);
          // 計(jì)算所有數(shù)值的平均值
          double get_all_average(int n, int m, double dou[n][m]);
          // 找出所有數(shù)中的最大值
          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); // 記住每次只能處理輸入一個(gè)數(shù)
                      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 李阿昀 閱讀(860) 評(píng)論(0)  編輯  收藏 所屬分類: C Primer Plus 復(fù)習(xí)題與編程練習(xí)
          主站蜘蛛池模板: 信宜市| 常州市| 巴林右旗| 鄂托克前旗| 共和县| 辽阳县| 武强县| 九寨沟县| 香格里拉县| 安阳市| 集贤县| 哈巴河县| 曲阜市| 晋州市| 农安县| 老河口市| 资中县| 乳源| 本溪市| 于都县| 建德市| 伊金霍洛旗| 盘山县| 库车县| 察隅县| 马关县| 汨罗市| 大冶市| 砀山县| 白沙| 确山县| 根河市| 石嘴山市| 惠来县| 渝北区| 武义县| 天全县| 扶余县| 梧州市| 西林县| 会泽县|