NicVox
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- slideOptions: transition: slide title: W1- One-dimensional array, character array --- # #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`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully