当前位置:网站首页>Learning diary 6 array
Learning diary 6 array
2022-07-20 08:34:00 【Astray King】
To define an array : A set of several variables with a certain order relationship , The data type of variables must be the same
Array classification :1. Array of values , A character array , Pointer array , Array of structs .
One dimensional array
1. An array with only one subscript :< data type >< Array name >【< Constant expression >】
2. The array name should conform to the naming rules of identifiers
3. The array name cannot be the same as other variables
4. Constants in square brackets indicate the number of array elements
5. The array name represents the first address of memory , Is the address constant
6. Allocate continuous memory to the array at compile time .
A reference to a one-dimensional array
1. Arrays must be referenced element by element , Instead of quoting as a whole
2. Pay attention to subscripts from 0 Start , The scope is 0-n-1.
One dimensional array initialization
1. For ordinary local arrays, do not initialize , The value of the element in the array is uncertain .
2. about static Decorated array , Not initialized , The element defaults to 0.
3. The global array is not initialized , The array element defaults to 0;
4. All initialization ,int a[]={1,2,3,4,5}; Note that it can only be written in one line, not newline .
5. Partial initialization , Other automatic assignment 0;
6. The elements of an array cannot be assigned as a whole , Only a single assignment can be made .
Multidimensional arrays
1. Definition : Have two or more subscripts , It's called multidimensional arrays example :a[10][10]
2.< data type >< Array name >[ Constant expression 1][ Constant expression 2]
Constant expression 1 Is the number of rows , expression 2 Is the number of columns , The number of rows can be omitted , But the second dimension column number cannot be omitted .
3. initialization :
Initialize by line , The initial value of each line is {} Cover up int a[2][3]={ {1,2,3},{4,5,6}};
Initialize in element order int a[2][3]={1,2,3,4,5,6};
4. Memory allocation : It's like a one-dimensional array , Priority by line , Memory is still continuous .
Character arrays and strings
1. Definition : Character array is an array whose element data type is character type .char c[] char c[][10];
2. initialization
Assign values character by character
Use string constants to assign values , Note that the end of the string has \0, So the actual memory needs to occupy an extra byte , Pay attention to the problem of cross-border .
3. Definition of string : Referring to ‘\0’ A group of characters as closing characters , When storing a string into an array , Also put the special Terminator '\0' Deposit in , As the end of this string .
4. Input and output of string
printf("%s") When outputting the contents of the character array, it will be output until '\0' character
scanf("%s") When the input , End in case of blank space or carriage return .
String handling functions
1. String copy function strcpy /strncpy
Format :strcpy( A character array 1, A character array 2) / strncpy( A character array 1, A character array 2,number)
function : The string 2 Copy to character array 1 in / Put the character array 2 Before n Copy to character array 1
Return value : Returns an array of characters 1 The first address
Be careful : A character array 1 It has to be big enough , Copy even '\0' Copy together
2. String concatenation function strcat / strncat
Format :strcat( A character array 1, A character array 2)/strnact( A character array 1, A character array 2,number)
function : Put the character array 2 Connect to character array 1 after / Put the character array 2 front n Connect to character array 1
Return value : A character array 1 The first address
Be careful : A character array 1 Big enough , Array 1 Of \0 Delete , Add \0
3. String comparison function strcmp / strncmp
Format :strcmp( character string 1, character string 2)/strncmp( character string 1, character string 2,number)
function : Compare strings from left to right 1 And string 2(ASCII value ) size , Until you encounter different characters / Compare the front from left to right n Character size
Return value := Return to 0 < Returns a negative number > Return as a positive integer
4. String length function strlen
Format strlen( Character array name )
function : Calculate the effective length of the string
Return value : Returns the actual length of the string , barring \0
When you translate \ Not included \xhh Express 16 Hexadecimal number ,\ddd Represents octal number
Practical example
1.#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char ch1[100]="hello";
char ch2[]="hewlo";
char ch3[]="\x69\144\n";
int a,b;
strcat(ch1,ch2);// String concatenation function
puts(ch1);
putchar('\n');
//strcpy(ch1,ch2);// String copy function
//puts(ch1);
// putchar('\n');
a=strcmp(ch1,ch2);// String comparison function
printf("%d\n",a);
b= strlen(ch3);// String effective length function
printf("%d\n",b);
return 0;
}
2.#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char arr1[100]={};
char arr2[]="hello brother";
strncpy(arr1,arr2,5);
puts(arr1);
putchar('\n');
return 0;
}
3.
// Calculate the top ten items of Fibonacci series and output the results in reverse order
// Fibonacci sequence , The latter element is the sum of the first two elements
#include <stdio.h>
int main(int argc, char *argv[])
{
int disc[10];
disc[0]=disc[1]=1;
int i;
for(i=2;i<10;i++)
{
disc[i]=disc[i-1]+disc[i-2];
}
for(i=9;i>=0;i--)
{
printf("%d ",disc[i]);
}
return 0;
}
4. bubble sort
#include <stdio.h>
#define N 10
int main(int argc, char *argv[])
{
int i,j,t;
int a[N];
printf("please input %d numbers\n",N);
for(i=0;i<N;i++)// Loop input elements
scanf("%d",&a[i]);
for(i=0;i<N-1;i++)// The first level of the cycle is a cyclic sort N-1 Time
for(j=0;j<N-1-i;j++)// The second cycle , Compare sizes and swap elements , Every time you compare, the number of cycles will be reduced by one
{
if(a[j]>a[j+1])// If the current element is larger than the next one, exchange elements
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
for(i=0;i<N;i++)// Loop out array elements
printf("%5d",a[i]);
printf("\n");
return 0;
}
5. Choose the sorting method
#include <stdio.h>
#define N 10
int main(int argc, char *argv[])
{
int a[N],i,j,r,t;
printf("please input %d numbers\n",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]);
for(i=0;i<N-1;i++)// Through N-2 Second comparison
{
r=i;
for(j=i+1;j<N;j++)// If the latter is smaller than the former , Will R and J Exchange , Then cycle and compare with the next bit
{
if(a[j]<a[r])
r=j;
}
if(r!=i)// If R Not equal to I It means that the smallest one is the current number R position , Then connect him with I Bit exchange
{
t=a[r];
a[r]=a[i];
a[i]=t;
}
}
printf(" From childhood, the output is :\n");
for(i=0;i<N;i++)
printf("%5d",a[i]);// Cyclic output
printf("\n");
return 0;
}
6. Print the first ten lines of Yang Hui triangle
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[10][10]={ {0}};
int i,j;
for(i=0;i<10;i++)
{
a[i][0]=1;// Cycle to assign all the first column 1
for(j=1;j<=i;j++)// Control the number printed as triangle
a[i][j]=a[i-1][j-1]+a[i-1][j];// Find the rule and get the expression
}
for(i=0;i<10;i++)
{
for(j=0;j<=i;j++)
printf("%-8d",a[i][j]);//- Will make the number followed by a space
printf("\n");
}
return 0;
}
7. Enter a string , Then output it in reverse order
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* char arr[100]={0};// If not initialized , Random values will be assigned later , Result in output garbled code
int i,n;
printf("please input word\n");
gets(arr);
n=sizeof(arr)/sizeof(char);
// n=strlen(arr);
for(i=n-1;i>=0;i--)// Change the output order to achieve reverse output
putchar(arr[i]);
putchar('\n');
return 0;*/
char arr[100]={0},ch;// Exchange positions to achieve reverse order output
int i,j,n;
printf("please input a string:");
gets(arr);
i=0;
n=strlen(arr);
j=n-1;
while(i<j)
{
ch=arr[i];
arr[i]=arr[j];
arr[j]=ch;
i++;
j--;
}
puts(arr);
return 0;
}
8. Change the uppercase in the input string to lowercase , Lowercase to uppercase
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char arr[10]={0};
int n,i;
fgets(arr,sizeof(arr),stdin);// There is enough space to save the Enter key
n=strlen(arr);
for(i=0;i<n;i++)
{
if(arr[i]>='A'&&arr[i]<='Z')
arr[i]=arr[i]+32;
else if(arr[i]>='a'&&arr[i]<='z')
arr[i]=arr[i]-32;
}
puts(arr);
return 0;
}
9. Find the maximum number in the array
#include <stdio.h>
int main(int argc, char *argv[])
{
int max,i;
int a[10]={0};
printf("please input 10 number\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
for(i=1;i<10;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
printf("max=%d\n",max);
return 0;
}
10. Input 10 An integer is stored in a one-dimensional array , Statistics output the positive number , The number of negative numbers and zeros
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[10],i;
int j=0;
int k=0;
int m=0;
printf("please input 10 number\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
{
if(a[i]==0)
{
j++;
}
else if(a[i]>0)
{
k++;
}
else
{
m++;
}
}
printf(" A positive number is %d\n The number of negative numbers is %d\n The number of zeros is %d\n",k,m,j);
return 0;
}
11. Input 10 An integer is stored in a one-dimensional array , Then store in reverse order and output
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[10],i,t;
printf("please input 10 number\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
t=a[i];
a[i]=a[9-i];
a[9-i]=t;
}
for(i=0;i<10;i++)
printf("%d ",a[i]);
return 0;
}
边栏推荐
- yolov3的GUI界面(简易,图片检测)
- 第五十八篇:VS调试出现“覆盖。。。。是/N:否/A:全部)??”
- Yolov5 realizes smoking behavior detection
- Basic introduction to multithreading (with sample code)
- 学习日记3-数据的输入输出
- 第七十四篇:机器学习优化方法及超参数设置综述
- 利用函数指针数组实现计算器编写
- Pytorch implements retinanet (III) definition and training of loss
- Pointer in C language (learning experience)
- C language program environment and preprocessing
猜你喜欢
随机推荐
区间覆盖问题
更易上手的C语言入门级芝士 (2) 选择语句+循环语句、函数、数组、操作符(超详细)
Yolov5 bird detection
C语言实现基础版扫雷
YOLOv5苹果香蕉检测
Custom type: structure, bit segment, enumeration, union
Pytorch implements retinanet (III) definition and training of loss
Heap sorting and heap related operations
Yolov5 realizes smoking behavior detection
Yolov5 détection des oiseaux
字符串函数和内存操作函数
4000字,让你明白递推及其例题做法(C语言,有图)
堆堆排序及堆的相关操作
更易上手的C语言入门级芝士 (3) 常见关键字+define+指针+结构体(超详细)
C语言结构体类型
Pytorch target detection data processing (II) extracting difficult samples, low AP samples
c语言自定义类型:结构体、枚举、联合
枚举(enum)奇妙的使用、联合体(共用体union)对空间节省的巧妙
三子棋(N子棋)C语言编程实现,超详细讲解
YOLOv5实现吸烟行为检测