October Today morning to see the parade passion, but in the afternoon or to continue to write C language, in front of this very simple
int number[100];
scanf("%d" , &number[i]);
First, the definition of an array
- <Type> variable name [number of elements]
- The number of elements must be an integer
Array Features
- All elements have the same data type
- Once created, the size can not be changed
- (Memory elements in the array are ordered in this continuous)
Second, the array example
Write a program, an unspecified number of input [0,9] in the range of integer count the number of times each number appears, enter -1 for the end
Arrays are usually used the following steps:
#include <stdio.h>
int main(void)
{
// 数组的大小
const int number = 10;
int x;
// 定义数组
int count[number];
int i;
// 初始化数组
for (i = 0; i < number; i++)
{
count[i] = 0;
}
scanf("%d" , &x);
while( x != -1){
if(x >= 0 && x <= 9){
// 数组参与运算
count[x] ++;
}
scanf("%d" , &x);
}
// 遍历数组输出
for (i = 0; i < number; i++)
{
printf("%d:%d\n", i , count[i]);
}
return 0;
}
Third, the array operation
Integrated array initialization
int a[] = {2,4,6,7,1,3,5,9,11,13,23,14,32};
Integrated positioning initialization
int a[10] = {[0] = 2 , [2] = 3,6};
int i;
for (i = 0; i < 10; ++i)
{
printf("%d\t", a[i]);
}
// 2 0 3 6 0 0 0 0 0 0
- With [n] is given in the initialized data positioned
- No data is positioned behind the contact position in front of
- Value of zero padding other locations
- May not be given array size, let the compiler count
- Particularly suitable for the initial data sparse array
Array size
- sizeof gives the entire contents of the array occupied by the size in bytes
sizeof(a)/sizeof(a[0]);
- sizeof (a [0]) gives the size of a single element of the array, then divided by the number of cells is obtained array
- Such code, once the initial array data to modify, without modifying the code traversal
Array-valued
- Array variable itself can not be assigned
- Put all the elements of an array to another array, you must traverse
Fourth, look for the array elements
When the array as a function parameter, then another parameter often must be passed in the array size
When the array as a parameter:
- Size of the array can not be given in []
- The number of elements can not be calculated using the array sizeof
#include <stdio.h>
int main(void)
{
int a[] = {2,4,6,7,1,3,5,9,11,13,23,14,32,};
int x;
int loc;
printf("请输入一个数字:\n");
scanf("%d" , &x);
loc = search(x, a, sizeof(a)/sizeof(a[0]));
if (loc != -1)
{
printf("%d在第%d个位置上\n", x , loc);
}else{
printf("%d不存在\n", 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;
}
Five, prime
Can be determined whether the number is divisible known and prime <x of
#include <stdio.h>
int main(void)
{
const int number = 10;
int prime[10] = {2};
int count = 1;
int i = 3;
while(count < number){
if (isPrime(i,prime,count))
{
prime[count++] = i;
}
// 进行调试
{
printf("i=%d \tcnt=%d\t", i , count );
int i;
for (i = 0; i < number; i++)
{
printf("%d\t", prime[i]);
}
printf("\n");
}
i++;
}
for ( i = 0; i < number; i++)
{
printf("%d", prime[i]);
if ( (i+1)%5)
{
printf("\t");
}else{
printf("\n");
}
}
return 0;
}
int isPrime(int x, int knownPrimes[], int numberofKnowPrimes)
{
int ret = 1;
int i;
for (i = 0; i <numberofKnowPrimes ; i++)
{
if ( x % knownPrimes[i] == 0)
{
ret = 0;
break;
}
}
return ret;
}
Six, two-dimensional array
int a[3][5]
// 通常可以理解为a是一个3行5列的矩阵
Traversing the two-dimensional array
for(i = 0; i<3; i++){
for(j = 0; j<5; j++){
a[i][j] = i * j;
}
}
// a[i][j]是一个int,表示第i行第j列上的单元
Two-dimensional array initialization
int a[][5] = {
{0,1,2,3,4,},
{2,3,4,5,6,},
};
- The number of columns is to be given, the number of rows may have to set compiler
- {} Per line, separated by commas
- The last comma can exist
- If omitted, showing zero padding