發新話題
打印

[求助]陣列的原理

[求助]陣列的原理

各位懂得  陣列  的原理嗎?我一直搞不太懂他的道理!3 s; J! y/ t) [
是否能用個敘述呢?

TOP

[求助]陣列的原理

陣列基本上可分為一維陣列(1D-Array)和多維陣列  ]- y, z! }5 O( U- D1 Y
陣列基本上只是用來表示很多個不同的變數(variable)有著相同的數據類型(Data Type)
2 C) u; Z2 m, x$ P0 U) x我用C語言為例,我要宣告(Declare)10個整數(integer)的變數; P* T+ ^8 F" x! X2 N/ j9 V
如果不用陣列的話,就會用如下的指令:
* n* b9 @, J* A3 eint a,b,c,d,e,f,g,h,i,j;
2 M/ j7 ^' ]% g+ R" z如果我用陣列的話,只要用如下指令:
/ b6 @2 ]5 @2 A! yint array[10];6 ~+ x' O+ l, Z0 K* B9 s0 i' G
宣告一個array的長度為10,即是你宣告了10個相同data type 的varible
0 `7 [7 o2 W% k而它們的表示方法為array[0], array[1], ... , array[9]& z' G& h4 ~3 Q' o
從0開始到9,總共10個
. K; O6 i- V5 @% S# K4 j+ @不知可否解答你的問題呢?

TOP

[求助]陣列的原理

意思是說我可以把' l" @# n$ S  ~; i2 z0 Y
a=15 j( }/ J. I. _, A) P3 b
b=2
7 I# k  v# K8 f) f3 u4 lc=3
/ r2 o0 V- r8 Q  b...
) a, n: u, s; \* n寫成陣列的方式,讓他一次把Data Type整合在一起給記憶體是嗎?

TOP

[求助]陣列的原理

如果整合在記憶體?* ?3 v0 f  w% E, _9 c
不明白?  I" |2 L, o# o$ `+ }2 D
我的意思只是說用陣列在宣告很多變數會比逐個宣告來得方便和清楚
; ^4 p4 F" e4 {! K: M但前題是同一個data type的變數才可以一起用陣列宣告

TOP

[求助]陣列的原理

Array,你當佢係條隊有唔同人在排隊中,他們是同一種data type!
: W4 w$ m7 b6 N: J/ P但當你學

TOP

[求助]陣列的原理

我說的記憶體是程式會跟memory要,所以一次寫在同一個位置,
/ m" R8 R6 M' v# M6 T$ Q) E3 Q而不會讓函數自己亂找記憶體。

TOP

[求助]陣列的原理

提示: 作者被禁止或刪除 內容自動屏蔽

TOP

[求助]陣列的原理

引用:
下面引用由mnkjiul2004/09/10 11:35pm 發表的內容:6 O8 F1 H# l/ }' K0 S
我說的記憶體是程式會跟memory要' G! e3 D8 f2 o& l( k
>>> 當然,就算係你要指定的memory address都係program去提取!
$ U" Q# w* n4 X2 s7 A' L
1 W) |, F2 J4 ~# X' {2 R所以一次寫在同一個位置,而不會讓函數自己亂找記憶體。& `) l& @* k$ i0 ~
>>> 未必!你如果無allocate memory (即係未assign新的memory俾佢,佢真係亂找符合你個array size的記憶體!(你可以自己寫個array再check佢個address!)
9 X( N: O  N/ x7 G1 b6 \. Y- w& a* s4 J8 W- t
# m& Q( M. H* u& N

TOP

[求助]陣列的原理

Arrays are data structures consisting of related data items of the same type.
" L* L: ~. K) P* t0 _7 |9 U) GArrays are "static" entities in that they remain the same size throughout program execution (they may be of automatic storage class).

TOP

[求助]陣列的原理

Arrays ---------- *An array is a group of memory locations related by the fact that they all have the same name and the same type. *To refer to a particular location or element in the array, we specify: 1. the name of the array 2. the position number of the particular element *E.g. +----+----+----+----+----+----+----+----+----+----+----+----+ | -45| 6 | 0 | 72 |1543|-89| 0 | 62 | -3 | 1 |6453| 78 | +----+----+----+----+----+----+----+----+----+----+----+----+ c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] *The first element in every array is the zeroth element; i.e., "c[0]". * In general, the ith element of array "c" is referred to as "c[i-1]". * The position number (must be an integer or an integer expression) contained within square brackets is more formally called a subscript, or index. *E.g. a=5, b=6 c[a+b] += 2; adds 2 to array element c[11]. Declaring Arrays ------------------------- *The programmer specifies the types of each element and the number of elements required by each array. *The computer reserve the appropriate amount of memory. *E.g. int c[12]; is used to tell the computer to reserve 12 elements for integer array "c". *E.g. int b[100], x[27]; char str[50]; float reading[200]; *E.g. array1.c #include int main() { int n[10], i; for (i=0; i<=9; i++) n = 0; printf("%s%13s\n", "Element", "Value"); for (i=0; i<10; i++) printf("%7d%13d\n", i, n); return 0; } *The elements of an array can also be initialized in the array declaration by following the declaration with an equals sign and a comma-separated list (enclosed in braces) of initializers. *E.g. array2.c #include int main() { int i, n[10] = {32, 27, 64, 18, 95, 14, 90, 70, 60, 37}; printf("%s%13s\n", "Element", "Value"); for (i=0; i<10; i++) printf("%7d%13d\n", i, n); return 0; } *If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to zero. *It is important to remember that arrays are not automatically initialized to zero, just like other variables. * If the array size is omitted from a declaration with an initializer list, the number of elements in the array will be the number of elements in the initializer list. *E.g. five-element array int n[] = {1, 2, 3, 4, 5}; *E.g. array3.c #include #define SIZE 10 int main() { int s[SIZE], j; for (j=0; j <= SIZE - 1; j++) s[j] = 2 + 2 * j; printf("%s%13s\n", "Element", "Value"); for (j=0; j #define SIZE 12 int main() { int a[SIZE] = {1, 3, 5, 4, 7, 2, 99, 16, 45, 67, 89, 45}, i, total = 0; for (i=0; i; printf("Total of array element values is %d\n", total); return 0; } *A string such as "hello" is really an array of individual characters in C. *E.g. char string1[] = "first"; *It is important to note that the string "first" contains five characters plus a special string termination character called the null character (';\0';). *Thus, array "string1" actually contains six elements. *All strings in C end with the null character. *A character array representing a string should always be declared large enough to hold the number of characters in the string and the terminating null character. *E.g. string1[0] is the character ';f'; and string1[3] is the character ';s';. *We also can input a string directly into a character array from the keyboard using "scanf" and the conversion specification "%s". *E.g. char string2[20]; scanf("%s", string2); *The name of the array is passed to "scanf" without the preceding "&" used with other variables. *"scanf" can write beyond the end of the array. *A character array representing a string can be output with "printf" and the "%s" conversion specifier. *E.g. printf("%s\n",string2); *The characters of the string are printed until a terminating null character is encountered. *E.g. string.c #include int main() { char string1[20], string2[] = "string literal"; int i; printf("Enter a string: "); scanf("%s", string1); printf("string1 is: %s\nstring2 is: %s\n" "string1 with spaces between characters is:\n", string1, string2); for (i=0; string1 != ';\0';; i++) printf("%c ", string1); printf("\n"); return 0; } *In functions that contain automatic arrays where the function is in and out of scope frequently, make the array "static" so it is not created each time the function is called. Passing Arrays to Functions --------------------------------------- *To pass an array argument to a function, specify the name of the array without any brackets. *E.g. int hourlyTemp[24]; .... modifyArray(hourlyTemp,24); .... *When passing an array to function, the array size is often passed so the function can process the specific number of elements in the array. *The name of the array is actually the address of the first element of the array. * Because the starting address of the array is passed, the called function knows precisely where the array is stored. *Therefore, when the called function modifies array elements in its function body, it is modifying the actual elements of the array in their original memory locations. *E.g. #include int main() { char array[5]; printf("array = %p\n&array[0] = %p\n", array, &array[0]); return 0; } *Although entire arrays are passed simulated call by address, individual array elements can be passed call by values exactly as simple variables are. *E.g. #include int main() { char str[]="hello"; foobar(str[3]); .... } *For a function to receive an array through a function call, the function';s parameter list must specify that an array will be received. *E.g. void modifyArray(int b[], int size) { int j; for (j=0; j void tryToModifyArray(const int []); int main() { int a[] = {10, 20, 30}; tryToModifyArray(a); printf("%d %d %d\n", a[0], a[1], a[2]); return 0; } void tryToModifiyArray(const int b[]) { b[0] /= 2; /* error */ b[1] /= 2; /* error */ b[2] /= 2; /* error */ } Multiple-Subscripted Arrays ------------------------------------------ *A common use of multiple-subscripted arrays is to represent tables of values (in rows and columns). *Tables or arrays that require two subscripts (indexes) to identify a particular element are called double-subscripted (two-dimensional) arrays. *Every element in array "a" is identified by a name of form "a[j]": "a" is the name of the array, and "i" and "j" are the indexes that uniquely identify each element in "a". *A multiple-dimensional array can be initialized: int b[2][3] = {{1,2,3}, {4,5,6}}; *E.g. multiarray.c #include void printArray(int [][3]); int main() { int array1[2][3] = {{1,2,3},{4,5,6}}, array2[2][3] = {1,2,3,4,5}, array3[2][3] = {{1,2},{4}}; printf("Values in array1 by row are:\n"); printArray(array1); printf("Values in array2 by row are:\n"); printArray(array2); printf("Values in array3 by row are:\n"); printArray(array3); return 0; } void printArray(int a[][3]) { int i,j; for (i=0;i<2;i++) { for (j=0;j<3;j++) printf("%d ", a[j]); printf("\n"); } }

TOP

[求助]陣列的原理

好多“雞腸”
  O. q1 q: Y$ S9 W( h0 Z, R8 N  V看不懂

TOP

引用:
原帖由 a8d2ljf 於 19-4-2008 20:28 發表 淋病梅毒 ?虱尿道炎非淋菌性尿道炎
請用文字回覆填寫!...謝謝
否則當惡意灌水!

TOP

發新話題
eXTReMe Tracker