数组定义类型
#include <iostream>
using namespace std;
int main()
{
//1.数据类型 数组名[ 数组长度 ];
int arr1[5];
arr1[0] = 10;//注意数组是由0开始索引的
arr1[1] = 20;
arr1[2] = 30;
arr1[3] = 40;
arr1[4] = 50;
cout << arr1[0] << endl;
cout << arr1[1] << endl;
cout << arr1[2] << endl;
cout << arr1[3] << endl;
cout << arr1[4] << endl;
//2.数据类型 数组名[ 数组长度 ] = { 值1,值2 ...};
int arr2[5] = { 10, 20, 30 };//如果初始化数据并未填充完毕,其余将由0代替;
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << endl;
}
//3.数据类型 数组名[ ] = { 值1,值2 ...};
int arr3[] = { 10, 20, 30, 40, 50, 60 };
for (int i = 0; i < 6; i++)
{
cout << arr3[i] << endl;
}
return 0;
一维数组名称的用途(数组名是常量了,不可以进行赋值了)
#include <iostream>
using namespace std;
int main()
{
//一维数组名称的用途:
//1.可以统计整个数组在内存中的长度
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;
//2.可以获取数组在内存中的首地址
cout << "数组首地址为:" << (int)arr << endl;
cout << "数组第一个元素地址为:" << (int)&arr[0] << endl;//&别忘了
return 0; //这里的int是把16进制转化为10进制
}
数组案例求最大值(没看教程打的)
using namespace std;
int main()
{
int arr[5] = { 300,350,400,200,250};
int a = 0;
for (int i = 0; i < 5; i++)
{
if (arr[i] > a)
{
a = arr[i];
}
}
cout << a << endl;
return 0;
数组案例的逆置
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1,4,5,2,7 };
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 a = arr[start];
arr[start] = arr[end];
arr[end] = a;
start++; end--;
}
cout << "逆置后的数组:" << endl;
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
system("pause");
return 0;
}
冒泡排序***理解
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 4,2,8,0,5,7,1,3,9 };
cout << "排序前的顺序:" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
for (int i = 0; i < 9 - 1; i++)//我有9个元素,那就要弄八次排序
{
for (int j = 0; j < 9 - 0 - 1; j++)//举例:在第0组有九个元素所以要进行八次排序;同理
{
if(arr[j]>arr[j+1])
{
int a = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = a;
}
}
}
cout << "冒泡排序后的顺序:" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
二维数组的定义方式:
#include <iostream>
using namespace std;
int main()
{
//1.数据类型 数组名[行数][列数];
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++)
{
if (i == 1)//这行代码是我自己加的为的就是看起来好看
{ //但是后面有更方便的写法
cout << endl;
}
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
}
cout << endl;
//2.数据类型 数组名[行数][列数] = { {数据1,数据2 } ,{数据3,数据4 } };
int arr1[2][3] =
{
{1,2,3},
{4,5,6}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr1[i][j] << " ";
}
cout << endl;
}
//3.数据类型 数组名[行数][列数] = { 数据1,数据2,数据3,数据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;
}
//4.数据类型 数组名[][列数] = { 数据1,数据2,数据3,数据4 }
int arr3[][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;
}
return 0;
}
二维数组名的用途:
#include <iostream>
using namespace std;
int main()
{
//用途:1.查看二维数组所占内存空间2.获取二维数组首地址
int arr[2][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 << "二维数组首地址:" << arr << endl;
cout << "二维数组第一行地址:" << arr[0] << endl;
cout << "二维数组第二行地址:" << arr[1] << endl;
cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;
return 0;
}
二维数组案例(复习到了string,和运算符)
using namespace std;
int main()
{
int scores[3][3] =
{
{100,100,100},
{60,60,60},
{30,40,50}
};
for (int i = 0; i < 3; i++)
{
int a = 0;
for (int j = 0; j < 3; j++)
{
a += scores[i][j];
}
string name[3]{ "张三","李四","王五" };
cout << name[i] << "的总分为:" << a << endl;
}
return 0;