1. 程式人生 > 其它 >C++ 學習筆記 —— 陣列

C++ 學習筆記 —— 陣列

概述:

就是一個集合,裡面存放了相同型別的資料元素

特點1:陣列中的每個元素都是相同的資料型別

特點2:陣列是由連續的記憶體位置組成的

1 、一維陣列定義方式

資料型別 陣列名 [陣列長度];

陣列型別 陣列名[資料長度] = {值1,值2,,,,};

陣列型別 陣列名[ ]={} ;

 

#include <iostream>

using namespace std;

int main()

{

cout <<"一維陣列練習:" << endl;

/*

1. 資料型別 陣列名 [陣列長度];

2. 資料型別 陣列名 [陣列長度]={值1,值2.。。。};

3. 陣列型別 陣列名 [ ] ={值1,值2.。。。。};

*/

//1. 資料型別 陣列名 [陣列長度];

int iarr1[5];

iarr1[0] = 0;

iarr1[1] = 1;

iarr1[2] = 2;

iarr1[3] = 3;

iarr1[4] = 4;

for (int i = 0; i < 5; i++)

{

cout << iarr1[i] << endl;

}

cout <<"-----------"<< endl;

//2. 資料型別 陣列名 [陣列長度]={值1,值2.。。。};

int iarr2[5] = { 5,6,7,8,9 };

for (int i = 0; i < 5; i++)

{

cout << iarr2[i] << endl;

}

cout << "----------" << endl;

//3. 陣列型別 陣列名 [ ] ={值1,值2.。。。。};

int iarr3[] = { 10,11,12,13,14 };

for (int i = 0; i < 5; i++)

{

cout << iarr3[i] << endl;

}

cout << "-----------"<< endl;

system("pause");

return 0;

}

 

2 、一維陣列名

用途:

可以統計整個陣列在記憶體中的長度 可以獲取陣列在記憶體中的首地址

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "一維陣列名用途練習:" << endl;

//1.可以統計陣列在記憶體中的長度

int iarr[5] = { 0,1,2,3,4 };

cout << "陣列iarr在記憶體中的長度為:" << sizeof(iarr) <<"個位元組" << endl;

cout << "陣列iarr有" << sizeof(iarr) / sizeof(iarr[0]) << "個元素"<< endl;

//2.可以獲取陣列在記憶體中的首地址

cout << "陣列iarr的首地址是:"<< &iarr[0] << endl; cout<< "陣列iarr的首地址是:"<< iarr << endl;

system("pause");

return 0;

}

 

3、 練習案例 1 :五隻小豬稱體重

案例描述: 在一個數組中記錄了五隻小豬的體重,

如:int arr[5] = {300,250,200,400,250}; 找出並列印最重的小豬體重。

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "用資料實現五隻小豬稱體重練習:"<< endl;

int arr[5] = { 100,25,8965,455,8755 };

int max = 0; for (int i = 0; i < 5; i++)

{

if (max < arr[i]) { max = arr[i];

}

}

cout << "最重的小豬體重是:"<< max << endl;

system("pause");

return 0;

}

 

4、 練習案例2——陣列元素逆置

案例描述:請宣告一個5個元素的陣列,並且將元素逆置,

(原陣列:12345,逆置後:54321)

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "陣列元素逆置練習:"<< endl;

//1.宣告一個五個元素的陣列

int array[6] = { 1,2,3,4,5,6};

int freq1 = sizeof(array) / sizeof(array[0]);

for (int i = 0; i < freq1; i++)

{

cout << array[i] << endl;

}

cout << "----------------------"<< endl;

//2.申明陣列的開始下標和結束下標

int start = 0;

int end = sizeof(array) / sizeof(array[0] )- 1;

//3.定義一個變數,臨時存放元素的值

int temp = 0;

//4.利用for迴圈進行逆置

// int freq = sizeof(array) / sizeof(array[0]) / 2;

// for (int i = 0; i < freq; i++)

// {

// temp = array[start];

// array[start] = array[end];

// array[end] = temp;

// start++;

// end--;

// }

while (start < end)

{

temp = array[start];

array[start] = array[end];

array[end] = temp;

start++;

end--;

}

cout << "start =" << start << endl;

cout << "end = "<< end << endl;

for (int i = 0; i < freq1; i++)

{

cout << array[i] << endl;

}

system("pause");

return 0;

}

 

4、案例練習:氣泡排序

作用:

最常用的排序演算法,

對陣列內元素進行排序. 比較相鄰的元素,如果第一個比第二個大,就交換他們兩個 對每一對相鄰元素做同樣的工作,執行完畢後,找到第一個最大的值 重複以上的步驟,每次比較次數-1,直到不需要比較

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "氣泡排序練習:" << endl;

//利用氣泡排序實現升序序列

int arr[9] = { 4,2,8,0,5,7,1,3,9 };

cout << "交換前:"<< endl;

for (int i = 0; i < 9; i++)

{

cout << arr[i] << endl;

}

//1.讓所有相鄰的兩個元素進行比較

int temp = 0;

for (int i = 0; i < 9; i++)

{

for (int j=0;j<9-i-1;j++)

{

if (arr[j] > arr[j+1])

{

temp = arr[j+1];

arr[j+1] = arr[j];

arr[j] = temp;

}

}

}

//2.因為每比完一次一定會出現一個最大值,在末尾

//3.所以每比較一次,相鄰的兩個元素比較次數 -1

//4.第一次迴圈比較元素個數減一次

cout << "交換後:"<< endl;

for (int i = 0; i < 9; i++)

{

cout << arr[i] << endl;

}

system("pause");

return 0;

}

 

5 、二維陣列

二維陣列就是在一維陣列上,多加一個維度。

定義方式(4種):

1.資料型別 陣列名 [行數] [列數];

2.資料型別 陣列名 [行數] [列數] ={ {資料1,資料2},{資料3,資料4} } ;

3.資料型別 陣列名 [行數] [列數] = { 資料1,資料2,資料3 ,資料 4};

4.資料型別 陣列名 [ ] [列數] ={ 資料1,資料2,資料3 ,資料 4 };

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "二維陣列的定義練習:"<< endl;

/*

二維陣列定義方式:

1.資料型別 陣列名[行數][列數];

2.資料型別 陣列名[行數][列數]={{資料1,資料2,資料3},{資料4,資料5,資料6},{資料7,資料8,資料9}};

3.資料型別 陣列名[行數][列數]={資料1,資料2,資料3,資料4,資料5,資料6,資料7,資料8,資料9};

4.資料型別 陣列名[][列數] ={資料1,資料2,資料3,資料4,資料5,資料6,資料7,資料8,資料9};

*/

//1.資料型別 陣列名[行數][列數];

int arr1[2][3]; int num = 0;

//外層迴圈列印行數,內層迴圈列印列數

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{

arr1[i][j] = num; num++;

}

}

//外層迴圈列印行數,內層迴圈列印列數

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{

cout << arr1[i][j] << endl;

}

}

cout << "------------------" << endl;

//2.資料型別 陣列名[行數][列數] = { {資料1,資料2,資料3},{資料4,資料5,資料6},{資料7,資料8,資料9} };

int arr2[2][3] = { {1,2,3},{4,5,6} };

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{

cout << arr2[i][j] <<" ";

}

cout << endl;

}

cout << "-----------------"<< endl;

//3.資料型別 陣列名[行數][列數]={資料1,資料2,資料3,資料4,資料5,資料6,資料7,資料8,資料9};

int arr3[2][3] = { 1,2,3,4,5,6 };

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{

cout << arr3[i][j] << " ";

}

cout << endl;

}

cout << "------------------" << endl;

//4.資料型別 陣列名[][列數] ={資料1,資料2,資料3,資料4,資料5,資料6,資料7,資料8,資料9};

int arr4[][3] = { 1,2,3,4,5,6 };

for (int i = 0; i < 2; i++)

{

for (int j = 0; j < 3; j++)

{ cout << arr4[i][j] << " ";

}

cout << endl;

}

system("pause");

return 0;

}

 

.5.1、二維陣列陣列名

檢視二維陣列所佔記憶體空間

獲取二維陣列首地址

 

eg:

#include <iostream>

using namespace std;

int main()

{

cout << "二維陣列陣列名練習:"<< endl;

//1.檢視二維陣列所佔記憶體空間

int array[2][3]= { {1,2,3}, {4,5,6} };

cout <<"二維陣列所佔記憶體空間為:" << sizeof(array) << "個位元組" << endl;

cout << "第一行所佔空間位:"<< sizeof(array[0]) << "個位元組" << endl;

cout << "第二行所佔空間位:"<< sizeof(array[1]) << "個位元組"<< endl;

cout <<"第一個元素所佔記憶體空間為:" << sizeof(array[0][0]) << "個位元組"<< endl;

cout << "二維陣列的行數為:" << sizeof(array) / sizeof(array[0]) <<"行" << endl;

cout << "二維陣列的列數為:"<< sizeof(array[0]) / sizeof(array[0][0]) << "列" << endl;

//2.檢視陣列首地址

cout <<";二維陣列的首地址是:" << array << endl;

cout << "二維陣列的首地址是:" << &array << endl;

cout << "二維陣列的首地址是:" << &array[0][0] << endl;

cout <<"二維陣列的第一行首地址是:" << &array[0][0] << endl;

system("pause");

return 0;

}

 

5.2、二維陣列應用案例  考試成績統計:

案例描述:有三名同學(張三,李四,王五),再一次開始中的人成績分別如下表,請分別輸出三名同學的總成績

 

eg:

#include <iostream>

#include <string>

using namespace std;

int main()

{

cout << "輸出三名同學的總成績:" << endl;

int achent[3][3] = { {100,100,100}, {90,50,100}, {60,70,80} };

string name[3] = { "張三","李四","王五" };

for (int i = 0; i < 3; i++)

{

int score = 0;

for (int j = 0; j < 3; j++)

{

score += achent[i][j];

}

cout << name[i] << "的成績:"<< score << endl;

}

return 0;

}