C++ Basic Introductory Tutorial (3)

5 arrays

5.1 Overview

The so-called array is a collection that stores data elements of the same type.
Feature 1: Each data element in the array is of the same data type.
Feature 2: The array is composed of consecutive memory locations

5.2 One-dimensional arrays

5.2.1 Ways to define one-dimensional array
Three ways to define one-dimensional array:
data type array name [array length];
data type array name [array length] = {value 1, value 2 ...};
data type array name [ ] = { value1, value2 ... };

example

int main() 
{
    
    
	//定义方式1
	//数据类型 数组名[元素个数];
	int score[10];
	//利用下标赋值
	score[0] = 100;
	score[1] = 99;
	score[2] = 85;
	//利用下标输出
	cout << score[0] << endl;
	cout << score[1] << endl;
	cout << score[2] << endl;
	//第二种定义方式
	//数据类型 数组名[元素个数] =  {值1,值2 ,值3 ...};
	//如果{}内不足10个数据,剩余数据用0补全
	int score2[10] = {
    
     100, 90,80,70,60,50,40,30,20,10 };
	//逐个输出
	//cout << score2[0] << endl;
	//cout << score2[1] << endl;
	//一个一个输出太麻烦,因此可以利用循环进行输出
	for (int i = 0; i < 10; i++)
	{
    
    
		cout << score2[i] << endl;
	}
	//定义方式3
	//数据类型 数组名[] =  {值1,值2 ,值3 ...};
	int score3[] = {
    
     100,90,80,70,60,50,40,30,20,10 };
	for (int i = 0; i < 10; i++)
	{
    
    
		cout << score3[i] << endl;
	}
	system("pause");
	return 0;
}

Summary 1: The naming convention of the array name is consistent with the naming convention of the variable name, do not have the same
name as the
variable. Summary 2: The subscript in the array starts from 0 for indexing The length of the array in memory You can get the first address of the array in memory Example:



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;
	cout << "数组中第二个元素地址为: " << (int)&arr[1] << endl;
	//arr = 100; 错误,数组名是常量,因此不可以赋值
	system("pause");
	return 0;
}

Note: The array name is a constant and cannot be assigned.
Summary 1: Print the array name directly, and you can view the first address of the memory occupied by the array.
Summary 2: Perform sizeof on the array name to obtain the size of the memory space occupied by the entire array.
5.2.3 Bubble Sorting
Function: The most commonly used sorting algorithm, sorting the elements in the array
and comparing adjacent elements. If the first is bigger than the second, swap them both.
Do the same for each pair of adjacent elements, and when done, find the first maximum value.
Repeat the above steps, each time the number of comparisons is -1, until no comparison is required

Example: Sort the array { 4,2,8,0,5,7,1,3,9 } in ascending order

int main() 
{
    
    
	int arr[9] = {
    
     4,2,8,0,5,7,1,3,9 };
	for (int i = 0; i < 9 - 1; i++)
	{
    
    
		for (int j = 0; j < 9 - 1 - i; j++)
		{
    
    
			if (arr[j] > arr[j + 1])
			{
    
    
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	for (int i = 0; i < 9; i++)
	{
    
    
		cout << arr[i] << endl;
	}
	system("pause");
	return 0;
}

5.3 Two-dimensional arrays

A two-dimensional array is an extra dimension added to a one-dimensional array.
5.3.1 Ways to define two-dimensional array
Four ways to define two-dimensional array:
data type array name [row number] [column number];
data type array name [row number] [column number] = { {data 1, data 2 }, {data3, data4} };
data type array name [rows] [columns] = { data1, data2, data3, data4}; data
type array name [] [columns] = { Data 1, Data 2, Data 3, Data 4};

Suggestion: use the second one to be more intuitive and improve the readability of the code for the above four definition methods
. Example:

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}
	};
	//方式3
	//数据类型 数组名[行数][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr3[2][3] = {
    
     1,2,3,4,5,6 }; 
	//方式4 
	//数据类型 数组名[][列数] = { 数据1,数据2 ,数据3,数据4  };
	int arr4[][3] = {
    
     1,2,3,4,5,6 };
	system("pause");
	return 0;
}

Summary: When defining a two-dimensional array, if the data is initialized, the number of lines can be omitted.
5.3.2 Two-dimensional array array name
View the memory space occupied by the two-dimensional array Example
of obtaining the first address of the two-dimensional array

:

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[1] << endl;
	cout << "二维数组第一个元素地址:" << &arr[0][0] << endl;
	cout << "二维数组第二个元素地址:" << &arr[0][1] << endl;
	system("pause");
	return 0;
}

**Summary 1: The name of the two-dimensional array is the first address of the array.
Summary 2: When sizeof is performed on the name of the two-dimensional array, the size of the memory space occupied by the entire two-dimensional array can be
obtained
. students (Zhang San, Li Si, Wang Wu), the scores in one test are as follows, please output the total scores of the three students

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 << names[i] << "同学总成绩为: " << sum << endl;
	}
	system("pause");
	return 0;
}

6 functions

6.1 Overview

Function: Encapsulate a piece of frequently used code to reduce repeated code
. A large program is generally divided into several program blocks, and each module implements a specific function.

6.2 Definition of functions

The definition of a function generally has 5 steps:
1. Return value type
2. Function name
3. Parameter list
4. Function body statement
5. Return expression
Syntax:

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

Return type: A function can return a value. In the function definition
Function name: Give the function a name
Parameter list: When using the function, the data passed
in Function body statement: The code inside the curly braces, the statement that needs to be executed in the function
Return expression: Linked to the return value type, After the function is executed, the corresponding data is returned.
Example: define an addition function to add two numbers

//函数定义
int add(int num1, int num2)
{
    
    
	int sum = num1 + num2;
	return sum;
}

6.3 Function call

Function: Use the defined function
Syntax: Function name (parameter)
Example:

//函数定义
int add(int num1, int num2) //定义中的num1,num2称为形式参数,简称形参
{
    
    
	int sum = num1 + num2;
	return sum;
}
int main() 
{
    
    
	int a = 10;
	int b = 10;
	//调用add函数
	int sum = add(a, b);//调用时的a,b称为实际参数,简称实参
	cout << "sum = " << sum << endl;
	a = 100;
	b = 100;
	sum = add(a, b);
	cout << "sum = " << sum << endl;
	system("pause");
	return 0;
}

Summary: The parentheses in the function definition are called formal parameters, and the parameters passed in when the function is called are called actual parameters

6.4 Passing by value

The so-called value transfer means that the actual parameter transfers the value to the formal parameter when the function is called.
When the value is passed, if the formal parameter occurs, it will not affect the actual parameter.
Example:

void swap(int num1, int num2)
{
    
    
	cout << "交换前:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	int temp = num1;
	num1 = num2;
	num2 = temp;
	cout << "交换后:" << endl;
	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	//return ; 当函数声明时候,不需要返回值,可以不写return
}
int main() 
{
    
    
	int a = 10;
	int b = 20;
	swap(a, b);
	cout << "mian中的 a = " << a << endl;
	cout << "mian中的 b = " << b << endl;
	system("pause");
	return 0;
}

Summary: When passing by value, formal parameters cannot modify actual parameters

6.5 Common Styles for Functions

There are 4 common function styles:
no parameter and no return
, with parameter and no return,
without parameter and return,
with parameter and return

example:

//函数常见样式
//1、 无参无返
void test01()
{
    
    
	//void a = 10; //无类型不可以创建变量,原因无法分配内存
	cout << "this is test01" << endl;
	//test01(); 函数调用
}
//2、 有参无返
void test02(int a)
{
    
    
	cout << "this is test02" << endl;
	cout << "a = " << a << endl;
}
//3、无参有返
int test03()
{
    
    
	cout << "this is test03 " << endl;
	return 10;
}
//4、有参有返
int test04(int a, int b)
{
    
    
	cout << "this is test04 " << endl;
	int sum = a + b;
	return sum;
}

6.6 Declaration of functions

Function: Tell the compiler the name of the function and how to call the function. The actual body of the function can be defined separately.
A function can be declared multiple times, but a function can only be defined once
Example:

//声明可以多次,定义只能一次
//声明
int max(int a, int b);
int max(int a, int b);
//定义
int max(int a, int b)
{
    
    
	return a > b ? a : b;
}
int main() 
{
    
    
	int a = 100;
	int b = 200;
	cout << max(a, b) << endl;
	system("pause");
	return 0;
}

6.7 Writing functions in separate files

Function: Make the code structure clearer
There are generally 4 steps to write functions in separate files
Create a header file with the suffix .h
Create a source file with the suffix .cpp
Write the function declaration in the header file
Write the function definition in the source file

Example:

**//swap.h文件
#include<iostream>
using namespace std;
//实现两个数字交换的函数声明
void swap(int a, int b);
**
//swap.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;
}
//main函数文件
#include "swap.h"
int main() 
{
    
    
	int a = 100;
	int b = 200;
	swap(a, b);
	system("pause");
	return 0;
}

Guess you like

Origin blog.csdn.net/fjj2397194209/article/details/131303447