数组
/*数组*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//1. 数据类型 数组名[数组长度];
int arr[5];
//数组元素赋值
//数组元素下标是从0开始索引的
arr[0] = 10;
//访问数组元素
cout << arr[0];
//2. 数据类型 数组名[数组长度]={值1,值2,...};
//如果在初始化时没有全部写完,则会用0来填补剩余的数据
int arr2[5] = { 10,20,30 };
for (int i = 0; i < 5; i++)
{
cout << arr2[i] << endl;
}
//2. 数据类型 数组名[]={值1,值2,...};
//定义数组的时候,必须要有初始长度
int arr3[] = { 10,20,30,40,50,60 };
system("pause");
return 0;
}
/*一维数组数组名*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
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 << "数组首地址为" << arr << endl;
cout << "数组中第一个元素的地址为" << &arr[0] << endl;
cout << "数组中第二个元素的地址为" << &arr[1] << endl;
//数组名是一个常量,不可以对其进行赋值
system("pause");
return 0;
}
/*一维数组练习案例:五只小猪称体重*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
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;
}
/*一维数组练习案例:数组元素逆置*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//先声明一个数组[3,4,2,5,1],逆置成[1,5,2,4,3]
//1.创建数组
int arr[5] = { 3,4,2,5,1 };
cout << "逆置前的数组:" << endl;
for (int j = 0; j < 5; j++)
{
cout << arr[j] << endl;
}
//2.实现逆置
//2.1记录起始下标位置
//2.2记录结束下标位置
//2.3起始和结束位置的元素互换
//2.4起始位置++,结束位置--
//2.5循环执行2.1,直到起始位置大于等于结束位置
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 j = 0; j < 5; j++)
{
cout << arr[j] << endl;
}
system("pause");
return 0;
}
冒泡排序
比较相邻的元素,如果第一个元素比第二个大,就交换他们
对每一对相邻的元素进行同样的操作,执行完毕后,找到第一个最大值
重复上述步骤,比较次数-1,直到不需要进行比较
/*冒泡排序*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
int arr[9] = {4,2,8,0,5,7,1,3,9};
cout << "排序前的顺序为:" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
int len = sizeof(arr) / sizeof(arr[0]);
int temp = 0;
//我自己的方法
while (len > 1)
{
for (int j = 0; j < len - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
len--;
}
//老师的方法
//总共排序轮数:元素个数-1
for (int i = 0; i < len - 1; i++)
{
//内层循环对比 次数=元素个数-当前轮数-1
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "排序后的顺序为:" << endl;
for (int i = 0; i < 9; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
二维数组
/*二维数组*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//二维数组定义方式
int arr[2][3] = { {1,2,3},{4,5,6} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
/*二维数组数组名*/
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
//可以查看占用内存空间大小
int arr[2][3] = { {1,2,3},{4,5,6} };
cout << "二维数组所占空间大小:" << sizeof(arr) << endl;//24
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;
system("pause");
return 0;
}
/*二维数组案例:考试成绩统计*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
int scores[3][3] = { {100,100,100},{90,50,100},{60,70,80} };
string name[3] = {"张三","李四","王五"};
for (int i = 0; i < 3; i++)
{
int sum = 0;
for (int j = 0; j < 3; j++)
{
sum = sum + scores[i][j];
}
cout << name[i] << "的考试总分为:" << sum << endl;
}
system("pause");
return 0;
}