0%

C语言学习笔记之数组运算

Array Calculation

数组计算

Array Calculation

Intergrated initialization of arrays

1
int a[] = {2,4,5,6,7};
  • give the initial value of all elements of the array by directly using “{}”
  • don’t need to give how large the array is, because the compiler will help you count.

image-20221220221006157

locate Intergrated initialization

postscript: only C99

1
2
3
int a[10] = {
[0] = 2, [2] = 3, 6 ,
};
  • locate initial data by [n]
  • data have no located will attach it to the back of front
  • other location will assign zero(0)

image-20221220220930328

size of array

image-20221220221253372

array assignment

image-20221220221448348

image-20221220221620643

image-20221220221825875

full of source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
int search(int key, int a[], int length);

int main(void)
{
int a[] = {2,3,4,6,1,0,8};
int x;
int loc;
scanf("%d", &x);
loc = search(x, a, sizeof(a)/sizeof(a[0]));
if (loc != -1){
printf("%d在%d这里\n", x, loc);
} else {
printf("%d不存在", x);
}
return 0;
}
int search(int key, int a[], int length)
{
int ret = -1;
int i;
for (i = 0; i < length; i++)
{

if (a[i] == key) {
ret = i;
break;
}
}
return ret;
}

An example for Array Calculation

determine prime number

-------------本文结束感谢您的阅读-------------