C++ Elementary Tutorial (3)

1. One-dimensional array of C++

C++ supports array data structures, which can store a fixed-size sequential collection of elements of the same type. An array is used to store a series of data, but it is often thought of as a series of variables of the same type.

The array declaration is not to declare individual variables, but to declare an array variable, such as numbers, and then use numbers[0], numbers[1], ..., numbers[99] to represent individual variables. Specific elements in an array can be accessed by index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element, and the highest address corresponds to the last element.

The values ​​stored in the array are called array elements , the number of elements stored in the array is the length of the array , and the elements in the array need to be accessed through indexes ( subscripts ).

1. Declare an array

To declare an array in C++, you need to specify the type of elements and the number of elements, as follows:

数组类型 数组名(数组长度)

The above declaration syntax is used to declare a one-dimensional array ( a one-dimensional array can be understood as a row of data in an Excel table ). The length of the array must be an integer constant greater than zero, but you can also declare the initial length of the array as 0, which means that the array is empty, but it has no practical meaning, so this is not recommended (the length of the array cannot be negative ) . The data type can be any valid C++ data type. The naming rules of array names are the same as the naming rules of variables. For example, to declare an array number of type int containing 10 elements , the declaration statement is as follows:

int number[10];

2. Initialize the array

Initialize the array, that is, assign values ​​to the array elements. In C++, arrays can be initialized individually, or with a single initialization statement, as follows:

//声明数组时初始化
int a[5]={
    
    1,2,3,4,5};
int b[]={
    
    1,2,3,4,5};
//先声明数组,再初始化数组
int n[5];
//通过下标访问数组元素
n[0]=0;
n[1]=1;
n[2]=2;
n[3]=3;
n[4]=4;

At initialization, the number of values ​​between braces { } cannot be greater than the number of elements we specified in square brackets [ ] when we declared the array.

The law of array index (subscript): the subscript of the array starts from 0 and increases in turn, and the maximum value is the length of the array -1 .

3. Array exercises

(1) Enter n numbers from the keyboard and save them in the array, and use a loop to output all elements of the array, separated by spaces.

Implementation code:

#include<iostream>
using namespace std;

int main()
{
    
    
	//定义变量n表示数组长度
	int n;
	//输入n的值
	cout<<"请输入需要保存元素的数量:";
	cin>>n;
	//定义数组,长度为n
	int arr[n];
	//初始化数组
	for(int i=0;i<n;i++){
    
    
		cout<<"请输入第"<<i+1<<"个数:";
		cin>>arr[i];
		cout<<endl;
	}
	//打印输出所有元素
	cout<<"数组中的元素为:";
	for(int i=0;i<n;i++){
    
    
		cout<<arr[i]<<" ";
	} 

 } 

(2) Input n integers from the keyboard, calculate the sum of these numbers, and output the maximum value among these input numbers.

Problem-solving ideas: save the input numbers in a one-dimensional array, calculate the sum of all elements of the array and find the maximum value.

#include<iostream>
using namespace std;

int main()
{
    
    
	//定义变量n表示整数的个数 
	int n;
	//定义变量sum保存整数的和,初值为0
	int sum=0; 
	//输入n的值
	cout<<"请输入整数的个数:";
	cin>>n;
	//定义数组,长度为n,保存输入的数字 
	int arr[n];
	//输入整数,保存到数组中
	for(int i=0;i<n;i++){
    
    
		cout<<"请输入第"<<i+1<<"个数:";
		cin>>arr[i];
		cout<<endl;
	}
	
	//定义变量max表示最大值,初值为arr[0]
	int max=arr[0]; 
	//遍历数组,计算总和并寻找最大值 
	for(int i=0;i<n;i++){
    
    
		//将数组元素一次相加,即为求和
		sum += arr[i]; 
		//寻找最大值:以max表示最大值,如果数组元素比max大,则表示这个元素时最大值,
		//将元素值赋值给max,当循环结束,max中保存的一定是最大值。 
		if(arr[i]>max){
    
    
			max=arr[i];
		}
		 
	} 
	//打印输出结果
	cout<<"你输入的整数总和为:"<<sum<<",最大值为:"<<max; 
 } 

(3) Print the first n items of the Fibonacci sequence: input an integer n from the keyboard, save the first n items of the Fibonacci sequence into an array, and output all elements in the array.

Hint: The Fibonacci sequence is 1 1 2 3 5 8 13 21 34 55 89…

Implementation code:

#include<iostream>
using namespace std;

int main()
{
    
    
	//定义变量n 
	int n;
	//输入n的值
	cout<<"请输入n:";
	cin>>n;
	//定义数组,长度为n
    //防止数据溢出,使用long long类型的数组
	long long arr[n];
	//计算斐波拉契数列前n项,保存到数组中
	//斐波拉契数列第一项和第二项都为1,从第三项开始,每项的值=前两项之和
	//先保存第一项、第二项
	arr[0]=1;
	arr[1]=1;
	//使用循环计算第三项之后的值 
	for(int i=2;i<n;i++){
    
    
		//下标i表示当前项,i-1表示前第一项,i-2表示前第两项 
		arr[i]=arr[i-1]+arr[i-2]; 
	}
	
	cout<<"斐波拉契数列的前"<<n<<"项为:";
	//遍历数组,输出所有元素 
	for(int i=0;i<n;i++){
    
    
		cout<<arr[i]<<" ";  
	} 
 } 

2. Two-dimensional array in C++

A two-dimensional array is equivalent to an Excel table, consisting of rows and columns, and each row is a one-dimensional array. Therefore, a two-dimensional array can be regarded as a collection of multiple one-dimensional arrays. For example:

1234561 a[0] a[1] a[2] a[3] a[4] a[5]2 b[0] b[1] b[2] b[3] b[4] b[5]2 c[0] c[1] c[2] c[3] c[4] c[5]3 d[0] d[1] d[2] d[3] d[4] d[5]4 e[0] e[1] e[2] e[3] e[4] e[5]
以上便是一个二维数组,这个二维数组有5行(5个一维数组)6列(每个一维数组的长度都为6

According to the above content, if you want to represent the element c[3], it can be expressed as: (row 3, column 4), which is similar to the coordinates in mathematics, and it needs to be represented by rows and columns. For example, to represent the elements of the third row and fourth column of array n, it should be written as:

n[2][3]

Note: Whether it is a multi-dimensional array, the subscript of the element starts from 0.

1. Declare an array

A two-dimensional array is declared as:

数据类型 数组名[行数][列数];

For example:

//声明一个3行4列的整型数组
int num[3][4];
//声明一个5行5列的字符数组
char ch[5][5];

2. Initialize the two-dimensional array

The initialization of a two-dimensional array is similar to that of a one-dimensional array, for example:

int n[2][3]={
    
    
{
    
    1,2,3},
{
    
    4,5,6}
}

or:

int n[][2]={
    
    
{
    
    1,2},
{
    
    3,4},
{
    
    5,6}
}

or:

int n[2][2]={
    
    1,2,3,4}

or:

int n[][2]={
    
    1,2,3,4,5,6}

Note: When the two-dimensional array is initialized, the number of rows can be omitted, but the number of columns must be written.

Enter the value of a two-dimensional array from the keyboard:

#include<iostream>
using namespace std;

int main()
{
    
    
	//声明二维数组
    int num[2][3];
	//初始化二维数组需要使用双层循环,外层用于控制行数,内层用于控制列数
	for(int i=0;i<2;i++){
    
    //二维数组的行 
		for(int j=0;j<3;j++){
    
    //二维数组的列 
			//数组的元素使用[行号][列号]表示
			cin>>num[i][j];
		}
	}
	//输出数组元素时,同样使用双层循环
	for(int i=0;i<2;i++){
    
    
		for(int j=0;j<3;j++){
    
    
			cout<<num[i][j]<<" ";
		}
	} 
 } 

3. Practice questions

(1) Define and assign a two-dimensional array with 4 rows and 4 columns, exchange the data of the first row and the fourth row, and output.

#include<iostream>
using namespace std;

int main()
{
    
    
	//定义一个4行4列的数组,交换第一行和第四行的值 
	int num[4][4];
	//定义一维数组,用于交换值
	int temp[4]; 
	int n=1; 
	//初始化二维数组,赋值为1-16 
	for(int i=0;i<4;i++){
    
    //二维数组的行 
		for(int j=0;j<4;j++){
    
    //二维数组的列 
			//数组的元素
			num[i][j]=n++;
		}
	}
	//输出数组元素时,同样使用双层循环
	cout<<"交换前:\n"; 
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			cout<<num[i][j]<<"\t";
		}
		cout<<endl;
	} 
	//交换 
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(i==0){
    
    
				temp[j]=num[0][j];
				num[0][j]=num[3][j];
				num[3][j]=temp[j];
			}
//			if(i==3){
    
    
//				num[3][j]=temp[j];
//			}
		}
	} 
	//输出数组元素时,同样使用双层循环
	cout<<"交换后:\n";
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			cout<<num[i][j]<<"\t";
		}
		cout<<endl;
	} 
 } 

(2) Calculate the sum of elements on the diagonal of a 4X4 matrix.

#include<iostream>
using namespace std;

int main()
{
    
    
	//计算4行4列矩阵两条对角线上元素的和
	/*
		1   2   3   4
		5   6   7   8
		9  10  11  12
		13 14  15  16
	*/
	//定义二维数组 
	int num[4][4];
	int n=1;
	//定义变量sum用于存放和 
	int sum=0; 
	//初始化二维数组,赋值为1-16 
	for(int i=0;i<4;i++){
    
    //二维数组的行 
		for(int j=0;j<4;j++){
    
    //二维数组的列 
			//数组的元素
			num[i][j]=n++;
		}
	}
	//打印矩阵
	cout<<"矩阵为:\n"; 
	for(int i=0;i<4;i++){
    
    
		cout<<"\t";
		for(int j=0;j<4;j++){
    
    
			cout<<num[i][j]<<"\t";
		}
		cout<<endl;
	} 
	cout<<"\n对角线上的元素为:\n"; 
	//计算和:对角线上的元素有一个共同特点,一条对角线上的元素行号和列号相等,另一条上的元素行+列=3 
	for(int i=0;i<4;i++){
    
    
		for(int j=0;j<4;j++){
    
    
			if(i==j||i+j==3){
    
    
				sum+=num[i][j];
				cout<<num[i][j]<<"\t";
			}
		}
	} 
	//输出总和
	cout<<"\n元素总和为:"<<sum; 
 } 

operation result:

矩阵为:
	1	2	3	4
	5	6	7	8
	9	10	11	12
	13	14	15	16
对角线上的元素为:
1	4	6	7	10	11	13	16
总和为:68

(3) Input a two-dimensional array of n*n, find the maximum value in each row and store it in a one-dimensional array and output

#include<iostream>
using namespace std;

int main()
{
    
    
	//输入一个n*n的二维数组,找出每行中的最大值并存到一维数组中,输出 
	//定义变量n 
	int n;
	cout<<"请输入长度n:";
	cin>>n; 
	//定义二维数组 
	int num[n][n];
	//定义一维数组
	int max[n]; 
	//定义变量temp
	int temp;
	int key=1; 
	//初始化二维数组,赋值 
	for(int i=0;i<n;i++){
    
    //二维数组的行 
		for(int j=0;j<n;j++){
    
    //二维数组的列 
			//数组的元素
			num[i][j]=key++;
		}
	}
	//打印数组
	cout<<"生成的数组为:\n"; 
	for(int i=0;i<n;i++){
    
    
		cout<<"\t";
		for(int j=0;j<n;j++){
    
    
			cout<<num[i][j]<<"\t";
		}
		cout<<endl;
	} 
	//找每一行中的最大值 
	for(int i=0;i<n;i++){
    
    
		//假设本行的第一个元素为最大值 
		temp=num[i][0];
		for(int j=0;j<n;j++){
    
    
			//如果有元素值大于最大值,则改变最大值 
			if(num[i][j]>temp){
    
    
				temp=num[i][j];
			}
		}
		//内层循环结束,表示比较完了一行数据,得到该行的最大值,存入一维数组中 
		max[i]=temp;
	} 
	//输出数组中的所有元素 
	for(int i=0;i<n;i++){
    
    
		cout<<"第"<<i+1<<"行最大值为:"<<max[i]<<endl;
	}
 } 

Guess you like

Origin blog.csdn.net/qq_43884946/article/details/129814852