重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設、域名注冊、服務器等服務
為企業(yè)提供網(wǎng)站建設、域名注冊、服務器等服務
所謂數(shù)組,就是一個集合,里面存放相同類型的數(shù)據(jù)元素
成都創(chuàng)新互聯(lián)服務緊隨時代發(fā)展步伐,進行技術革新和技術進步,經過10年的發(fā)展和積累,已經匯集了一批資深網(wǎng)站策劃師、設計師、專業(yè)的網(wǎng)站實施團隊以及高素質售后服務人員,并且完全形成了一套成熟的業(yè)務流程,能夠完全依照客戶要求對網(wǎng)站進行網(wǎng)站設計制作、網(wǎng)站制作、建設、維護、更新和改版,實現(xiàn)客戶網(wǎng)站對外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。
一維數(shù)組定義的三種方式:
數(shù)據(jù)類型 數(shù)組名[數(shù)組長度];
數(shù)據(jù)類型 數(shù)組[數(shù)組長度]={值1,值2,...};
數(shù)據(jù)類型 數(shù)組名[] = {值1,值2,...};
數(shù)組特點:
示例
#include
using namespace std;
int main()
{
//1、數(shù)據(jù)類型 數(shù)組名[數(shù)組長度]
int arr[5];
//給數(shù)組中的元素進行賦值
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
//訪問數(shù)據(jù)元素
cout << arr[0] << endl;
//2、數(shù)據(jù)類型 數(shù)組名[數(shù)組長度] = {值1,值2...}
//如果在初始化數(shù)據(jù)時候,沒有全部填寫完,會用0來填補剩余的數(shù)據(jù)
// int arr2[5] = {1,2,3}
int arr2[5] = { 10,20,30,40,50 };
cout << arr2[3] << endl;
//利用循環(huán)的方式 輸出數(shù)組中的元素
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << endl;
}
//3、數(shù)據(jù)類型 數(shù)組名[] = {值1,值2...}
//定義數(shù)組的時候,必須有初始的長度
int arr3[] = {90,80,70,60,30,20,10,1,2,3,4};
for (int j = 0; j < 9; j++)
{
cout << arr3[j] << endl;
}
system("pause");
return 0;
}
總結1:數(shù)組名的命名規(guī)范與變量名規(guī)范一致,不要出現(xiàn)變量重名
總結2:數(shù)下標是從0開始索引的
一維數(shù)組名稱的用途:
示例
#include
using namespace std;
int main()
{
//數(shù)組名用途
//1、可以通過數(shù)組名統(tǒng)計整個數(shù)組占用內存大小
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "整個數(shù)組占用內存空間為:" << sizeof(arr) << endl;
cout << "每個元素占用內存空間為:" << sizeof(arr[0]) << endl;
cout << "數(shù)組中元素個數(shù)為:" << sizeof(arr) / sizeof(arr[0]) << endl;
//2、可以通過數(shù)組名查看數(shù)組首地址
cout << "數(shù)組首地址為:" << (int)arr << endl;
cout << "數(shù)組中第一個元素地址為:" << (int)&arr[0] << endl;
cout << "數(shù)組中第二個元素地址為:" << (int)&arr[1] << endl;
system("pause");
return 0;
}
案例描述:在一組數(shù)組中記錄了五只小豬的體重,如:int arr[5]={300,350,400,200,250};
,找出并打印最重的小豬體重。
解題思路:找到數(shù)組中的最大值,訪問數(shù)組中每個元素,如果這個元素比我認定的最大值要大,更新最大值。
解題程序:
#include
using namespace std;
int main()
{
//1、創(chuàng)建5只小豬體重的數(shù)組
int arr[5] = { 300,350,200,400,250 };
//2、從數(shù)值中找到最大的值
int max = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
//3、打印最大的值
cout << "max = " << max << endl;
system("pause");
return 0;
}
案例描述:請聲明一個5個元素的數(shù)組,并且將元素逆置。
(如原數(shù)組元素為:1,3,2,5,4;逆置輸出結果為:4,5,2,3,1)
解題思路:找到起始位置元素下標和末尾元素位置下標互換,需要一個中間temp用來互換
示例:
#include
using namespace std;
int main()
{
//1、創(chuàng)建數(shù)組
int arr[5] = { 1,3,2,5,4 };
cout << "數(shù)組逆置前" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
//2、實現(xiàn)逆置
//2.1記錄起始下標位置
//2.2記錄結束下標位置
//2.3起始下表與結束下標的元素互換
//2.4起始位置++,結束位置--
//2.5循環(huán)執(zhí)行2.1操作,起止位置 >=結束位置
int start = 0;
int end = sizeof(arr) / sizeof(arr[0]) - 1;
while (start < end)
{
//實現(xiàn)元素互換
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
//下標更新
start++;
end--;
}
//3、數(shù)組元素逆置后
cout << "數(shù)組元素逆置后:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
}
作用:最常用的排序算法,對數(shù)組內元素進行排序
解答:
#include
using namespace std;
int main()
{
//利用冒泡排序實現(xiàn)升序序列
int arr[9] = { 4,2,8,0,5,7,1,3,9};
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
//開始冒泡排序
//總過排序輪數(shù)為元素個數(shù)-1
for (int i = 0; i < 9 - 1; i++)
{
//內層循環(huán)對比次數(shù) = 元素個數(shù)-當前輪數(shù)-1
for (int j = 0; j < 9 - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
二維數(shù)組就是在一維數(shù)組上,多加了一個維度。
二維數(shù)組定義的四種方式:
數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ];
數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {{數(shù)據(jù)1, 數(shù)據(jù)2}, {數(shù)據(jù)3, 數(shù)據(jù)4}};
數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};
數(shù)組類型 數(shù)組名[ ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};
建議:以上四種定義方式,利用第二種更加直觀,提高代碼的可讀性
示例:
#include
using namespace std;
int main()
{
//二維數(shù)組
/*
1. `數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ];`
2. `數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {{數(shù)據(jù)1, 數(shù)據(jù)2}, {數(shù)據(jù)3, 數(shù)據(jù)4}};`
3. `數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};`
4. `數(shù)組類型 數(shù)組名[ ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};`
*/
//1、數(shù)據(jù)類型 數(shù)組名[行數(shù)][列數(shù)];
int arr1[2][3];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[0][2] = 3;
arr1[1][0] = 4;
arr1[1][1] = 5;
arr1[1][2] = 6;
//外層循環(huán)打印行數(shù),內存行數(shù)打印列數(shù)
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr1[i][j] << " ";
}
cout << endl;
}
//2. `數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {{數(shù)據(jù)1, 數(shù)據(jù)2}, {數(shù)據(jù)3, 數(shù)據(jù)4}};`
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;
}
//3. `數(shù)組類型 數(shù)組名[ 行數(shù) ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};`
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;
}
//4. `數(shù)組類型 數(shù)組名[ ][ 列數(shù) ] = {數(shù)據(jù)1, 數(shù)據(jù)2, 數(shù)據(jù)3, 數(shù)據(jù)4};`
int arr4[][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;
}
system("pause");
return 0;
}
示例:
#include
using namespace std;
int main()
{
//二維數(shù)組數(shù)組名稱用途
//1、可以查看占用內存空間大小
int arr[2][3] =
{
{1,2,3},
{4,5,6}
};
cout << "二維數(shù)組數(shù)占用內存空間大小:" << sizeof(arr) << endl;
cout << "二維數(shù)組第一行占用的內存空間為:" << sizeof(arr[0]) << endl;
cout << "二維數(shù)組第一個元素占用內存為:" << sizeof(arr[0][0]) << endl;
cout << "二維數(shù)組行數(shù)為:" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二維數(shù)組列數(shù)為:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
//2、查看二維數(shù)組的首地址
cout << "二位數(shù)字的首地址為:" << (int)arr << endl;
cout << "二維數(shù)組第一行首地址為:" << (int)arr[0] << endl;
cout << "二維數(shù)組第二行首地址為:" << (int)arr[1] << endl;
cout << "二維數(shù)組第一個元素首地址:" << (int)&arr[0][0] << endl;
system("pause");
return 0;
}
考試成績統(tǒng)計:
案例描述:有三個同學(張三,李四,王五),在一次考試中的成績分別如下表,請分別輸出三名同學的總成績
語文 | 數(shù)學 | 英語 | |
---|---|---|---|
張三 | 100 | 100 | 100 |
李四 | 90 | 50 | 100 |
王五 | 60 | 70 | 80 |
思路:
參考答案:
#include
#include
using namespace std;
int main()
{
//二維數(shù)組案例-考試成績統(tǒng)計
//1、創(chuàng)建二維數(shù)組
int scores[3][3] =
{
{100,100,100},
{90,50,100},
{60,70,80}
};
string names[3] = {"張三","李四","王五"};
for (int i = 0; i < 3; i++)
{
int sum = 0;//統(tǒng)計分數(shù)總和的變量
for (int j = 0; j < 3; j++)
{
sum += scores[i][j];
//cout << scores[i][j] << " ";
}
cout << names[i] << "的總分為:" << sum << endl;
//cout << "sum = " << sum << endl;
}
system("pause");
return 0;
}