Article Directory
One, the array
The so-called array is a collection in which data elements of the same type are stored
Features:
-
Each data element in the array isThe same data type
-
The array is made up ofContiguous memory locationconsist of
-
We can passSubscriptTo access the elements in the array, the subscript of the array element starts from 0
1. One-dimensional array
1.数据类型 数组名[ 数组长度 ];
2.数据类型 数组名[ 数组长度 ]={
值1,值2...};
3.数据类型 数组名[ ] = {
值1,值2...};
One-dimensional array name purpose:
-
Count the length of the entire array in memory: sizeof (array name)
-
Get the first address of the array in memory: cout<<array name<<endl;
Example: View the length of the array and the first address of the array
#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;
}
Output result
Bubble Sort
The most commonly used sorting algorithm to sort the elements in the array
practice:
-
Compare adjacent elements, if the first one is greater than the second, swap them two
-
Do the same work for each pair of adjacent elements, and find the first maximum value after execution
-
Repeat the above steps, the number of comparisons -1 each time, until there is no need to compare
The total number of sorting rounds = the number of elements-1 The number of
comparisons per round = the number of elements-the number of sorting rounds-1
Case analysis
Given a set of data {4,2,8,0,5,7,1,3,9 }, sort them in ascending order according to the bubble sort method
Example
#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;
}
Output result
(2) Two-dimensional array
1.数据类型 数组名[ 行数 ] [ 列数 ];
2.数据类型 数组名[ 行数 ] [ 列数 ] = {
{
数据1,数据2},{
数据3,数据4} };
3.数据类型 数组名[ 行数 ] [ 列数 ] = {
数据1,数据2,数据3,数据4};
4.数据类型 数组名[ ] [ 列数 ] = {
数据1,数据2,数据3,数据4};
The purpose of the two-dimensional array name:
-
View the memory occupied by a two-dimensional array
-
Get the first address of a two-dimensional array
Example
#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;
}
Output result
Second, the function
Encapsulate a piece of frequently used code to reduce repetitive code
A larger program is generally divided into several program blocks, and each module realizes a specific function
1. The definition of the function:
-
Return value type
-
Function name
-
parameter list
-
Function body statement
-
return expression
返回值类型 函数名(参数列表)
{
函数体语句
return表达式
}
2. Function call
函数名(参数);
The parentheses in the function definition are called formal parameters, and the parameters passed in when the function is called are called actual parameters.
Example
#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;
}
Output result
3. Value transfer
The so-called value transfer is that the actual parameter passes 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
If the function does not need to return a value, you can write void when declaring it
Example
#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;
}
Output result
4. Common styles of functions
-
No participation, no return
-
Participation without return
-
Return without participation
-
Participate and return
Example
#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;
}
Output result
5. Function declaration
Tell the compiler the name of the function and how to call the function, the actual body of the function can be defined separately
The function can be declared multiple times, but the function can only be defined once
Example
#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;
}
Output result
6. Sub-file writing of functions
There are generally 4 steps to write a function file:
-
Create a header file with the extension .h
-
Create a source file with a suffix of .cpp
-
Write the function declaration in the header file
-
Write the definition of the function in the source file
Header file.h
#include<iostream>
using namespace std;
void swap(int a, int b);
Function.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;
}
The main program calls .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;
}
[Note] The learning course is-Dark Horse Program C++ Tutorial