2021-01-23 打卡学习C++第三天


一、数组

所谓数组,就是一个集合,里面存放了相同类型的数据元素

特点:

  • 数组中每个数据元素都是相同的数据类型

  • 数组是由连续的内存位置组成的

  • 我们可以通过下标来访问数组中的元素,数组元素下标是从0开始

1、一维数组

1.数据类型  数组名[ 数组长度 ]2.数据类型  数组名[ 数组长度 ]={
    
    1,值2...}3.数据类型  数组名[  ] = {
    
    1,值2...}

一维数组名用途:

  • 统计整个数组在内存中中的长度:sizeof(数组名)

  • 获取数组在内存中的首地址:cout<<数组名<<endl;

示例: 查看数组长度和数组首地址

#include<iostream>
using namespace std;

int main()
{
    
    
	int arr[] = {
    
     1,2,3,4,5,6 };
	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;

}

输出结果
在这里插入图片描述

冒泡排序

最常用的排序算法,对数组内元素进行排序
做法:

  1. 比较相邻的元素,如果第一个比第二个大,就交换它们两个

  2. 对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值

  3. 重复以上步骤,每次比较次数-1,直到不需要比较为止

排序总轮数 = 元素个数 - 1
每轮对比次数 = 元素个数 - 排序轮数 - 1

案例分析
给出一组数据 { 4,2,8,0,5,7,1,3,9 },按照冒泡排序的方法对其进行升序排列
在这里插入图片描述

示例

#include<iostream>
using namespace std;

//利用冒泡排序进行升序排列
int main()
{
    
    
	int arr[] = {
    
     4,2,8,0,5,7,1,3,9 }, count, i, j, num;
	count = sizeof(arr) / sizeof(arr[0]);
	cout << "给出一组数:" << arr[0];
	for (i = 1; i < count; i++)
	{
    
    
		cout << ","<< arr[i];
	}
	cout << "\n对这组数进行升序排列" << endl;
	
	for (i = 0; i < count-1; i++)
	{
    
    
		for (j = 0; j < count-i-1; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				num = arr[j+1];
				arr[j+1] = arr[j];
				arr[j] = num;
			}
		}
	}

	cout << "利用冒泡排序进行升序排列后:" << arr[0];
	for (i = 1; i < count; i++)
	{
    
    
		cout << "," << arr[i];
	}
	cout << "\n";
	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

(2)二维数组

1.数据类型 数组名[ 行数 ] [ 列数 ]2.数据类型 数组名[ 行数 ] [ 列数 ] = {
    
     {
    
    数据1,数据2}{
    
    数据3,数据4} }3.数据类型 数组名[ 行数 ] [ 列数 ] = {
    
    数据1,数据2,数据3,数据4}4.数据类型 数组名[  ] [ 列数 ] = {
    
    数据1,数据2,数据3,数据4}

二维数组名的用途:

  • 查看二维数组所占内存

  • 获取二维数组首地址

示例

#include<iostream>
using namespace std;

int main()
{
    
    
	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[0][0] << endl;


	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

二、函数

将一段经常使用的代码封装起来,减少重复代码
一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能

1、函数的定义:

  1. 返回值类型

  2. 函数名

  3. 参数列表

  4. 函数体语句

  5. return 表达式

返回值类型  函数名(参数列表)
{
    
    
	函数体语句
		
	return表达式	
}

在这里插入图片描述

2、函数的调用

函数名(参数);

函数定义里小括号内称为形参,函数调用时传入的参数称为实参

示例

#include<iostream>
using namespace std;

//求和
int add(int num1, int num2)
{
    
    
	int sum = num1 + num2;
	return sum;
}
int main()
{
    
    
	int a = 10;
	int b = 20;
	int c = add(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;

	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

3、值传递

所谓值传递,就是函数调用时实参将数值传入给形参

值传递时,如果形参发生,并不会影响实参

如果函数不需要返回值,声明的时候可以写void

示例

#include<iostream>
using namespace std;

//交换两值
void swap(int num1, int num2)
{
    
    
	cout << "交换前:" << num1 << " " << num2 << endl;
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "交换后:" << num1 << " " << num2 << endl;
}

int main()
{
    
    
	int a = 10;
	int b = 20;
	cout << "调用函数前:a = " << a << " b = " << b << endl;
	swap(a, b);
	cout << "调用函数后:a = " << a << " b = " << b << endl;

	system("pause");
	return 0;
}

输出结果
在这里插入图片描述

4、函数的常见样式

  1. 无参无返

  2. 有参无返

  3. 无参有返

  4. 有参有返

示例

#include<iostream>
using namespace std;

//1、无参无返
void test01()
{
    
    
	cout << "this is test01 " << endl;
}

//2、有参无返
void test02(int num)
{
    
    
	cout << "this is test02 X = " << num << endl;
}

//3、无参有返
int test03()
{
    
    
	int flag = 1;
	return flag;
}

//4、有参有返
int test04(int num1, int num2)
{
    
    
	int sum = num1 + num2;
	return sum;
}

int main()
{
    
    
	test01();
	int a = 10;
	test02(a);
	int b = test03();
	cout << "this is test03 b = " << b << endl;
	int c = test04(a, b);
	cout << "this is test04 c = " << c << endl;

	system("pause");
	return 0;

}

输出结果
在这里插入图片描述

5、函数声明

告诉编译器函数名称及如何调用函数,函数的实际主体可以单独定义

函数的声明可以多次,但是函数的定义只能有一次

示例

#include<iostream>
using namespace std;

int add(int num1, int num2);
int main()
{
    
    
	int a = 10;
	int b = 20;
	int c = add(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;

	system("pause");
	return 0;

}

int add(int num1, int num2)
{
    
    
	int sum = num1 + num2;
	return sum;
}

输出结果
在这里插入图片描述

6、函数的分文件编写

函数份文件编写一般有4个步骤:

  1. 创建后缀名为 .h 的头文件

  2. 创建后缀为 .cpp 的源文件

  3. 在头文件中写函数的声明

  4. 在源文件中写函数的定义

头文件.h

#include<iostream>
using namespace std;

void swap(int a, int b);

函数.cpp

#include"swap.h"

void swap(int a, int b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

主程序调用.cpp

#include"swap.h"

void swap(int a, int b)
{
    
    
	int temp = a;
	a = b;
	b = temp;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

【注释】 学习课程为-黑马程序C++教程

猜你喜欢

转载自blog.csdn.net/qq_42616280/article/details/113029537