跳到主要內容

計算導論與 C 語言基礎:第十週(C語言的基本成分 —— C程序的數組)

計算導論與 C 語言基礎:第九週(C語言的基本成分 ——  C程序的數組)


Part I : 一維數組初始化
注意:數組中的數字必須是個常數,若為 a[ i ] 也須定義 i 為一常數



若在main 中不定義,也可以在開頭使用 #define 的方式進行定義。   


若是開頭沒有對數組進行初始化,則在不同編譯器會自動給數組一組初始值,在VC編譯器中為下。
若是在數組中,括號中沒有數字,則在數組初始化時,會自動依照常數的定義直接從0開始編序。


若是沒有給數組完整的初始化,僅部分數組給予初始值,則剩下的部分則會自動以 0 於後補上。
若只給一個初始值,則全部都給相同的初始值=0
**但是切記==>若只有一個數組,卻有多個初始值,則不合理,無法成立。


二維數組
二為數組的基本定義方式: int a[3][4] 解釋如下
setw(3)表示每個數組佔三個byte
同樣,若是數組中的初始值給的不完整,則會在其他格補上0
有關於二維數組的初始化,參考下方,則可數組中的數字依照順序一一條列。




三維數組
三維數組的概念。




Part II :數組的作用
數組最經典的使用方法是在統計數值上面的方便。
習題如下:
結果
另外的方法:(提示)使用switch
更簡化:
經舔習題:為老師分學院和系,並數人數。
經典習題二:找出素數(質數)







留言

這個網誌中的熱門文章

【Android Development for Beginners】 Lesson 2A : Create a New Project , Plan How to Build the Layout, Build Layout

This Notes is according to the udacity free course : Android Development for Beginners Lesson 2A : Create a New Project Plan How to Build the Layout Build layout Follow the instruction below to build up a new app called "Just Java" use empty activity .

【Android Development for Beginners】Lesson 2A : Update a Variable in the App

This Notes is according to the udacity free course : Android Development for Beginners The Notes below are my own notes in class. Lesson 2A :  Update a Variable in the App When you put new variable in the MainActivity.java you will get a new results when you press the button"+"

【Android Development for Beginners】Lesson 2B : String Data Type, Combining Strings Together, and Integer Variables

This Notes is according to the udacity free course : Android Development for Beginners Lesson 2B :  String Data Type,  Combining Strings Together and Integer Variables Finally We cross the river and change the layouts successfully. But We can do it better by letting the price change automatically but not change after the we click the "order" button. So this is what we want to do in this class.