# #C Programming (II) - 1 ## One-dimensional array, character array English: Lei KuoLiang nicolaslouis@mail.fcu.edu.tw Chinese(TW): Wang M.H --- ### This week's course catalog 1. What is an array? 2. One-dimensional array     * Announcement of one-dimensional array     * Use of one-dimensional arrays 3. Character array     * Declaration of character array     * Input and output of character array     * string.h --- ## What is an array? ---- If you compare memory to a large cabinet with a pile of drawers ![](https://i.imgur.com/zRPpUC9.png) ---- "Declaring a variable" can be likened to "getting a drawer with the cabinet." ![](https://i.imgur.com/CSYW8kS.png) ---- Use the & operator to get the address (number) of the variable (drawer) ※We often use this & operator in scanf ![](https://i.imgur.com/gaJo54N.png) ---- Then, "declaring an array" can be likened to "a bunch of drawers (continuous space) that are serially numbered with the cabinet (memory)" ![](https://i.imgur.com/3agiBJd.png) ---- "The name of the array" is the "memory address of the first element" of the array. ![](https://i.imgur.com/Hyxcs5J.png) ---- Array = continuous memory space --- ## One-dimensional array --- ### One-dimensional array declaration ---- Declare the array format: **`variable type array name [array size]; `** For example: Declare an int array of size 5 **`int a[5];`** ---- You can also give the array size directly to the number inside, and the computer will automatically determine the array size: **`int a[] = {12, 56, 7, 18, -3};`** ```c a[0] = 12 a[1] = 56 a[2] = 7 a[3] = 18 a[4] = -3 ``` ---- If the array size is given and the value is given, if the number of values inside is smaller than the array size, the insufficient position will be filled with 0. **`int a[10] = {12, 56, 7, 18, -3};`** ```c a[0] = 12 a[1] = 56 a[2] = 7 a[3] = 18 a[4] = -3 a[5] = 0 a[6] = 0 a[7] = 0 a[8] = 0 a[9] = 0 ``` ---- We can use the concept of the previous page to zero the array: **`int a[10] = {0};`** ```c a[0] = 0 a[1] = 0 a[2] = 0 a[3] = 0 a[4] = 0 a[5] = 0 a[6] = 0 a[7] = 0 a[8] = 0 a[9] = 0 ``` ---- Note: The index (subscript) of the array starts from 0! `int a[10];` The index of array a will be 0~9. --- ### Usage of one-dimensional arrays ---- Declare an int array a of size 10, zero the array a, then assign 1 to a[0], assign 3 to a[5], and print a[0]~a[9]. ```c int i, a[10] = {0}; a[0] = 1; a[5] = 3; for (i = 0; i < 10; i++) printf("%d ", a[i]); printf("\n"); ``` Results ``` 1 0 0 0 0 3 0 0 0 0 ``` ---- Note! If the array is declared without zeroing, the array may be stowed. ![](https://i.imgur.com/3V2t1lu.png) ---- Use the for loop to enter 10 integers into the array and then output it backwards: ```c int i, a[10]; for (i = 0; i < 10; i++) scanf("%d", &a[i]); for (i = 9; i >= 0; i--) printf("%d ", a[i]); printf("\n"); ``` Input ``` 1 2 3 5 8 13 21 34 55 89 ``` Output ``` 89 55 34 21 13 8 5 3 2 1 ``` ---- Verify the continuity of elements in the array: Use & Address ![](https://i.imgur.com/qbu8PLh.png) * Think: Why are the addresses different by 4? * Try it: don't use %d, use %x or %p ---- Array memory size (sizeof()) ```c int a[10]; printf("%d\n", sizeof(a[0])); printf("%d\n", sizeof(a)); ``` Results ``` 4 40 ``` ---- Note: After the array is declared, you can no longer use the way to declare the value! ```c int a[5]; a = {12, 56, 7. 18, -3};//ERROR!!!!! a[] = {12, 56, 7. 18, -3};//ERROR!!!!! a[5] = {12, 56, 7. 18, -3};//ERROR!!!!! ``` Can only be assigned one by one ```c int a[5]; a[0] = 12; a[1] = 56; a[2] = 7; a[3] = 18; a[4] = -3; ``` --- ### Exercise one * The known fee series fib[0] = 0, fib[1] = 1, fib[k] = fib[k - 2] + fib[k - 1], k > 1. * There will be multiple lines in the input, each line has an integer n, 0 ≦ n ≦ 93. Print out the value of fib[n]. Input ``` 11 21 31 ``` Output ``` 89 10946 1346269 ``` --- ## Character array ---- In most other programming languages, there is a string type of data, but unfortunately, the C language does not have the string data type, instead it is a character array. --- ### Declaration of character array ---- Declare the array format: **`char array name [array size]; `** For example: Declare a character array of size 5 **`char a[5];`** ---- It is also possible to give the string directly without giving the array size, and the computer will automatically determine the array size: **`char a[] = "hello";`** ```c a[0] = 'h' a[1] = 'e' a[2] = 'l' a[3] = 'l' a[4] = 'o' a[5] = '\0' ``` * Note: '\0' is the end character of the string. When assigned as a string, it will be automatically added. * '\0' ASCII code is 0 ---- If a given array size is given to a given string, if the string length is less than the array size, the insufficient position will complement '\0'. **`char a[10] = "hello";`** ```c a[0] = 'h' a[1] = 'e' a[2] = 'l' a[3] = 'l' a[4] = 'o' a[5] = '\0' a[6] = '\0' a[7] = '\0' a[8] = '\0' a[9] = '\0' ``` ---- ```c char a[] = "hello"; char b[] = {'h', 'e', 'l', 'l', 'o'}; ``` The difference between the two is that the `'e'` of a will add one more `'\0'`. And the array size of a is 6, and the array size of b is 5 ```c a[0] = 'h' b[0] = 'h' a[1] = 'e' b[1] = 'e' a[2] = 'l' b[2] = 'l' a[3] = 'l' b[3] = 'l' a[4] = 'o' b[4] = 'o' a[5] = '\0' ``` ---- Note: You can't assign a string again when you declare the character array! ```c char a[5]; a = "hello";//ERROR!!!!! a[] = "hello";//ERROR!!!!! a[5] = "hello";//ERROR!!!!! ``` --- ### Input and output of character array ---- Input and output of the character array, printf, scanf use %s Scanf reads blank key, tab, newline will end reading ```c char str[10]; scanf("%s", str); //You don't need to use the & address operator because the array name itself is the address printf("%s\n", str); ``` Input ``` 123 abcc ``` Output ``` 123 ``` ---- Input and output of character array Gets read the newline will end the reading, puts will automatically wrap at the end when printed ```c char str[10]; gets(str); puts(str); printf("%s\n", str); ``` Input ``` 123 abcc ``` Output ``` 123 abcc 123 abcc ``` ---- #### Input EOF ```c scanf("%s", a) != EOF; gets(a) != NULL; ``` --- ## string.h a library of functions for manipulating character arrays Before use #`include <string.h>` ---- ### strlen() String length ```c= #include <stdio.h> #include <string.h> int main(){ char str[10] = "hello"; int len; len = strlen(str); printf("%d\n", len); len = strlen("happy!!"); printf("%d\n", len); } ``` Results ``` 5 7 ``` ---- ### strcmp() String comparison The 2-character array starts with the 0th character and compares the size of the ASCII code. * If the first parameter is large, pass back 1 * If they are equal, return 0 * If the second parameter is large, return -1 ---- ```c= #include <stdio.h> #include <string.h> int main(){ char strA[10] = "hello"; char strB[10] = "1234"; char strC[10] = "321"; int cmp; cmp = strcmp(strA, "hello"); //equal printf("%d\n", cmp); cmp = strcmp(strB, strC); //strC is bigger printf("%d\n", cmp); } ``` Results ``` 0 -1 ``` ---- ### strcpy() String copy Copy the string from the second parameter to the first parameter ```c= #include <stdio.h> #include <string.h> int main(){ char strA[20] = "better than brian"; char strB[10] = "hello"; strcpy(strA, strB); //Pay attention to the array size when strcpy! //strlen(strB) < strA Array size printf("%s\n", strA); strcpy(strB, "happy!!"); printf("%s\n", strB); } ``` Results ``` hello happy!! ``` ---- ### strcat() String connection After the string in the second parameter is connected to the string of the first parameter ```c= #include <stdio.h> #include <string.h> int main(){ char strA[50] = "hello"; char strB[50] = "better than brian"; strcat(strA, " is "); //Pay attention to the array size printf("%s\n", strA); strcat(strA, strB); //Pay attention to the array size printf("%s\n", strA); } ``` Results ``` hello is hello is better than brian ``` ---- Of course, there are other functions, you can search the Internet yourself~~ --- ### Exercise 2 * One day, a university professor was very angry because the student's grades were too bad. He decided to shift the button to the right by pressing the button that was deliberately pressed when he typed home with the keyboard, that is, he had to type "a" and type "d". Originally, I wanted to type "p" and type "]". I originally had to type "0" and "=", and the blank key was not mistyped. ![](https://www.asus.com/ROG-Republic-Of-Gamers/ROG-Horus-GK2000-RGB-Mechanical-Gaming-Keyboard/websites/global/products/biIhG58oXyW5DA8a/images/battlefield/color-keyboard.png) ---- * Input will have multiple lines of text, the characters are the half-shaped symbols and half-shaped lowercase English letters and numbers in the keyboard above, and the text does not contain "\`", "1", "q", "w", "a", "s", "z", "x", "\\". * Output restored text Input ``` p m[ojku d ]t,/ pu b[fu .t 5= g[''dyf/ k[r .obk g[tf pu b[fu ph p moi 3= ]t,f ``` Output ``` i bought a pen, it cost me 30 dollars, how much does it cost if i buy 10 pens ``` --- ### Homework ---- * For the fifth week of the job continuation (I), refer to [this page] (https://hackmd.io/@K-54OPo-RMyCWp-fBFE48Q/SkTwAtktB) * Store character data in ++ arrays++ * Added role to give ++ name function ++ * The attack damage formula is modified to: (int) (attack attack power * phase gram override * residual blood rate - attacker's defense) * Use a while big loop and let the user ++ select the option ++ at the beginning of each loop    * case 1: Add or modify new roles    * case 2: Display all role data    * case 3: fighting    * case 0: End the game ---- ![](https://i.imgur.com/pa4IJxm.png =600x620) ---- #### Attack damage formula detailed description * Attack damage = (int) (attack attack power * phase gram ratio * residual blood rate - attacker's defense)    * Attribute phase gram ratio:      Attribute relationship W>F>A>G>W      If it is a relationship between the two: the advantage is 1.2 times, the disadvantage is 0.8 times      If the two sides have no gram, the attack power is 1.0 times each.    * Residual blood multiplication rate: the blood volume of the attacker is ++ less than 5% of the original 5%, the residual blood rate is 1.2 times. ---- #### Declaration example ```c Char char1_name[21]; Char char2_name[21]; Char char3_name[21]; Char char4_name[21]; Char type[4]; //attribute Int hp[4]; //blood volume Int atk[4]; //attack Int def[4]; //defense ``` ---- ![](https://i.imgur.com/8OHwUgl.png =1000x600) ---- ![](https://i.imgur.com/cvHOiJc.png =800x600) --- ###### tags: `1082 Ai-Mod-Eng-LKL`
{"metaMigratedAt":"2023-06-15T01:51:08.267Z","metaMigratedFrom":"YAML","title":"W1- One-dimensional array, character array","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"befaa4d9-75b6-4c05-baa7-7949e0ffa1e2\",\"add\":12242,\"del\":1312}]"}
    211 views