1 / 14

Array( 陣列 )

Array( 陣列 ). Anny Email: anny@fg.tp.edu.tw http://www.fg.tp.edu.tw/~anny. 陣列 (array). 定義:是一群具有 相同名稱 以及 相同型別 的記憶體位置。 若要引用陣列的某個位置或元素,我們必須指定 陣列名稱 ,以及此元素在陣列中的 位置編號 例如: int A[5]; A[3]=A[1]+A[2];. 一維 Array 的宣告. 整數陣列的宣告

tait
Télécharger la présentation

Array( 陣列 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array(陣列) Anny Email: anny@fg.tp.edu.tw http://www.fg.tp.edu.tw/~anny

  2. 陣列(array) • 定義:是一群具有相同名稱以及相同型別的記憶體位置。 • 若要引用陣列的某個位置或元素,我們必須指定陣列名稱,以及此元素在陣列中的位置編號 • 例如:int A[5];A[3]=A[1]+A[2];

  3. 一維Array 的宣告 • 整數陣列的宣告 • int student[5];意義:宣告了5個int大小的連續空間,名稱 為student,沒有預設值,則為系統殘值。 int int int int int student student[3] student[4] student[2] student[1] student[0]

  4. 0 0 0 0 0 student student[3] student[4] student[2] student[1] student[0] 一維Array 的宣告 • 整數陣列的宣告 • int student[5]={0};意義:宣告了5個int大小的連續空間,名稱 為student, 且裏面的值皆預設為0。

  5. 1 3 4 6 7 student student[3] student[4] student[2] student[1] student[0] 一維Array 的宣告 • 整數陣列的宣告 • int student[5]={1,3,4,6,7};意義:宣告了5個int大小的連續空間,名稱 為student, 其預設值如下:

  6. 1.2 3.2 4.5 num num[0] num[1] num[2] num[3] 一維Array 的宣告 • 浮點數陣列的宣告 • float num[4]={1.2,3.2,4.5};意義:宣告了4個float大小的連續空間,名稱 為num, 其預設值如下:

  7. 一維Array 的宣告 • 字元陣列的宣告—常用來儲存字串 • char name[10]="John";意義:宣告了10個char大小的連續空間,名稱 為name, 其預設值如下: • char name[10]={'J','o','h','n'}; • char name[10]; 'J' 'o' 'h' 'n' name name[1] name[2] name[8] name[9] name[0]

  8. 定義陣列 • 請畫出示意圖: • int c[12]; • int b[100], x[27]; • float a[3]={2.1, 3.2, 3.3}

  9. 畫下結果 • int i, a[10];for (i=0; i<10; i++)a[i] = i; • char name[6]; name[0] = 'J'; name[1] = 'a'; name[2] = 'm'; name[3] = 'e'; name[4] = 's'; name[5] = '\0';printf("%s",name); 請問a[5]=? 請問name是什麼?

  10. 練習:輸入10個學生成績,並算平均 #include <stdio.h> #include <stdlib.h> Int main() { int i, stuscore[10], total=0; for ( ; ; ){ printf("請輸入%d號成績:", ); scanf("", );} for ( ; ; )total = total + stuscore[i]; printf("平均為:%f \n", ); system("PAUSE");return 0; }

  11. int int int int int A 74 48 30 17 62 A[0] A[1] A[2] A[3] A[4] int int int i max min Array 使用範例一:比較大小 #include <stdio.h> #include <stdlib.h> int main() { int A[5]={74,48,30,17,62}; int i,min,max; min=max=A[0]; printf("elements in array A are "); for(i=0;i<5;i++) { printf("%d ",A[i]); if(A[i]>max) /* 判斷最大值 */ max=A[i]; if(A[i]<min) /* 判斷最小值 */ min=A[i]; } printf("\nMaximum is %d",max); printf("\nMinimum is %d\n",min); system(“PAUSE”); return 0; } 0 1 2 3 4

  12. Array 的運用—寫下這個程式在做什麼 • #include <stdio.h>#include <stdlib.h>int main(){ int f[10]; int i; f[0]=1,f[1]=1; for (i=2; i<10; i++){f[i]=f[i-1]+f[i-2]; } return 0;}

  13. 練習:搜尋(Search) • 請設計一程式,讓亂數產生100個(1-100)的數存入陣列a中。當使用者輸入1數,搜尋它出現在此陣列中的第幾個。 • 提示:srand((unsigned)time(NULL));for (i= 1; i<=100; i++) a[i] = rand() % 100 + 1;

More Related