baiyanchen8
    • 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

      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.
      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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

    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.
    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # chap2 陣列&結構 {%hackmd @baiyanchen8/code %} ## 2.1 array0 ### 2.1.1 抽象化資料結構 Array由<index,value>組成 ### 2.1.2 C language 下的陣列 - link list 的陣列 - `int *list;` - 透過指標直接指向記憶體空間 - 配合malloc食用 - 一般固定大小陣列 - `int list[5];` - `int list[]={1,2,3,4,5,6,7,8,9,10}` ## 2.2 動態陣列 ### 2.2.1 一維陣列 ```c=1ㄢ int main() { // 使用malloc int *arr = (int *)malloc(5 * sizeof(int)); free(new_arr); return 0; } ``` ### 2.2.2 二維陣列 <p class="text-center"> <img src="https://th.bing.com/th/id/R.550976ea4de76c6e83a297d5699ae843?rik=4D851ZSDlc5q%2bQ&riu=http%3a%2f%2f3.bp.blogspot.com%2f-_-Izz3tAT9w%2fVQ1jX7dwU2I%2fAAAAAAAAAG8%2fba9KC3ERA30%2fs1600%2fdynamics_2d_array.png&ehk=gKTgu05j9EcWHhHGTGYZaR%2brIz3DSU%2fpivQMoE7CxOk%3d&risl=&pid=ImgRaw&r=0" width="" height="288"> </p> #### malloc (分配記憶體) `addr=(type *) malloc(length*siezof(type))` #### realloc (重新分配記憶體,用於增加記憶體長度) `new_addr=(type *) realloc(old_addr,new_length*siezof(type))` ## 2.3 結構(struct)&連結(link list) ### 2.3.1 結構(struct) - `typedef struct` ```c=1 typedef struct{ char name[10]; int age; float salary; }example_use; int main(int k,char *arr[]){ example_use asd; asd.age =0; printf("%d\n",asd.age); return 0; } ``` - `struct` ```c=1 struct example_2{ char name[10]; int age; float salary; }; int main(int k,char *arr[]){ struct example_2 as2; as2.salary=1.234; printf("%f",as2.salary); return 0; } ``` ### 2.3.2 union ```c=1 #include <stdio.h> union SampleUnion { int intValue; float floatValue; char stringValue[20]; }; int main() { union SampleUnion myUnion; myUnion.intValue = 42; printf("Integer value: %d\n", myUnion.intValue); myUnion.floatValue = 3.14; printf("Float value: %f\n", myUnion.floatValue); strcpy(myUnion.stringValue, "Hello, Union!"); printf("String value: %s\n", myUnion.stringValue); return 0; } ``` ### 2.3.3 結構內部實作 簡單而言,不用我們擔心 ### 2.3.4 自我參考表(常見於link list) ```c=1 struct link{ int value; struct link* next; }; ``` ## 2.4 多項式 ### 2.4.1 抽象化資料型態 懶得做xN,簡單而言就是可以用在很多地方 ### 2.4.2 多項式表示方式 ```c=1 struct poly{ int degree;//最高級數 float coef[100];//每級的係數 }; ``` ```c=1 struct poly add(struct poly A,struct poly B){ struct poly D; if (A.degree<B.degree){ D.degree=B.degree; }else{ D.degree=A.degree; } int i; for (i=0;i<D.degree;i++){ D.coef[i]=A.coef[i]+B.coef[i]; } } ``` ## 2.5 稀疏矩陣 ### 2.5.1 抽象化資料型態 稀疏矩陣(英語:sparse matrix),在數值分析中,<font color="ffff">是其元素大部分為零的矩陣</font>。反之,如果大部分元素都非零,則這個矩陣是稠密(dense)的。在科學與工程領域中求解線性模型時經常出現大型的稀疏矩陣。 ### 2.5.2 稀疏矩陣表示方式 ```c=1 struct term{ int col; int row; int value; }; struct term matrix[item+1]; ``` 通常,在這個表示法的第一行,也就是matrix[0]會用於放置整個矩陣的基本訊息。 - matrix[0].col - matrix的行數 - matrix[0].row - matrix的列數 - matrix[0].value - matrix中的非0值的數量 - 排序 - 升序 - row>col |i |0 |1 |2 |3 |4 |5 |6 |7 |8| |---|---|---|---|---|---|---|---|---|--| |r |6 |0 |0 |0 |1 |1 |2 |4 |5| |c |6 |0 |3 |5 |1 |2 |3 |0 |2| |value |8 |15 |22 |-15 |11 |3 |-6 |91 |28| ### 2.5.3 轉置矩陣 #### transpose ```c=1 struct term *transpose(const struct term *A) { struct term *B = (struct term *)malloc( (A[0].value + 1) * sizeof(struct term)); B[0].value = A[0].value; B[0].row = A[0].col; B[0].col = A[0].row; if (A[0].value > 0) { int i, j, k; k = 1; for (i = 0; i < A[0].col; i++) { for (j = 1; j < A[0].value + 1; j++) { if (A[j].col == i) { B[k].value = A[j].value; B[k].col = A[j].row; B[k].row = A[j].col; k++; } } } } return B; } ``` 分析: 正常而言 bigO:$\mathcal{O}(col\times value)$ 假設 value =$col\times row$ ⇒ $\mathcal{O}(col^2 \times row)$ ⇒太爛了 --- #### fast transport(use counting sort) ```c=1 #define MAX_TERMS 101 #define MAX_COL 10 typedef struct { int row; int col; int value; } Term; void fastTranspose(Term a[], Term b[]) { int rowTerms[MAX_COL], startingPos[MAX_COL]; int numCols = a[0].col; int numTerms = a[0].value; b[0].row = numCols; b[0].col = a[0].row; b[0].value = numTerms; if (numTerms > 0) { int i; for ( i = 0; i < numCols; i++) { rowTerms[i] = 0; } for ( i = 1; i <= numTerms; i++) { rowTerms[a[i].col]++; } startingPos[0] = 1; for ( i = 1; i < numCols; i++) { startingPos[i] = startingPos[i - 1] + rowTerms[i - 1]; } for ( i = 1; i <= numTerms; i++) { int j = startingPos[a[i].col]++; b[j].row = a[i].col; b[j].col = a[i].row; b[j].value = a[i].value; } } } ``` 基本上就跟counting sort 的內容非常相像 ### 2.5.4 矩陣乘法 ```c= #define MAX_TERMS 100 typedef struct { int row; int col; int value; } Term; void sparseMatrixMultiplication(Term A[], Term B[], Term result[]) { // Initialize result matrix for (int i = 0; i < MAX_TERMS; ++i) { result[i].row = 0; result[i].col = 0; result[i].value = 0; } // Multiply matrices A and B for (int i = 0; i < MAX_TERMS && A[i].value != 0; ++i) { for (int j = 0; j < MAX_TERMS && B[j].value != 0; ++j) { if (A[i].col == B[j].row) { // Multiply and accumulate result[i].value += A[i].value * B[j].value; result[i].row = A[i].row; result[i].col = B[j].col; } } } } ```

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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