5.1数组
int main(){
int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
int arr2[5] = {10,20,30,40,50};
"""
cout<<arr2[0]<<endl;
cout<<arr2[1]<<endl;
cout<<arr2[2]<<endl;
cout<<arr2[3]<<endl;
cout<<arr2[4]<<endl;
"""
for (int i = 0;i<5;i++)
{
cout<<arr2[i]<<endl;
}
system("pause");
return 0;
}
5.2.2一维数组数组名
int main(){
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
cout<<"整个数组占用内存空间为:"<<sizeof(arr)<<endl;
cout<<"每个元素占用内存空间为:"<<sizeof(arr[0])<<endl;
cout<<"数组中元素个数为:"<<sizeof(arr)/sizeof(arr[0])<<endl;
cout<<"数组首地址为:"<<(int)arr<<endl;
cout<<"数组中第一个元素的地址为:"<<(int)&arr[0]<<endl;
cout<<"数组中第二个元素的地址为:"<<(int)&arr[1]<<endl;
system("pause");
return 0;
}
案例:五只小猪称体重
int main(){
int arr[5] = {300,350,200,400,250};
int max = 0;
for(int i=0;i<5;i++)
{
if(arr[i]>max)
{
max = arr[i];
}
}
cout<<"最重的小猪体重为:"<<max<<endl;
system("pause");
return 0;
}
案例:数组元素逆置
int main(){
int arr[5] = {1,3,2,5,4};
cout<<"数组逆置前"<<endl;
for(int i = 0;i<5;i++)
{
cout<<arr[i]<<endl;
}
int start = 0;
int end = sizeof(arr)/sizeof(arr[0])-1;
while(start<end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
cout<<"数组元素逆置后"<<endl;
for(int i = 0;i<5;i++)
{
cout<<arr[i]<<endl;
}
system("pause");
return 0 ;
}
5.2.3冒泡排序
int main(){
int arr[9] ={4,2,8,0,5,7,1,3,9};
cout<<"未排序前的元素:"<<endl;
for(int i=00;i<9;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
for(int i = 0;i<9-1;i++)
{
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;
}
}
}
cout<<"排序后的元素"<<endl;
for(int i=00;i<9;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
5.3二维数组
int main(){
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
for(int i = 0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
//第二种
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;
}
//第三种
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;
}
//第四种
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;
}
5.3.2二维数组数组名
int main(){
int arr[3][3]=
{
{1,2,3},
{4,5,6}
};
cout<<"二维数组占用内存空间为::"<<sizeof(arr)<<endl;
cout<<"二维数组第一行占用内存空间为::"<<sizeof(arr[0])<<endl;
cout<<"二维数组第一个元素占用内存为:"<<sizeof(arr[0][0]<<endl;
cout<<"二维数组行数为:"<<sizeof(arr)/sizeof(arr[0])<<endl;
cout<<"二维数组列数为:"<<sizeof(arr[0])/sizeof(arr[0][0])<<endl;
cout<<"二维数组的首地址为:"<<(int)arr<<endl;
cout<<"二维数组第一行首地址:"<<(int)arr[0]<<endl;
cout<<"二维数组第二行首地址为:"<<(int)arr[1])<<endl;
cout<<"二维数组第一个元素首地址:"<<(int)&arr[0][0]<<endl;
cout<<"二维数组第二个元素首地址:"<<(int)&arr[0][1]<<endl;
system("pause");
return 0;
}
5.3.3二维数组应用案例
#include<iostream>
using namespace std;
#include<string>
int main(){
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;
for(int j = 0;j<3;j++)
{
sum +=scores[i][j];
//cout<<scores[i][j]<<" ";
}
cout<<names[i]<<i+1<<"的总分为:"<<sum<<endl;
}
system("pause");
return 0;
}