当前位置:网站首页>C language learning day_ 04
C language learning day_ 04
2022-06-27 09:56:00 【Bad bad -5】
List of articles
- Learning reference B Stand on the video of teacher Hao bin , The source code in the article can be contacted by private letter if necessary .
Array
- effect
- In order to solve the problem of storing and using a large amount of the same type of data
- For large amounts of the same type of data , There is no need to redefine the variable name
- To simulate the real world
- One dimensional array :
a[x]A straight line - Two dimensional array :
a[x][y]The plane - Three dimensional array :
a[x][y][z]Space - Four bit array :
a[x][y][z][h]Space + Time
- One dimensional array :
- In order to solve the problem of storing and using a large amount of the same type of data
- Format
aIs a variable name.nExpress n A variable- The first is
a[0], The last one isa[n-1]
a[n] = {
1, 2, ...,n};
example :
/* Array */
# include <stdio.h>
int main(void)
{
int a[5] = {
1, 2, 3, 4, 5}; //a Represents the array name ,5 Indicates that there are five elements
int i;
for (i = 0; i < 5; ++i)
{
printf("%d\n", a[i]);
}
return 0;
}
a[5] = {1, 2, 3, 4, 5}a Represents the name of the array ,5 Indicates that there are five elements , Each item isa[0]、a[1]...a[4]
/* Running results */
1
2
3
4
5
Press any key to continue
One dimensional array
- Format :
int a[3];- It means that there is a total of 3 Elements
- 3 The names of the elements are
a[0]、a[1]、a[2]
- Definition :
- by n Continuous allocation of storage space for variables
- All variables must have the same data type
- All variables must have the same byte size
/* Wrong writing 1*/
int a[5];
a[5] = {
1, 2, 3, 4, 5};
- Only when you define an array can you assign a value as a whole
- After definition
a[5]It is not an array , It's the first... Of the array 6 term- Array
a[5]in , Onlya[0]-a[4]term , No,a[5]term
example :a Copy the values of the array to b Array
/* Wrong writing 2*/
b = a; //a、b Represents the address of the first element in the array
// Write it correctly
for (i = 0; i < 5; ++i)
b[i] = a[i];
Array operation
- initialization
- assignment
- Sort
- Ask for the biggest / Small value
- The horse
- lookup
- Insert
- Delete
initialization
- Fully initialized
int a[5] = {1, 2, 3, 4, 5};
- Incomplete initialization
int a[5] = {1, 2, 3};- Uninitialized elements are automatically 0
- Not initialized
int a[5];- All elements are garbage values
- Zero clearing
int a[5] = 0- All elements are 0
assignment
/* Array assignment */
# include <stdio.h>
int main(void)
{
int a[5];
int i;
printf(" Please enter a[0] Value :");
scanf("%d", &a[0]);
printf("a[0] = %d\n", a[0]);
printf(" Please enter a[3] Value :");
scanf("%d", &a[3]);
printf("a[3] = %d\n", a[3]);
printf("---------------\n");
for (i = 0; i < 5; ++i)
printf("a[%d] = %d\n", i, a[i]); // Loop through the values of the array items
return 0;
}
/* Running results */
Please enter a[0] Value : 1
a[0] = 1
Please enter a[3] Value :3
a[3] = 3
---------------
a[0] = 1
a[1] = -858993460 // Items that are not initialized are filled with garbage data
a[2] = -858993460
a[3] = 3
a[4] = -858993460
Press any key to continue
The horse
example : Invert the elements of an array to output
/* Invert the elements of an array to output */
# include <stdio.h>
int main(void)
{
int a[5] = {
1, 2, 3, 4, 5};
int i, j;
int t; // For storing temporary data
i = 0;
j = 4;
while (i < j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
++i; // The global value of the variable self increasing or self decreasing is not used , therefore i++ and ++i You can use
--j;
}
printf(" The inverted array is output as :");
for (i = 0; i < 5; ++i)
printf("%d, ", a[i]);
printf("\n");
return 0;
}
/* analysis */
i = 0
j = 4
0 < 4 establish , Into the loop
t = a0 = 1
a0 = a4 = 5
a4 = t = 1
i = 1
j = 3
1 < 3 establish , Into the loop
t = a1 = 2
a1 = a3 = 4
a3 = t = 2
i = 2
j = 2
2 < 2 Don't set up , Exit loop
a[5] ={
5, 4, 3, 2, 1} //a[2] Value does not change
/* Running results */
The inverted array is output as :5, 4, 3, 2, 1,
Press any key to continue
- Test whether even numbers still hold
Two dimensional array
- Format :
int a[3][4];- It means that there is a total of 12 Elements , Can be viewed as 3 That's ok 4 Look at
- 12 The names of the elements are :
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
a[i][j]It means the first onei + 1That's ok , The firstj + 1Column
2D array initialization
- The two ways are equivalent
/* 2D array initialization */
int a[3][4] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
int a[3][4] = {
{
1, 2, 3, 4},
{
5, 6, 7, 8},
{
9, 10, 11, 12}
};
Two dimensional array operation
- Output to two-dimensional array
- Sort 2D arrays
- Find the maximum value of each line
- Judge whether the matrix is symmetrical
- Multiplication of matrices
/* Two dimensional array representation and output */
# include <stdio.h>
int main(void)
{
int a[3][4] = {
{
1, 2, 3, 4},
{
5, 6, 7, 8},
{
9, 10, 11, 12}
};
// You can also enter a two-dimensional array as follows
//int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int i,j;
for (i = 0; i < 3; ++i)
{
printf(" The first %d That's ok :", i + 1);
for (j = 0; j < 4; ++j)
{
printf("%-3d", a[i][j]); //-3d% Indicates left alignment , Intermediate proportion 3 The position of the cursor
}
printf("\n");
}
return 0;
}
/* Running results */
The first 1 That's ok :1 2 3 4
The first 2 That's ok :5 6 7 8
The first 3 That's ok :9 10 11 12
Press any key to continue
Whether there is a multidimensional array
- There is no multidimensional array
- Because memory is linear and one-dimensional
nThe dimension group can be regarded asn - 1One dimensional array of dimension groupsint a[3][4];- The array contains 3 One dimensional array of elements
- Each one-dimensional array can also contain 4 Elements
【 Article source code reference GitHub】
All of the above are original , If unknown or wrong , Please point out .
边栏推荐
- Reorganize common shell scripts for operation and maintenance frontline work
- R language plot visualization: plot to visualize the two-dimensional histogram contour map, add numerical labels on the contour lines, customize the label font color, and set the mouse hover display e
- Source insight 工具安装及使用方法
- 【面经】云泽科技
- Decompile the jar package and recompile it into a jar package after modification
- unity--newtonsoft.json解析
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- Installation and use of SVN version controller
- QT运行显示 This application failed to start because it could not find or load the Qt platform plugin
- Video file too large? Use ffmpeg to compress it losslessly
猜你喜欢
随机推荐
Arduino PROGMEM静态存储区的使用介绍
新旧两个界面对比
Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03
Google browser chropath plug-in
R language uses econcharts package to create microeconomic or macro-economic charts, demand function to visualize demand curve, and customize the parameters of demand function to enrich the visualizat
Reading and writing Apache poi
R语言使用caret包的preProcess函数进行数据预处理:对所有的数据列进行center中心化(每个数据列减去平均值)、设置method参数为center
On June 23, the video address of station B in the third episode of rust chat room
Advanced mathematics Chapter 7 differential equations
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
Scientists develop two new methods to provide stronger security protection for intelligent devices
Comparison between new and old interfaces
R語言plotly可視化:可視化多個數據集歸一化直方圖(historgram)並在直方圖中添加密度曲線kde、設置不同的直方圖使用不同的分箱大小(bin size)、在直方圖的底部邊緣添加邊緣軸須圖
Quick start CherryPy (1)
使用Aspose.cells将Excel转成PDF
Une compréhension facile de la simplicité de la classification bayésienne du lissage laplacien
Quelques exercices sur les arbres binaires
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Quartz (timer)
Only one confirmcallback is supported by each rabbittemplate








