Embedded C language difficulty one: arrays

Source: Internet
Author: User

1. Definition of arrays

Consists of several arrays of the same type that have sequential relationships, and each variable in the array is called the element of the array.

Expression:< Storage type > < data type > < array name > [< constant expression;]

2. References to one-bit arrays

Representation method: Array name [subscript]

Columns: Define 10 arrays and reverse output

#include <stdio.h>

int main () {

/* Array definition, with 10 elements */

Int I,A[10];

A[0]=a[1]=1;

for (i=2;i<10;i++) {

/* Subscript is an integer expression with a label range of 0-9*/

A[I]=A[I-2]+A[I--1];

printf ("Fibonacci numbers...\n");

for (i=9;i>=0;i--) {

printf ("a[%d] is%d\n", i,a[i]);

return 0;

}

}

}

Output Result:

A[9] is 55

A[8] is 34

A[7] is 21

A[6] is 13

A[5] is 8

A[4] is 5

A[3] is 3

A[2] is 2

A[1] is 1

A[0] is 1

Array subscript is starting from 0, the range is 0 to n-1, where you are the number of elements, through the subscript can easily access the elements in the array.

Remember that when referencing an array of subscripts, the subscript cannot be crossed.

3. Initialization of a local array

(1) The local array is not initialized.

For a normal local array, if it is not initialized, the value of the element in the array is indeterminate.

(2) Static array is not initialized.

If you define a static array without initialization, the elements in the array default to 0.

(3) Global array is not initialized.

As above, if not initialized, it defaults to 0.

(4) Initialize all.

int a[10]={1,2,3,4,5,6,7,8,9,10};

(5) Partial initialization.

int a[10]={1,2,9,23,5};

In order only the first five values are assigned, and the last five are automatically assigned as 0.

(6) The array is assigned all values.

If all is assigned, the constants in the array subscript can be omitted, and the compiler automatically calculates the length of the array elements based on the list.

Int a[]={1,2,9,56,12,3,8,46,7,13,};

Note that "[]" cannot be omitted at this time.

(7) The array is all initialized to 0.

For this particular case, there are a number of instances

Int Main () {

int a[10]={0};

return 0;

}

There are also these kinds of, columns:

Int Main () {

int a[10],i;

for (i=0;i<10;i++) {

a[i]=0;

return 0;

}

}

#include <stdio.h>

int main () {

int a[10];

memset (A,0,sizeof (a));

return 0;

}

1. Bubble sort

(1) Compare the first number with the second number, if it is in reverse order a[0]>a[1], then the second number and the third number, and then compare to the number of n-1 and nth number, the first trip bubble sort, finally, the largest number is placed in the last element position.

(2) The first n-1 number of the second trip bubble sort, and finally, the second largest number is placed in the position of the n-1 element.

(3) Repeat the above process, after a total of n-1 bubble sort, sort end.

Code City Column:

#include <stdio.h>

#define N 10

int main () {

int a[n],i,j,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++) {

for (j=0;j<n-1-i;j++) {

if (A[j]>a[j+1]]) {

T=A[J];

A[j]=a[j=1];

a[j+1]=t;

}

}

printf ("The array after sort;\n");

for (i=0;i<n;i++)

printf ("%5d", A[i]);

printf ("\ n");}

return 0;

}

Output Result:

Please input 0 numbers

5,12,6,8,78,45,32,14,11,9

The array after sort:

5,6,8,9,11,12,14,32,45,78

Embedded C language difficulty one: arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.