0% found this document useful (0 votes)
3 views21 pages

10 Array

The document provides an overview of arrays in programming, explaining their purpose, declaration, and usage for storing multiple values of the same type. It includes examples of initializing arrays, assigning values, and performing operations such as finding maximum values and sorting. Additionally, it touches on multidimensional arrays and character arrays, demonstrating their structure and how to work with them in code.

Uploaded by

luaa960324
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

10 Array

The document provides an overview of arrays in programming, explaining their purpose, declaration, and usage for storing multiple values of the same type. It includes examples of initializing arrays, assigning values, and performing operations such as finding maximum values and sorting. Additionally, it touches on multidimensional arrays and character arrays, demonstrating their structure and how to work with them in code.

Uploaded by

luaa960324
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming of Engineering

陣列 Array

1
Why
• A variable can only store one value.

• If there is a lot of data to be processed, a lot of variables must be declared.


Ex: int student1=10, student2=30, student3=30 , …. student100=10;

• Arrays are suitable for processing a large number of variables.

• An array is a collection of elements of the same type placed in contiguous memory


locations that can be individually referenced by using an index to a unique identifier.
Ex: int student[100]={10, 30, … 10};

2
Array declaring: dataType arrayName [arraySize];

Ex:

int car [6];


int: type of element to be stored
car : name of the array

6 : size of the array

3
Elements
int car [10] ; // declaring
car [0] = 10; // assigning
car [1] = 11; // assigning
car [9] = 12; // assigning 。

car [9] = car [3];

car [10] = 1001; ????????

4
Example: assign values to elements by loop.
int iArray[5]; // 宣告陣列

for ( int i=0; i < 5; i++) // 用迴圈設定各元素值


iArray[ i ] = i * 5;

cout << "iArray 陣列的大小為 " << sizeof(iArray) << " 個位元組" << endl;

for (int i=0; i<5; i++) // 用迴圈輸出各元素值


cout << "iArray[" << i << "] =" << iArray[i] << endl;

5
Assign initial value to array
int a [5] = {1 , 2 , 3 , 4 , 5};
int b [5] = {1, 2 , 3 } ;
int c [ ] = {1, 2 , 3 , 4 , 5 };

//Here, we have not mentioned the size of the array. In such cases, the compiler
automatically computes the size.

int x[6] = { 19 , 10 , 8 }; // store only 3 elements in the array

//Here, the array x has a size of 6. However, we have initialized it with only 3 elements. The
compiler automatically assigns 0 to the remaining places.

6
Example
int A[] = {1,2,3,4,5}; // 未指定陣列大小

int B[5] = {6,7,8}; // 未設定全部元素的初始值

cout << ”size of A is " << sizeof(A) << endl;

cout << ”size of B is " << sizeof(nArray) << endl;

for (int i=0 ; i < 5 ; i++ ) { // 用迴圈輸出各元素值

cout << ”A[" << i << "] =" << A[i] << '\t' << ”B[" << i << "] =" << B[i] << endl;

}
7
Example : find max or min
int numbers [5];
cout << "Please enter 5 numbers in sequence" << endl;
for (int i=0;i<5;i++) { // assign values to elements
cout << ” Please enter the " << (i+1) << " number:";
cin >> numbers [i];
}
int MAX = numbers[0]; // assign the first value to MAX

for (int i=1;i<5 ;i++) // finding the MAX


if (numbers[i]>MAX) // if TRUE
MAX = numbers[i]; // then assign numbers[i] to MAX
cout << ”The max is " << MAX;
8
Exchange the values of the elements
int A [ 3 ] = { 5 , 4 , 3 }, temp ;
cout << A[0] << A[1] << A[2] ;

temp = A [ 0 ] ;
A[0]=A[1];
A [ 1 ] = temp;

cout<<A[0]<<A[1]<<A[2];

9
int A [ m ] = { 5 , 4 , 3….. }
Sorting ?
N i< i=i
loop A [5] m +1
i=0 {5, 4, 3, 2, 1 } 🡪 {4, 5, 3, 2, 1 } Y
N
i=1 {4, 5, 3, 2, 1 } 🡪 {4, 3, 5, 2, 1 } A[ i ] >
A[
i=2 {4, 3, 5, 2, 1 } 🡪 {4, 3, 2, 5, 1 }
i+1]
i=3 {4, 3, 2, 5, 1 } 🡪 {4, 3, 2, 1, 5 }
Y

exchange A[ i ], A [ i+1 ]

10
Sorting int A [ 6] = { 6, 5, 3, 4, 0, 1 }

653401 534016 k<


563401 354016 m -1
536401 345016
k =k +1N Y
534601 340516
534061 340156 i < m- i=i
534016 k-1 +1
N
Y
340156 301456
340156 031456
304156 013456 A[ i ] >A [ N
301456 i+1]
Y
013456
013456 交換 A[ i ], A [ i+1 ]

11
Array and memory
• An array is a collection of elements of the same type placed in
contiguous memory locations that can be individually referenced by
using an index to a unique identifier.
Ex: int stu [ 5 ] ={10, 20, 30, 40, 50};

4 x 5 = 20 bytes

int int int int int

stu [ 0 ] stu [ 1 ] stu [ 2 ] stu [ 3 ] stu [ 4 ]

12
傳統陣列
• 陣列長度:陣列儲存的元素個數
int a[3];
char b[4];
float c[5];

• 陣列的長度不可為變數,須在執行之前就已經確定
const int SIZE = 10;
int foo[SIZE];
int i;
cin >> i;
❖ 新標準容許陣列長度為變數
int bar[i];

13
Value assignment
• The value of the elements must be assigned one by one.
int foo[100];
for( int i = 0 ; i < 100 ; ++i ) foo[i] = i+1 ;

1 2 3 ... ... 100


foo[0] foo[1] foo[2]

• How to assign the element values of array A to array B?


int i , foo[10] , bar[10];
for( i=0 ; i<10 ; ++i ) foo[i] = i+1 ;
// bar = foo ; // incorrect

for( i=0 ; i<10 ; ++i ) bar[i] = foo[i] ;

14
Size of the array
• The size of the array is often set with a constant. It is good for the loop
process.
const int SIZE = 10;
int i , square[SIZE];
for( i = 0 ; i < SIZE ; ++i ) square[i] = i*i;
for( i = 0 ; i < SIZE ; ++i )
cout << i*i << ”=” << square[i] << endl;

• sizeof ()
int i , foo[] = { 1 , 3 , 5 , 7 };
int size = sizeof(foo) / sizeof(int);
for( i = 0 ; i < size ; ++i ) cout << foo[i] << endl;
❖sizeof(foo) : foo 陣列總共佔用的位元組數目
sizeof(int) : 一個整數所佔用的位元組數目
15
Multidimensional array
• int a[2][3] ; // a 為 2 列 3 行的整數矩陣

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]

6 個整數,共 24 bytes

• float b[2][2][3] ; // 共 12 個元素, 48 個位元組


b[0][0][0] b[0][0][1] b[0][0][2] b[0][1][0] b[0][1][1] b[0][1][2]

b[1][0][0] b[1][0][1] b[1][0][2] b[1][1][0] b[1][1][1] b[1][1][2]

• 多維陣列的總個數:
int foo[2][4][3];
cout << sizeof(foo) / sizeof(int) << endl; // 印出 24
16
Initial value assignment
• 一個大括號方式的設定:
char a[2][3] = {’a’,’b’,’c’,’d’,’e’,’f’};
int b[2][3][2] = {1,2,3,4,5,6,6,5,4,3,2,1};

• 多個大括號方式的設定: 1 0 0
A= 2 3 0
int a[2][3] = { {1},{2,3} };
int b[2][3] = { {1,0,0},{2,3,0} };
1 0 0
int c[3][3] = { {1},{1,1},{1,1,1} }; B= 1 1 0
1 1 1
以上用矩陣表示為:

17
example
• 有四個學生(student1,2,3,4),每人有三科成績 (g1, g2, g3)。
試寫程式,可輸入四個學生的各項成績,輸出學生的個別成績,
平均成績。若成績小於60,則在成績前面加註*記號。 setw

成績單.cpp
18
19
Character array

• char name; // name is char variable

• char name[4]; // name is char array variable

20
Example
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char name1[]= "John Smith"; // 以字串為初始值
6. char name2[]= {'M','a','r','y',' ','W','h','i','t','e’}; // 設定個別字元為初始值
7. cout << "name1[]大小為:" << sizeof(name1) << endl;
8. cout << "name2[]大小為:" << sizeof(name2) << endl;
9. cout << "name1[]:" << name1 << endl;
10. cout << "name2[]:" << name2 << endl;
11. }

21

You might also like