LAB-03  @ Third week  Study how to use Loop to process an Array again

¥[°h¿ï¤w¸gºI¤î ..
    ¦pªG¹L¥h¨â¶g§A¨Ó¤£¤Î°Ñ»P .. ²{¦b¶}©l¤]ÁÙ¨Ó±o¤Î !
¾Çµ{¦¡¨S¦³±¶®|, ´N¬O­n¦h¦h½m²ß¦Ó¤w !
½Ð§â½m²ß¹Lµ{»P¤ß±o post ¨ì¥»®ÕLMS ( e3.nctu.edu.tw )°Q½×°Ï¤¤¤À¨Éµ¹¤j®a;
Note:
ƒÜ  §A¦b°Q½×°Ïªºµoªí¤å³¹(¥]¬A²ßÃD½m²ß¦¨ªG, °Ý°ÝÃD, ¦^µª¦P¾Ç°ÝÃDµ¥µ¥) 
  ±N¬O³o½Òµ{¾Ç´Á¤À¼Æªº¨Ì¾Ú! ½Ðª`·N§Ú­Ì¦³³W©w²ßÃD½m²ß¦¨ªGpostªº­Ó®æ¦¡:
(1)	¦¹ÃD½m²ß¤ß±o¡K
(2)	Running script (¼@¥», ´N¬O°õ¦æ¹Lµ{, ¥iÅý§O¤H·ÓµÛ°µ)
(3)	Source code, ­n±a¦³ Line numbers
(¥i¥Î§Ú´£¨Ñªº linenum.c ²£¥Í§Aµ{¦¡ªº Line numbers)
 
ƒÜ ¦pªG§A¦Û¤v¨t¤W­p·§½Òªº²ßÃD°µ¦n¤F¤]¥i¥H post ¦b§Ú­Ìªº LABs ½m²ß°Ï, 
¦ý½Ð¥[¤W¤@¨Ç¤ß±o»¡©ú¦b«e­±! ³o¼Ë¤]¦³¥[¤À!!

________________________________________________________________________
·R¤FµL«á®¬ .. ªü¤£¬O .. §Ú¬O»¡.. ¬JµM¿ï¤F³oªù½Ò .. ¨º´N¤£­n«á®¬ ! ­n¥[ªo !
________________________________________________________________________

§Æ±æ¤j®a¥i¥H«ù¤§¥H«í,  ¾¨¶q¤£¯Ê½Ò, ¤@°_¨Ó½m²ß, ¤@°_°Q½×,
¦³°ÝÃD(¥]¬A¥»½Òµ{ªº°ÝÃD»P§Aªº¥²­×½Ò­p·§ªº°ÝÃD)
´NÁ|¤â§ä¤p§U±Ð¹L¨Ó°Ý:-)

­è¶}©lı±o«ÜÃøªº¦P¾Ç¤]¤£­n¦Ç¤ß! ­n§Ô­@ ! §Ô­@¦³¨Ç¬Ý¤£À´  ¡K 
¥u­n§A¨C¶g³£¦³¨Ó½m²ß, ¤@©w·|¶i¨B !!!

* ¤@©w­n§â§Ú­Ìµ¹ªº¨Ò¤l´ú¸Õ¬Ý¬Ý, ³Ì¦nÁÙ¯à¶Ã§ï¦A´ú¸Õ, 
  ¦³¬Ý¤£À´ªº´N­n¥ß¨èÁ|¤â½Ð±Ð§U±Ð¸s! ©Ò¦³¨Ò¤l³£À´·íµM«Ü¦n, 
  ¦ý¬O¹J¨ì¸ûÃøÀ´±o, ¥ý¤£ºÞ¤]¬O¥i¥Hªº, ¦³¨Ç¥u­n¦A¹L¤@°}¤l¤£¤p¤ß¦ÛµM´N·|À´¤F ƒº  


 
LAB-03 @ Third week  Study how to use Loop to process an Array again  (p.1/4)
Problem:  Write a C program that can read many integers and then print:
  (a) How many integers you read? Á`¦@Ū¨ì´X­Ó¼Æ (¥Nªíµ²§ôªº¨º­Ó¤£ºâ) 
  (b) The average (in real number format, use double) ¥­§¡­È
  (c) How many data are greater than or equal to the average? ¦h¤Ö­Ó¤£¤p©ó¥­§¡­È? 
  (d) The sum of square of each data (¥­¤è©M) in double. 
  (e) The variance (Åܲ§¼Æ), in double.
  (f) The Standard deviation (¼Ð·Ç®t), in double.
Purpose:  To Master how to process an Array using Loops
Description:
  (a) Assume that each data ³£¬O¤@­Ó¥i©ñ¤J long ªº¾ã¼Æ; 
     ¿é¤J¬O¨C¦C¤@­Ó¾ã¼Æ, Your program must repeat reading data till
     ª½¨ìŪ¨ì -1 ¤~ªí¥Ü¸ê®Æµ²§ô(·íµM -1 ¤£ºâ), ©Î¬OŪ¨ì¨S¦³¸ê®Æ (EOF)
     ¤]­nµ²§ô¨Ã¦L¥X©Ò¦³³W©wªº¸ê®Æ.
  (b) ¦]¬°­nºâ¥X¥­§¡­È (average)«á¤~¦³¿ìªk count ¦³¦h¤Ö­Ó¸ê®Æ¤j©ó©Î
      µ¥©ó¥­§¡¼Æ, ©Ò¥H¥²¶·¥Î array §â¸ê®Æ¥ý¦s°_¨Ó; °²³]¸ê®Æ³Ì¦h¤£·|¶W
      ¹L 999 ­Ó,©Ò¥H§Aªº array ¦Ü¤Ö­n¦³ 999 ­Ó¤¸¯À.
  (c) ­pºâÅܲ§¼Æ(variance)ªº¤èªk°ª¤¤À³¸Ó¾Ç¹L, §Ñ±¼ªº¥i¥H¤Wºô¬d, 
      ¨ä¹ê¥u­n°O¦ísum(©M) »P ¥­¤è©M (sum of square) ´N¥i¥H¦³¤½¦¡­pºâ,
      ¤£»Ý¥Î¨ì array, ¦ý¬O¬JµM¤w¸g¦³ array °O¦í©Ò¦³Åª¤Jªº, 
      «h¥i¥H¥Î Loop ±q array ­pºâ.
  (d) ¼Ð·Ç®t(Standard deviation)«h¬OÅܲ§¼Æ¨ú¥­¤è®Ú, ¥i¥H¥Î sqrt( ) ¨ç¼Æ.
  (e) Áp¦Ò«á¥i¯àÅ¥¨ì°ê¤å¥­§¡ 65.36, ¼Ð·Ç®t 9.88, °²¦p§A¦Ò85.3¤À,
      ¤S°²³]¸Ó¦¸Áp¦Ò¦@¦³8¸U¤H°Ñ¥[, ½Ð°Ý§Aªº°ê¤å¤j¬ùŤF¦h¤Ö¤H?
      ³o­Ó°ÝÃD°ò¥»¤W¬O°²³]¤j®aªº¦¨ÁZ¬O§Î¦¨±`ºA¤À°t(Normal distribution),
      ¨Æ¹ê¤W¤]ªº½T¦p¦¹!  
      Ãö©ó±`ºA¤À°tªº¬ÛÃöª¾ÃÑ¥i¥H¤Wºô¬d¸ß¡¨Wiki ±`ºA¤À°t¡¨¬Ý¬Ý¤]«Ü¦³½ì!
  (f) «ä¦Ò: ¦pªG¨Ï¥ÎªÌ¿é¤J®É¶Ã¥´·|«ç¼Ë? 
          ¤S¦pªG¿é¤J®É¨C¦C¤£¥u¥´¤@­Ó¤]¤£ª¾¹D¨Ï¥ÎªÌ¨C¦C·|¥´´X­Ó, 
          §ó¤£ª¾¹D¨ì©³¬O¥ÎªÅ¥Õ¹j¶}©Î¬O¥Î³r¸¹¹j¶}©O??

  ** ¤U¶g½m²ß: Ū¤J ¾ã¼Æ n, ¦L¥X n ¶¥ Pascal Triangle (½Ð¥ý¤Wºô¬d¤Ú´µ¥d¤T¨¤§Î)

More Hints:
  (1)¬Ý§¹ÃD¥Ø¤w¸g·|¼gªº´Nª½±µ¶}©l¼g, ÁÙ¤£·|ªº½Ð¥ý¬Ý p30.c, p31.c µ¥½d¨Ò.
  (2)¨ä¹ê·|¼gªº¤]«ØÄ³¬Ý¬Ý p30.c, ¸Óµ{¦¡½Æ²ß¤W¶g´£¹Lªº¸ê®Æ¦p¦ó
     ¦b¹q¸£¦s©ñªº°ÝÃD, ¤]Åý§A¬Ý¨ì¥Î Loop ³B²z array, 
      ¸Ì­±ªº print(unsigned char*p) ·|§â¦r¦êp¬Ý§@¤@­Ó array of char,
      µM«á§â¨C­Ó char ¤¸¯À®³¨Ó¬Ý§@ int ¾ã¼Æ¦L¥X¥h!
  (3)¨Ì¾Ú C »y¨¥³W©w, Åܼƪº type ¥u¦³¤À¾ã¼Æ©M¹ê¼Æ¨âºØ:
     ¾ã¼Æ(integer)¨Ì¦û¥Î°O¾ÐÅé¦h¤j¤S¤À: char, short, int, long, long long
      ¹ê¼Æ(real number)¨Ì¦û¥Î°O¾ÐÅé¦h¤j¤S¤À: float, double, long double;
      ¨ä¤¤ float ¥Î 32 ­Ó0»P1(32 bits), double ¥Î 64 bits,
       long double «h¤£¤@©w, ¦³¨Ç¬Æ¦Ü¥i¯à¸ò double ¤@¼Ë!
  (4)´N³o¼Ë? ¨S¿ù, ´N¬O³o¼Ë!  char (¦r²Å; ¤å¦r²Å¸¹; ¦r¤¸) ´N¬O³Ìµuªº¾ã¼Æ,
     ¦]¬°²{¦b¤j¦h¼Æ¹q¸£±Ä¥Î¬ü°ê¼Ð·Ç½X ASCII ¦s©ñ­^¤å,
      ¤å¦r '0' ¬O48, ¦r¥À¤j¼g 'A' ¬O 65, ¤p¼g 'a' ¬O 97;
     ¤¤¤å¤º½X«h¤j¦h±Ä¥Î¸êµ¦·|ªº Big5 ¤j¤­½X!
     ª`·N¥Î³æ¤Þ¸¹§¨¦í¬O char ¤]´N¬O¾ã¼Æ, ¥ÎÂù¤Þ¸¹§¨¦í«h¬O¦r¦ê³á !!! 
  (5)¦b¤£¤ÓÀ´Åܲ§¼Æªº¤½¦¡¤§«e, ³o¼Ëºâ:
     ¥ý¥Î¤@­ÓLoopŪ¤J¸ê®Æ®É¶¶«Kºâ¥X sum ©M°O¿ýÁ`­Ó¼Æ n;
     ³o®É´N¥iºâ¥X¥­§¡ avg = sum/n; µM«á¥OÅܼÆdouble v = 0.0;
     ¦A¥Î Loop §â¨C­Ó¼Æ´î¥h avg «á¥­¤è¨Ã¥[¨ì v;
     Loop §¹«á, v=v/n; ³o´N¬OÅܲ§¼Æ¤F, µM«á ¼Ð·Ç®t std = sqrt(v);
 
LAB-03 @ Third week  Study how to use Loop to process an Array again  (p.2/4)

   01 //p30.c  -- @CopyLeft by tsaiwn@cs.nctu.edu.tw
   02 //demo usage of char array ; using it as a string
   03 //½Ð°õ¦æ«á¬Ý¥X"ªü«ó"ªº¤º½X
   04 // Most computers adopt ASCII code + Big5 code
   05 // ­»´ä¤¤¤å½X¥Î HZ (º~¦r);  ¤j³°¤¤¤å½X¥Î GB (°ê¼Ð)
   06 // IBM ¤j¹q¸£¥Î EBCDIC instead of ASCII code
   07 #include 
   08 char haha[ ]={65, 66, 67, 164, 106, 165, 106, 165,107,0};
   09 char gg[ ]="Hello§õµn½÷heeheehee";
   10 char*yy = "Hey ªü«ó How are you?\n";  // ª`·N³o¦³ \n ´«¦C 
   11 void print(unsigned char*); // «Å§i declare »¡·|¦³³o¨ç¼Æ
   12 int main( ) {
   13    printf("haha=%s\n", haha);
   14    printf("gg is ");
   15    print(gg);  // pass gg to print( )
   16    printf("gg = %s\n", gg);
   17    printf("yy is ");
   18    print(yy);  // pass yy to print( )
   19    printf("yy = %s\nHit ENTER key..", yy);
   20    getchar( ); return 0;
   21 }
   22 /****** Running script ..
   23 D:\testc>gcc p35.c
   24 
   25 D:\testc>a
   26 haha=ABC¤j¥j¥k
   27 gg is 72 101 108 108 111    167 245 181 110 189
   28       247 104 101 101 104    101 101 104 101 101
   29       0
   30 gg = Hello§õµn½÷heeheehee
   31 yy is 72 101 121 32 170    252 171 243 32 72
   32       111 119 32 97 114    101 32 121 111 117
   33       63 10 0
   34 yy = Hey ªü«ó How are you?
   35 
   36 Hit ENTER key..
   37 
   38 D:\testc>
   39 ***********************/
   40 void print(unsigned char x[999]) {
   41    // Note that though parameter x[ ] is an array
   42    // actually ¶Ç¹L¨Óªº¬O pointer («ü¼Ð) !
   43    // «ü¼Ð©M array (°}¦C) ¥Î°_¨Ó¬O¤¬³qªº!!
   44    // ¼g x[999] ªº 999 ¤]¬O¦Û´Û´Û¤H, ¦]µLªkª¾¹D array ¦h¤j
   45    // ¹ê»Ú¤W¶Ç¤J¦¹ function ªº¥u¬O array ²Ä0­Ó¤¸¯Àªº address
   46    int i=0; 
   47    for( ; ; ) {   // while(38==38) {
   48       printf("%d ", x[i]);
   49       if(x[i] == 0) break;   // string is NULL-terminated
   50       ++i;  if(i%5==0)printf("   "); 
   51       if(i%10==0)printf("\n      "); // 10 data/line
   52    } // for
   53    printf("\n");  // new line
   54 }// print(
   55 ///////////// END of the program

LAB-03 @ Third week  Study how to use Loop to process an Array again  (p.3/4)

   01 //p31.c  ---  - ¥Î¤F¥ß©e¤uµ{Âà¥]·§©À: ¥t¼gfunction°µ¿é¤J !
   02 //  Try to write a function(¨ç¼Æ) long getLong( ); to read a Long int
   03 //  µM«á¨C¦¸­nŪ¨ú¤@­Ó¾ã¼Æ´N ¡§¥s¡¨ (Call) ¥¦À°¦£°µ !
   04 #include 
   05 #include 
   06   //  (ª`·N)  atol( ), atoi( ),  ¥H¤Î atof( ) ³£«Å§i¦b 
   07 long n, nBig; // how many data?  nBig: how many bigger than average ?
   08 long getLong(void);  // ¥ý«Å§i(Án©ú)·|¼g¤@­Ó getLong( ) function ƒº
   09 void statistics(void);  // Án©ú·|¼g¤@­Ó¨S¦³°Ñ¼Æªºfunction ¥s°µ statistics
   10 long x, a[1234]; // ¦¹ÃD³W©w¦Ü¤Ö 999 ­Ó¤¸¯À; x ¬O¼È¦s¥Îªº
   11 double sum, s2, avg, variance, std; // ©M, ¥­¤è©M, ¥­§¡, Åܲ§¼Æ, ¼Ð·Ç®t
   12 int main(  ) {   // ¤j¦h¼Æ main program ³£³o¼Ë¶}ÀY
   13      n = nBig = 0; sum = s2 = 0.0;
   14      x = getLong( );  // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì x
   15      while(x != -1) {   //   (ª`·N)  ³W©w -1 ¬O¥Nªíµ²§ô
   16          if( feof(stdin) ) break;  // ­Y¬O EOF ¤]­n°± ! (ª`·N) stdin ¬OÁä½L
   17           // ÁÙ¦³°Ú, ­Y¤w¸g EOF µo¥Í, getLong( ) ¦^¶Ç¨Óªº¤]¬O¿ùªº !!!
   18          //  §â x ©ñ¤J a[ ], 
   19          ++n;   // ¬JµM¦s¤J array, ²Î­pªº¨Æµ¥¤U Call function statistics §@
   20          x = getLong( );  // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì x
   21      } // while
   22      printf("Total %d data read.\n", n);  
   23      statistics( ); // ²Î­pªº¨Æ´N¥æµ¹¥¦ 
   24      // ·Q¿ìªk¼È°±¤@¤U, ¥H§K¥i¯à°{¤@¤U´N¤£¨£
   25 }// main(
   26 long getLong( ) {     // ¥H«á­n§ï¿é¤Jªº¤èªk¥u­n§ï³o function ´N¥i :-)
   27     static char buf[99];   // a string buffer for input;  why "static" ?
   28     long ans;   // ª`·N­Y¥Î atof ­n§ï double; ¨ç¼ÆÀY³¡¤]­n§ï
   29     fgets(buf, sizeof(buf), stdin);   // stdin ´N¬OÁä½L, ­n°O±o§t¤J 
   30     ans = atol(buf);    // ±q¦r¦ê¤¤Åª¥X long ¾ã¼Æ (long); 
   31            ////// ­Y¬O¹ê¼Æ´N§â atol §ï¬° atof ³o¤]¬O§O¤H¼g¦nªº¨ç¼Æ(¨ç¦¡)
   32     return ans;
   33 }
   34 void statistics(void) {  // ¥i¥H¬Ý¨ì©Ò¦³ Global ¾ãÅéÅܼÆ(¥þ°ìÅܼÆ)
   35   // ¥Î Loop ºâ¥XÃD¥Ø³W©wªº¦UºØ²Î­p¸ê®Æ
   36   // ÁÙ¤£·|ªº¥ý½m²ß¦L¥X©Ò¦³ 1 ¨ì n ªº©_¼Æ (Hint:  k%2 !=0 «h k ´N¬O©_¼Æ)
   37 } // statistics(
   38 // ¦pªG§A¦³ K&R ½Ò¥», ¥i¬ã¨s½Ò¥»¤º°Q½× atoi »P atof ¨ç¼Æ¬O¦p¦ó¼gªº,
   39 // ¥i¥HÅý§A¼W¥[¥\¤O³á !
  
 
   LAB-03 @ Third week  Study how to use Loop to process an Array again  (p.4/4)

   01 //p32.c  ---  - ¥Î¤F¥ß©e¤uµ{Âà¥]·§©À: ¥t¼gfunction°µ¿é¤J !
   02 //  ¥Î long getLong( ); to read a Long int
   03 //  ³o­Ó getLong ¯dµÛ¥H«á´N¥iª½±µ®³¨Ó¥Î :-)
   04 //  This program read ¾ã¼Æ n µM«á¦L¥X 1.. n ¶¡¥i¦P®É³Q 3, 5, 7 ¾ã°£ªº¼Æ
   05 //  ­Y¿é¤J n ¬O -1 «h°±¤î, ­Y n ¬O 0 «h§ï¬°¹w³]­È 3388
   06 // ¦bWindows ¨t²Î¤§¤U, ´ú¸Õ¿é¤JCTRL_Z¬Ý¬Ý (Unix ¥Î CTRL_D ªí¥Ü EOF)
   07 #define N_DEFAULT 3388
   08 #include 
   09 #include 
   10   //  (ª`·N)  atol( ), atoi( ),  ¥H¤Î atof( ) ³£«Å§i¦b 
   11 long n, count; // read n, count for ¥i³Q 3, 5, 7 ¦P®É¾ã°£ªº¼Æ
   12 long getLong(void);  // ¥ý«Å§i(Án©ú)·|¼g¤@­Ó getLong( ) function ƒº
   13 int main(  ) {   // ¤j¦h¼Æ main program ³£³o¼Ë¶}ÀY
   14      int kk;
   15      printf("This µ{¦¡ read n, then ¦L¥X 1..n ¥i³Q 3, 5, 7 ¾ã°£ªº¼Æ\n");
   16      printf("n= ");
   17      n = getLong( );  // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì n
   18      while(n != -1) {   //  ª`·N³W©w -1 ¬O¥Nªíµ²§ô
   19          if(n==0) n = N_DEFAULT;
   20          if( feof(stdin) ) break; //­Y¬O EOF ¤]­n°±!(ª`·N) stdin ¬OÁä½L
   21          kk = count = 0;
   22          while(kk < n) {   // §O¼g¿ù³á, ª`·N¶i¤J«á·|¥ß¨è +1
   23              ++kk;
   24              if(kk%3 !=0 ) continue;
   25              if(kk% 5 !=0 ) continue;
   26              if(kk% 7 !=0 ) continue;
   27              printf("%5d ", kk); 
   28              ++count; 
   29              if(count%10 ==0) printf("\n"); // NewLine
   30          } // while(kk
   31          printf("\nTotal %d data.\n", count);
   32          printf("n= ");
   33          n = getLong( );  // ³z¹L getLong( ) Ū¨ú¾ã¼Æ¨ì n
   34      } // while
   35      printf("Hit ENTER key...", n, n, n);  // ¸Ì­±ªº n ¬O¥Õ¼gªº :-))
   36      getchar( ); // ·Q¿ìªk¼È°±¤@¤U, ¥H§K¥i¯à°{¤@¤U´N¤£¨£
   37 }// main(
   38 long getLong( ) {     // ¥H«á­n§ï¿é¤Jªº¤èªk¥u­n§ï³o function ´N¥i :-)
   39     static char buf[99];   // a string buffer for input;  why "static" ?
   40     long ans;   // ª`·N­Y¥Î atof ­n§ï double; ¨ç¼ÆÀY³¡¤]­n§ï
   41     fgets(buf, sizeof(buf), stdin);   // stdin ´N¬OÁä½L, ­n°O±o§t¤J 
   42     ans = atol(buf);    // ±q¦r¦ê¤¤Åª¥X long ¾ã¼Æ (long); 
   43            ////// ­Y¬O¹ê¼Æ´N§â atol §ï¬° atof ³o¤]¬O§O¤H¼g¦nªº¨ç¼Æ(¨ç¦¡)
   44     return ans;
   45 }
   46 // ¦pªG§A¦³ K&R ½Ò¥», ¥i¬ã¨s½Ò¥»¤º°Q½× atoi »P atof ¨ç¼Æ¬O¦p¦ó¼gªº,
   47 // ¥i¥HÅý§A¼W¥[¥\¤O³á !  ¥i±q®Ñ¥»«á­±¯Á¤Þ (index) ¬d¬Ý¦b²Ä´X­¶ :-)
   48 /************************
   49 D:\testc>gcc p32.c
   50 
   51 D:\testc>a
   52 This µ{¦¡ read n, then ¦L¥X 1..n ¥i³Q 3, 5, 7 ¾ã°£ªº¼Æ
   53 n= 500
   54   105   210   315   420
   55 Total 4 data.
   56 n= 0
   57   105   210   315   420   525   630   735   840   945  1050
   58  1155  1260  1365  1470  1575  1680  1785  1890  1995  2100
   59  2205  2310  2415  2520  2625  2730  2835  2940  3045  3150
   60  3255  3360
   61 Total 32 data.
   62 n= ^Z
   63 Hit ENTER key...
   64 
   65 D:\testc>
   66 *******************************************/
 
*** ¦pªG§Aªº¶i«×¤ñ¸ûºC, ¤§«eªº³£ÁÙ¨S½m¼ô, 
    ¨º´N¥ýÄ~Äò½m²ß¤§«eªº; ©Î¬O«e¤T¶gªº½m²ßÃDÁÙ¨S§ËÀ´, 
    ªü´NÁ|¤â§ì­Ó¤p§U±Ð¨Ó¸ò§AºCºC¬ã¨sƒº 
  **½Ðª`·N¤@©w­n§â¥¦­Ì§ËÀ´, ¦ý¬OºCºC¨Ó¨SÃö«Y!
    ³Ì­«­nªº´N¬O.. ¤£¥i¥H©ñ±ó!  
  **³q±`§Ô­@­Ó¤T¥|¶g¤£¤ÓÀ´ªºª¬ºA´N·|ºCºC¶}¬¤F :-)