2015.1.27
Array: A contiguous memory space in memory
int score[22];
1. Data types, data types of array elements
2. The array name is actually an address constant, which is the first address of an array element
3. The length of the array, representing the number of array elements
4. Size of memory space occupied by array = length of array * data type size
Array subscript
SCORE[0] ~ score[21]
Array subscript starting from 0 to n-1
The elements in the array belong to variables
Traverse
int main (int argc, const char * argv[]) {
int a[10];
for (int i=0; i<10; i++) {
scanf ("%d", &a[i]);
// }
//
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
printf ("\ n");
//
return 0;
//}
Array out-of-bounds access
Arrays cannot be accessed across borders
int main (int argc,const char *argv[])
//{
int a[10];
//
for (int i=0; i<10; i++) {
scanf ("%d", &a[i]);
// }
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
//
return 0;
//}
Array Reverse Access
//
int main (int argc,const char *argv[])
//{
int a[10];
for (int i=9; i>=0; i--) {
scanf ("%d", &a[i]);
// }
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
printf ("\ n");
return 0;
//}
Array name is address constant
int main (int argc,const char *argv[])
//{
int a[10];
printf ("%p\n", a);//int [10]
printf ("%p\n", &a[0]);//int *
printf ("%p\n", &a[1]);
//
return 0;
//}
How to initialize an array
1. Full initialization
int main (int argc,const char *argv[])
//{
int a[10]={1,2,3,4,5,6,7,8,9,10};
//
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
return 0;
//}
Partial initialization
int main (int argc,const char *argv[])
//{
int a[10]={0,0,1,2,3};
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
printf ("\ n");
return 0;
//}
Method initialized to 0
int main (int argc,const char *argv[])
//{
int a[10]={};
for (int i=0; i<10; i++) {
printf ("%d", a[i]);
// }
//
return 0;
//}
Character array initialization
int main (int argc, const char *argv[])
//{
Char ch[10]={' A ', ' B ', ' C '};
for (int i=0; i<10; i++) {
printf ("%c", Ch[i]);
// }
printf ("\ n");
return 0;
//}
int main (int argc,const char *argv[])
//{
Char str[11]= "HelloWorld";
//
printf ("%s\n", str);
//
return 0;
//}
To ask for a student's average score
int main (int argc,const char *argv[])
//{
int score[10]={};
int sum=0;
for (int i=0; i<10; i++) {
scanf ("%d", &score[i]);
// }
for (int i=0; i<10; i++) {
Sum+=score[i];
// }
printf ("%d\n", SUM/10);
//
return 0;
//}
Fibonacci sequence
Outputs the first 20 items of the Fibonacci sequence
1 1 2 3 5 8 13 21 34 55 89 ....
A[n]=a[n-1]+a[n-2]
int main (int argc,const char *argv[])
//{
int a[20]={1,1};
for (int i=2; i<20; i++) {
A[I]=A[I-1]+A[I-2];
// }
for (int i=0; i<20; i++) {
printf ("%d", a[i]);
// }
Putchar (' \ n ');
return 0;
//}
Enter 10 data to find the maximum number in the data and output it to the screen
#define LEN 10
int main (int argc,const char *argv[])
//{
int a[len]={};
for (int i=0; i<len; i++) {
scanf ("%d", &a[i]);
// }
int max = a[0];
for (int i=1; i<len; i++) {
if (Max<a[i]) {
max = A[i];
// }
// }
printf ("max =%d\n", max);
return 0;
//}
Find a value
int main (int argc,const char *argv[])
{
int a[len]={};
int Val;
for (int i=0; i<len; i++) {
scanf ("%d", &a[i]);
}
scanf ("%d", &val);
for (int i=0; i<len; i++) {
if (val = = A[i]) {
printf ("%d", I);
}
}
printf ("\ n");
return 0;
}
ios-c_day7___ Array Basics