C Language Study Notes (8) -- Array

Source: Internet
Author: User
Tags array definition

1 Array

 

In the process of program design, we often need to process a batch of data of the same type. It is obviously inconvenient to use basic type definition variables for storage. This is what we need to use arrays.

 

2 array features

 

1. the array size must be determined and cannot be changed randomly.

 

2. Elements of the array must be of the same type, and mixed types are not allowed.

 

3. One-dimensional array

 

3.1 Definition of a one-dimensional array:

 

Type specifier array name [constant expression];

 

For example:

 

Int a [10]

 

Pay attention to the following issues in the Array Life:

 

1. The naming rules of arrays are the same as those of variables;

 

2. Constant expressions represent the number of array elements, that is, the length of the array;

 

3. The subscript of the array starts from 0, and the subscript of the last element is the length of the array minus 1;

 

4. The definition of arrays can be declared and defined together with common variables.

 

Float I, j [];

 

3.2 reference of a one-dimensional array: array name [subscript expression];

 

Example: int a [5]; a [3] = 10;

 

The subscript expression can be any type of expression, but the value must be non-negative integer.

 

The memory space allocated by the array is a continuous space, from the low address to the high address.

 

Sample Code:

 

 

<Span style = "font-size: 18px;"> # include "stdio. h"

Main (){

Float score [10];

Int I;

Printf ("please input scores: \ n ");

For (I = 0; I <10; I ++)/* The subscript of the array is 0 ~ 9 */

Scanf ("% f", & score [I]);/* input each array element in sequence */

Printf ("\ n ");

For (I = 0; I <10; I ++)

Printf ("the % d score % f \ n", I + 1, score [I]);

} </Span>

When an array element is accessed at a time, loops are often used. During the loop, special attention should be paid to the subscript and the number of cycles of the array to prevent the subscript from exceeding the threshold.

 

3.3 initialization of a one-dimensional array: array name [array length] = {array element value };

 

For example, the following statement:

Int a [3] = {1, 2, 3 };

 

Note:

 

1. The definition is to assign an initial value to all elements of the array, for example:

 

Float f [3] = {1, 2, 3 };

 

2. You can assign only some values:

 

Float f [3] = {1, 2 };

 

In this way, the program automatically assigns values to the array elements from low to high, and the remaining element values are 0.

 

3. When assigning values to all elements of an array, you can leave the length of the array unspecified, but [] cannot be omitted:

 

Int a [] = {1, 2, 3 };

 

4. In particular, the array initialization assignment method can only be used to assign values when the array is defined, and can only assign values one by one after the definition. For example, the following is an error:

Int a [4];

 

A = {1, 2, 4}

 

Sample Code:

 

 

<Span style = "font-size: 18px;"> # include "stdio. h"

Main (){

Int I;

Int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10};/* all initialization */

Int B [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};/* Number of elements that can be omitted during initialization */

Int c [10] = {1, 2, 3, 4, 5};/* Partial initialization. The number of other elements is 0, and the length of the array cannot be omitted */

Int d [10];/* if no initial value is assigned, the value of the array element is not fixed. The length of the same Array cannot be omitted */

For (I = 0; I <10; I ++)

Printf ("% d", a [I]);

Printf ("\ n ");

For (I = 0; I <10; I ++)

Printf ("% d", B [I]);

Printf ("\ n ");

For (I = 0; I <10; I ++)

Printf ("% d", c [I]);

Printf ("\ n ");

For (I = 0; I <10; I ++)

Printf ("% d", d [I]);

Printf ("\ n ");

} </Span>

3.4 use of one-dimensional arrays

 

Save the scores of 30 students, view the number of students in each score segment, calculate the excellence rate and pass rate, and output the scores of all students, and mark the failure.

 

Code:

 

<Span style = "font-size: 18px;"> # define N 30

# Include "stdio. h"

Main (){

Float score [N];

Int count [4] = {0, 0, 0 };

Int I;

Float rate1, rate2;

For (I = 0; I <N; I ++) {/* enter the score and calculate the score level */

Scanf ("% f", & score [I]);

If (score [I]> = 85 & score [I] <= 100)

Count [0] ++;

Else if (score [I]> = 70)

Count [1] ++;

Else if (score [I]> = 60)

Count [2] ++;

Else

Count [3] ++;

}

Rate1 = (float) count [0]/N;/* calculate the excellence rate and pass rate. Pay attention to force type conversion */

Rate2 = (float) (N-count [3])/N;

Printf ("the number 85-100% d \ n", count [0]);/* number of output segments */

Printf ("the number 70-84% d \ n", count [1]);

Printf ("the number over 60% d \ n", count [2]);

Printf ("the number under 60% d \ n", count [3]);

Printf ("the good rate %. 2f, the fail rate %. 2f \ n", rate1, rate2);/* output excellence rate and pass rate */

For (I = 0; I <N; I ++) {/* output score and fail prompt */

If (score [I] <60)

Printf ("% d fail, socreis %. 1f \ n", I + 1, score [I]);/* the difference between the student ID and the corresponding score subscript is 1 */

Else

Printf ("% d score is %. 1f \ n", I + 1, score [I]);

} </Span>

Think exercises:

 

Process 20 Fibonacci series;

 

Returns the maximum value of an array;

 

Bubble Sorting;

 

Select sorting;

 

Binary Search:

 

4. Two-dimensional array

 

4.1 two-dimensional array definition:

 

Data Type array name [constant expression 1] [constant expression 2];

 

Example: int [30] [4];

 

4.2 references to two-dimensional arrays:

 

Array name [row subscript] [column subscript];

 

Example code of two-dimensional array input and output:

 

 

<Span style = "font-size: 18px;"> # include "stdio. h"

# Define M 3

# Define N 4

Main (){

Int I, j, a [M] [N];

For (I = 0; I <M; I ++)/* External Loop Control line */

For (j = 0; j <N; j ++)/* inner loop control column */

Scanf ("% d", & a [I] [j]);

Printf ("array a is: \ n ");

For (I = 0; I <M; I ++ ){

For (j = 0; j <N; j ++)

Printf ("% d", a [I] [j]);

Printf ("\ n ");

}

} </Span>

4.3 initialize a two-dimensional array

 

1. You can write all the data in a braces and separate them with commas (,). assign values to two-dimensional arrays in the order of array elements in the memory:

 

Int a [2] [3] = {1, 2, 3, 4, 5, 6}

 

2. you can also assign values to each element of the array, and write each row of the Two-dimensional array in a curly brackets: int a [2] [3] = {1, 2, 3}, {4, 5, 6 }};

 

3. You can also assign a value to the array: int a [2] [3] = {1, 2, 4}; equivalent:

 

Int [2] [3] = {1, 2, 3}, {4 }};

 

4. Like a one-dimensional array, If you assign values to all elements of the array, you can not specify the length of the first dimension, but the length of the second-dimensional array must be specified.

 

Two-dimensional array initialization example code:

 

 

<Span style = "font-size: 18px;"> # include "stdio. h"

Main (){

Int a [] [3] = {1, 2, 3, 4, 5, 6};/* The first dimension can be omitted during initialization */

Int B [2] [3] = {1, 2, 3}, {4, 5, 6};/* cannot be written as int B [2] [3] = {1, 2, 3} {4, 5, 6}; format */

Int c [3] [3] = {1}, {2}, {3 }};

Int d [3] [3] = {2}, {0}, {4 }}; /* When the element in the second row is null, the value 0 in {} and {} cannot be omitted */

Int e [3] [3] = {0, 1}, {0, 3}, {4};/* when assigning values to other values in a column, the preceding 0 */cannot be omitted */

Int I, j;

Printf ("output array a: \ n");/* branch outputs the values of each array */

For (I = 0; I <2; I ++ ){

For (j = 0; j <3; j ++)

Printf ("% d", a [I] [j]);

Printf ("\ n ");

}

Printf ("output array B: \ n ");

For (I = 0; I <2; I ++ ){

For (j = 0; j <3; j ++)

Printf ("% d", B [I] [j]);

Printf ("\ n ");

}

Printf ("output array c: \ n ");

For (I = 0; I <3; I ++ ){

For (j = 0; j <3; j ++)

Printf ("% d", c [I] [j]);

Printf ("\ n ");

}

Printf ("output array d: \ n ");

For (I = 0; I <3; I ++ ){

For (j = 0; j <3; j ++)

Printf ("% d", d [I] [j]);

Printf ("\ n ");

}

Printf ("output array e: \ n ");

For (I = 0; I <3; I ++ ){

For (j = 0; j <3; j ++)

Printf ("% d", e [I] [j]);

Printf ("\ n ");

}

} </Span>

4.4 2D array exercises

 

1. output the elements of a two-dimensional array in matrix form.

 

2. matrix conversion and row-column interchange.

 

5. Complete array exercises

 

1. Judge the background.

 

From letthinking's column

Related Article

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.