当前位置:网站首页>Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
Detailed explanation of typical application code of C language array - master enters by mistake (step-by-step code explanation)
2022-07-29 05:37:00 【Believe in youth】
Preface :
This article is self-study C Learning code summarized by oneself in the language process , Gradually elaborate on the instructions , Suitable for learning zero basic programming 、 Foundation to consolidate memories , Not suitable for masters to learn , The purpose of sending this is also to exchange and learn C Language experience , All say C Language is the basic language of all languages , By knowing one method you will know all , I believe the topic that countless leaders in front have proved ,C Language is the cornerstone of all programming languages . The learning of programming will not be stingy with the knowledge points summarized by staying up late , I hope it can help you who are reading .
Catalog
1. One dimensional array definition :
2. Detailed code explanation case :
2.1 Detailed code case + explain
2.2 Typical cases bubble sort detailed code + Explain
2.3 Explanation of typical application code — recursive :
2.4 Typical application code + Explain in detail :
3. matters needing attention :
1. One dimensional array definition :
stay C The use of arrays in languages must be defined first .
One dimensional arrays are defined as : Type specifier Array name [ Constant expression ].
among , A type specifier is any basic or constructive data type . The array name is a user-defined array identifier .
The constant expression in square brackets represents the number of data elements , Also known as the length of an array .
1.1 Basic code cases :
int a[10]; /* Description integer array a, Yes 10 Elements */
float b[10], c[20]; /* Explain real arrays b, Yes 10 Elements , Real array c, Yes 20 Elements */
char ch[20]; /* Description character array ch, Yes 20 Elements */2. Detailed code explanation case :
#include <stdio.h>
#include <stdlib.h>
/* Array :
1. Arrays have the same data type , And the address is continuous —— For storage needs
2. Arrays are ordered —— Convenient for us to use ( Storage is stored in sequence )
3. The number of subscripts of array elements is called dimension . According to different dimensions , Can be divided into one-dimensional arrays 、 Two dimensional array 、 Multidimensional arrays
[] A few brackets have several dimensions
Definition :
One dimensional array
1)
Definition : Type character Array name [ Constant expression ]
Type character — Specify what kind of data the array stores
Array name — It can be similarly understood as variable name
Constant expression — Specify the number of array elements
2) Array names are similar to variable names , All need to follow the identifier rules
When defining an array, we need to specify the number of elements in the array , That is, numbers are required in square brackets
When using an array, it can be a variable , It can also be a constant
Elements in an array can be used like variables
Don't cross the bounds when using arrays ( Array subscripts and constants are equal , There are as many elements as there are , Subscript from 0 Start )
*/
// Define an integer one bit array , The subscript of array element is from 0 Start
/*
problem :int a[10],a[0]—a[9] Assign values in sequence 0-9, How to realize this method ?
int main()
{
int a[10];
int i;
for(i=0;i<10;i++){
a[i] = i;
printf(" %d\n",a[i]);
}
for(i=9;i>=0;i--){
a[i] = i;
printf(" %d\n",a[i]);
}
return 0;
}
*/
/*
1) Assign values while defining a one-dimensional array , It's called array initialization
2) If the global array is not initialized , By default, the system considers it to be assigned a value 0
3) When assigning initial values to all array elements , You can not specify the array length
4) Local initialization , front 5 Each element has its initial value , The following elements are assigned a value of 0
Initialization of one dimensional array :
initialization : That is, assign initial values while defining Such as : int a=[10]={1,2,3,4,5,6,7,8,9,10}
*/
//1. All arrays are initialized ( Global initialization )
int main(){
int a[10]= {1,2,3,4,5,6,7,8,9,10};
int i;
for(i=0;i<10;i++){
printf("%d \n",a[i]);
}
}// Please correct , thank you !
2.1 Detailed code case + explain
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*
// problem :
1) Given a one-dimensional array , Find the maximum value in the array .
// Solution :“ The arena ”
Martial arts competition for marriage :1 People stand on the challenge arena —— Compare the rest ,
2) Flip the array
1 2 3 4 5 6 7
7 6 5 4 3 2 1
3) How to sort arrays
int a[6] {5,7,9,10,3,1} 1,3,5,7,9,10
*/
/*
//1. Define a one-dimensional array , At the same time, you need to initialize
int a[10]={7,4,9,8,5,34,28,45,56,97};
//2. Determine the champion , Given the initial maximum
int max=a[0];
//3. Loop traversal , Compare the values of each element in turn ,
// If the element value ratio max It's worth a lot , be max The value of is updated to this number , Otherwise it doesn't change .
int i;
for(i=0;i<10;i++)
{
if(a[i]>max){
max=a[i];
}
}
printf(" The maximum value is :%d\n",max);
*/
// Example 2 : Fallible method : Use reverse order output ( Not an option ):
/*
int a[6] = {1,2,3,4,5,6};
// Array considerations :a[] Brackets indicate that there are several elements ,
// You need to define the value later , Otherwise, it will be assigned , from “0” Start counting the first number
int i;
for(i=5;i>=0;i--){
printf("%d ",a[i]);// Output :6 5 4 3 2 1
}
printf("\n");
for(i=0;i<6;i++)
{
printf("%d ",a[i]);// Output :1 2 3 4 5 6
}
// This method only changes the order of output values , It didn't happen 1 To 6 Reversal of values between values
*/
// Example 2 correct solution : The flip of the array can be realized through the exchange of elements
//1 and 6 in ,2 and 5 in ,3 and 4 in ,
//1 2 3 4 5 6
//6 5 4 3 2 1
//a[0] and a[n-1] in a
/*
sizeof() Is a memory capacity measurement function , The function is to return the size of a variable or type ( In bytes );
stay C In language ,sizeof() It is an operator to judge the data type or expression length .
*/
/*
int a[6]= {1,2,3,4,5,6};
int i=0;
int tmp;// Use an idle variable to exchange
int j=sizeof(a)/sizeof(int)-1;// The elements in the computer are from 0 Starting number ,0 It's the first element
while(i<j){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
for(i=0;i<6;i++)
{
printf("%d ",a[i]);
}
printf("\n");
*/
/*
//3) How to sort arrays
int a[6] {5,7,9,10,3,1} 1,3,5,7,9,10
// Using bubble sort
*/
int a[6] = {5,7,9,10,3,1};
/*
// Bubble sorting process , Compare the size several times
int a[6] = {5,7,9,10,3,1};
1 5=n-1 5 7 9 3 1 10
2 4=n-2 5 7 3 1 9 10
3 3=n-3 5 3 1 7 9 10
4 2=n-2 3 1 5 7 9 10
5 5=n-1 1 3 5 7 9 10
*/
int i;// Number of sortings
int j;// Compare the values of two adjacent numbers
int tmp;// Define an exchange variable for temporary Exchange
int n = sizeof(a)/sizeof(int);
for(i=0;i<n;i++){
for(j=0;j<n-1-i;j++)
// Exchange of elements ( The premise is that two adjacent numbers , The former is bigger than the latter )
{
if(a[j]>a[j+1]){
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
// Output results
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
// If there is any mistake, please correct it , thank you .2.2 Typical cases bubble sort detailed code + Explain
#include<stdio.h>
void main()
{
int n[10] = { 25,35,68,79,21,13,98,7,16,62 };// Define a size as 10 Array of
int i, j,k,temp;
for (i = 1; i <= 9; i++)// The outer cycle is the number of rounds compared , There are... In the array 10 Number , Then it should be more 10-1=9 round
{
for (j = 0; j <= 9 - i; j++)// The inner loop compares the number of comparisons in the current round , for example : The first round of comparison 9-1=8 Time , The second round of comparison 9-2=7 Time
{
if (n[j] > n[j + 1])// If two adjacent numbers are in reverse order , Then switch places
{
temp = n[j];
n[j] = n[j + 1];
n[j + 1] = temp;
}
}
printf(" The first %d The data sorting after the sorting is completed :\n",i);
for (k = 0;k < 10; k++)
printf("%-4d", n[i]);
printf("\n");
}
printf(" The order of numbers after sorting :\n");
for (i = 0; i < 10; i++)
printf("%-4d", n[i]);
printf("\n");
}
// Please correct 2.3 Explanation of typical application code — recursive :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void PrintN(int N)
{
if (N)
{
PrintN(N-1);
printf("%d\n",N);
}
return;
}
void main()
{
int N;
clock_t start,finish;
double duration;
printf("please input N:\n");
scanf("%d",&N);
start = clock();
PrintN(N);
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf("%f seconds\n",duration );
return;
}
// Please correct 2.4 Typical application code + Explain in detail :
#include <stdio.h>
#include <stdlib.h>
/*
One dimensional array initialization
initialization : Is to assign initial values while defining , Such as
int a[10] = {1,2,3,4,5,6,7,8,9,10}
If you don't assign a value, it will be assigned automatically
The array name of a one-dimensional array
1. Array name is a constant
2. Array name is the first address of an array element
3.sizeof( Array name ) In fact, the total size of the array is calculated
problem :
1) Given an array , Find the maximum value of the array
resolvent : Given an initial value , Compare the size
Definition :i and max To compare
int a[10] = {10,20,90,31,32,44,55,66,10,23};
int max = a[0];
int i;
for(i=0;i<10;i++){
if(a[i]>max){
max = a[i];
}
}
printf(" The larger number in the array is : %d\n",max);
problem 2 : Realize array flip 、
Use the exchange method to realize the element position exchange in the array , Array flip .
*/
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int i=0;
int tmp;
int j = sizeof(a)/sizeof(int)-1;
while (i<j){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
for(i=0;i<10;i++){
printf("%d ",a[i]);
}
return 0;
}
If you read , Like the author's article , Please comment , Will continue to update .
Refined programming development partner .
3. matters needing attention :
For the array type description, the following points should be noted :
- The type of array actually refers to the value type of array elements .
- For the same array , All data elements of the same type .
- The writing rules of array name should conform to the writing rules of identifier .
- Array names cannot be the same as other variable names .
C Language learning begins with arrays and becomes more and more difficult , Good programmers = talent + Practice hard
Less lofty pursuit , More down-to-earth persistence .
边栏推荐
- B - identify floating point constant problems
- ClickHouse学习(一)ClickHouse?
- 【C语言系列】—深度解剖数据在内存中的存储(二)-浮点型
- Longest string without duplicate characters
- 微信小程序-组件传参,状态管理
- Alibaba cloud architect details nine trends in the game industry
- With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
- Clickhouse learning (VI) grammar optimization
- 阿里云架构师细说游戏行业九大趋势
- 移动端-flex项目属性
猜你喜欢

【C语言系列】—深度解剖数据在内存中的存储(二)-浮点型

三次握手四次挥手针对面试总结

Detailed explanation of serial port communication
![[C language series] - realize the exchange of two numbers without creating the third variable](/img/7c/468000bcbf740c9dd3535f2734728a.png)
[C language series] - realize the exchange of two numbers without creating the third variable

shell基本操作(上)

【JS题解】牛客网JS篇1-10题

Together with digital people, digital space and XR platform, Alibaba cloud and its partners jointly build a "new vision"

【C语言系列】— 不创造第三个变量,实现两个数的交换

Alibaba cloud and Dingjie software released the cloud digital factory solution to realize the localized deployment of cloud MES system

Clickhouse learning (V) cluster operation
随机推荐
Best practices for elastic computing in the game industry
[C language series] - print prime numbers between 100 and 200
Solution: find the position of the first and last element in a sorted array (personal notes)
Li Kou 994: rotten orange (BFS)
Side effects and sequence points
ClickHouse学习(十一)clickhouseAPI操作
uniapp之常用提示弹框
虚拟增强与现实第二篇 (我是一只火鸟)
Global components component registration
Provincial and urban three-level linkage (simple and perfect)
[sword finger offer] - explain the library function ATOI and simulate the realization of ATOI function
Clickhouse learning (IX) Clickhouse integrating MySQL
Longest string without duplicate characters
Clickhouse learning (V) cluster operation
Thrift安装手册
微信小程序视频上传组件直接上传至阿里云OSS
rem与px与em异同点
【无标题】
B - identify floating point constant problems
Day 3