C++学习笔记day3

1、一维数组

#include<iostream>
using namespace std;

int main()
{
	//数组
	/*
		1、数据类型 数组名[数组长度];
		2、数据类型 数组名[数组长度] = {值1,值2.....}
		3、数据类型 数组名[]={值1,值2.....}
	
	*/

	//1、数据类型 数组名[数组长度];

	//int arr[5];

	给数组中的元素进行赋值
	数组元素的下标从0开始索引的
	//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;*/


	//2、数据类型 数组名[数组长度] = { 值1,值2.....}
	/*int  arr2[5] = { 10,20,30,40,50 };
	for (int i = 0; i < 5; i++)
		cout << arr[i]<<endl;
*/
	//3、数据类型 数组名[]={值1,值2.....}
	//定义数组的时候,必须有初始长度
		/*int arr3[] = { 90,80,70,60,50,40,30,20,10 };
		for (int j = 0; j < 9; j++)
			cout << arr3[j] << endl;*/
	//数组名用途
	//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;
	//cout << "数组中第二个元素地址为:" << (int)&arr[1] << endl;
	数组名是常量,不可以进行赋值操作
	arr = 100;错误
	//1、创建5只小猪体重的数组
	//int arr[5] = { 300,350,200,400,250 };

	2、从数组中找到最大体重
	//int max = 0;
	//for (int i = 0; i < 5; i++)
	//{
	//	if (arr[i] > max)
	//	{
	//		max = arr[i];
	//	}
	//}
	//
	3、打印最大值
	//cout << "最重的小猪体重为:"<< max << endl;
	int arr[] = { 4,3,2,5,1 };
	int start = 0;
	int end = sizeof(arr) / sizeof(arr[0]) - 1;
	int temp = arr[start];
	cout << "元素逆置前:" << endl;
	for (int i = 0; i <= end; i++)
		cout << arr[i] << endl;

	//2、实现逆置
	//2.1记录起始下标位置
	//2.2记录结束下标位置
	//2.3起始下标位置和结束下标的元素互换
	//2.4起始位置++结束位置--
	//2.5循环执行2.1操作,直到起始位置>=结束位置
	
	while (start < end)
	{
		temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
		start++;
		end--;
	}
	//3、打印逆置后的数组
	cout << "数组元素逆置后:" << endl;
	for (int j = 0; j < (sizeof(arr) / sizeof(arr[0])); j++)
		cout << arr[j] << endl;
	system("pause");



	return 0;
}

2、排序

#include<iostream>
using namespace std;

int main()
{
	int arr[9] = { 5,2,6,8,3,7,9,1,4 };
	int temp = 0;
	cout << "冒泡排序前的顺序为:";
	for (int i = 0; i < 9; i++)
		cout << arr[i]<<" ";
	cout << endl;
	//开始冒泡排序
	for (int i = 0; i<8; i++)
	{	
		//	内层循环对比 次数=元素个数-当前轮数-1
		for (int j = 0; j <(9 - i-1); j++)
		{
			if (arr[j] > arr[j + 1])
			{
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	cout << "冒泡排序后的顺序为:";
	for (int j = 0; j < 9; j++)
		cout << arr[j]<<" ";
	cout << endl;

	system("pause");

	return 0;

}

3、二维数组

#include<iostream>
#include<string>
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++)
	//{
	//	for (int j = 0; j < 3; j++)
	//		cout << arr[i][j] << " ";
	//	cout << endl;
	//}
	2、数据类型 数组名[列数][行数] ={{数据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;
	//}

	3、数据类型 数组名[行数][列数] = {数据1,数据2,数据3,数据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、数据类型 数组名[][列数] = {数据1,数据2,数据3,数据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 << arr4[i][j] << " ";
	//	cout << endl;
	//}
	//二维数组名称用途

	//1、可以查看占用内存空间大小

	//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;

	2、可以查看二维数组首地址
	//cout << "二维数组首地址为:" << arr << endl;
	//cout << "二维数组第一行首地址为:" << arr[0] << endl;

	//考试成绩统计
	//1、创建二维数组
	string names[] = { "张三","李四","王五" };
	string subjects[]={"姓名 ","语文","数学","英语","总分"};
	int scores[3][3]=
	{
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};

	//2、统计每个人的总和分数
	for (int i = 0; i <5; i++)
		cout << subjects[i] << "\t ";
	cout << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << names[i]<<"\t";
		int sum = 0;//统计分数总和变量
		for (int j = 0; j < 3; j++)
		{
			
			sum += scores[i][j];
			cout << scores[i][j]<<"\t ";
		}
		cout  << sum << endl;
	}
	system("pause");


	 

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值