C language 04 Arrays

Source: Internet
Author: User

Array: A set of data that consists of members of the same data type.

An array is a constructed type, a new data type that consists of the same data type.

Definition of a one-dimensional array:

Type specifier array name [constant expression]= {value 1, value 2 ...};

int a[5] = {2, 3, 4};

int b[5] = {0};

int c[]={2, 4, 6, 8};

Char str[] = "IPhone5";

Access to array elements: The ordinal number of an array element in an array, starting with 0,

Subscript can be a constant, or it can be a variable a[0], a[i];

Note differences define arrays and array elements

int a[10];

a[10];

1. Define an integer array with 20 elements, and the range of values for each element is

Between 30-70, the array element is evaluated.

int a[20] = {};

int sum = 0;

for (int i = 0; i<20; i++) {

int random= arc4random ()% (70-30+1) +30;

A[i] = random;

Sum=sum+a[i];

}

printf ("sum:%d", sum);

3, to generate 2 arrays, each array has 10 elements, the element value range 20-40, the array corresponding elements added, put in another? An array.

int a[10]={0};

int b[10]={0};

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

int r= arc4random ()% (40-20+1) +20;

A[i]=r;

B[i]=r;

printf ("a[%d]+b[%d]=%d \ n", I,i,a[i]+b[i]);

}

Note: The system does not detect whether the subscript of an array element is out of bounds, and you must ensure that the array subscript cannot be crossed when programming.

An array is a whole and cannot be directly involved in an operation, only a single element can be processed, and a loop is used where the array is usually used.

Number of rent sort

Bubble sort is a more common sort algorithm;

The bubble sort involves the double loop, the outer loop controls the number of times, and the inner loop controls the number of comparisons;

1, random production of 10 [20,40] number, and 10 numbers from the small to the big sort.

int a[10]={0};

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

int r=arc4random ()% (40-20+1) +20;

A[i]=r;

}

for (int i=0; i<9; i++) {

for (int j = 0; j< 9; j + +) {

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

int TEMP=A[J];

A[J]=A[J+1];

A[j+1]=temp;

}

}

}

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

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

}

Character array

An array for storing character variables, belonging to a one-dimensional array, defined in the same way as a normal one-dimensional array, except that the type is char

The definition of a character array:

Char c[10] = {' I ', ' P ', ' h ', ' o ', ' n ', ' e '}; or char c[10]= "iphone";

An array of characters that have a ' \ s ' End identifier is also known as a string.

The system provides a number of string processing functions

Strlen () calculates the string length

strcpy () string copy

strcat () string concatenation

strcmp () string comparison

To find the number of spaces in a string:

For example: "I love IOS, I want an iphone5s" 6 spaces

int c= 0;

Char a[]= "I love IOS, I want an iphone5s";

for (int i = 0; i < strlen (a); i++) {

if (a[i]== ') {

c=c+1;

}

}

printf ("%d", c);

Reverse the string:

For example: "Afjnpue" is transformed into a string "EUPNJFA"

  (Note: Change the string of the body, not the reverse output)

Char a[]= "Afhnpue";

int Number=strlen (a);

for (int i = 0; i < NUMBER/2; i++) {//front and last number exchange

Char temp = a[i];

A[i]=a[number-1-i];

A[number-1-i]=temp;

}
printf ("%s", a);


System-enhanced string-handling functions are based on the
' + ' to determine if the string ends
Char str[]={' o ', ' K '};
Char str[]= "OK";


C language 04 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.