C--(ix) arrays

Source: Internet
Author: User
Tags array definition float double

Array:

Here just give some guidance, the specific code can ask Niang, because you know it has this method, you can use it, check it, just like look up a dictionary

1. Requirements:

Save 1 Students ' height?

Save 10 students ' height?

Save 100 students ' height?

。。。。。

2. What is an array

1) An ordered set of data with the same data type as the storage unit contiguous .

2) A collection of multiple data objects in an array , known as elements in an array. Each element in the array is of the same data type, and they are uniquely determined with a uniform array name and subscript (the position of the element in the array ). .

3) An array is also a variable , before and after the storage unit is contiguous. You need to define the post reference first.

3. Why arrays are required

1) Arrays are in the program design , in order to deal with the convenience of the same type of a number of variables organized in an orderly form of a form

2) You can simply think of a simplified variable declaration operation

3) The declaration array can be declared as n variables at the same time , and these variables are in a contiguous space

4. Array declaration syntax

Data type array name [constant expression ];

1. Array is a collection declaration of the same type variable

2, each element inside the array is referenced by subscript

3.

Data type array name [expression ];

expression: Indicates the length of the array

int height[];

An integral type array is defined above.

? array name is height

? There are 10 integer variables in the array

Note: Subscript problem, subscript range C language does not check subscript out of bounds

"Related notes":

1. " data type " can be any data type . char long short int float double long Long

2. " array name " must be a valid identifier. numbers, Letters , _ Cannot start with numbers

3. "Constant expression " means the length of the array (that is, the number of elements in the array ) .

Be sure to enclose it in square brackets, and it is not allowed to contain variables.

4. Each element in a one-dimensional array has only one subscript, and the first element 's

The subscript is "0" and the last element is labeled "array length minus 1".

i.e.: 0<= subscript <= Expression -1

5. References to arrays

"Related Notes"

1> The value of subscript expression must be a constant of integral type

2> for the referenced array elements, the use method is the same as the simple data type variable you learned earlier, including taking address & and so on .

3>C language does not have a syntax structure that can reference all the elements of an array at the same time , and can only refer to each element of a group by one. (set declaration, single use)

6. Initialization of arrays

Initialize: The initial value is assigned. The C language allows you to initialize an array directly when you define it.

General form :

type specifier array name [constant expression ] = {value table };

"Related Notes":

When the number of values in the curly braces is less than the number of elements in the array definition , theC language assigns the data to the first few elements of the array , and the remaining

Array elements are automatically initialized to 0.

Think :1) Neither write length nor initialize can it?

==========================

Character Array

character array : Not only can store multiple character constants information, but also can be used to save string constant information

How to express a student's name, home address, ID card number and other information?

1. Basic concept and form of string

2, the difference between the string and the character array and the length of the string calculation

3. How strings are stored

4. Input and output of strings

Problems with array usage considerations

1. Subscript is out of bounds and may modify other data

2. The array declaration size must be constant, the size of the declaration is fixed , can not be changed

3.sizeof can find the entire array of memory space contiguous space memory

4. Arrays cannot be assigned directly to each other, they should be assigned using loops, centrally declared, single reference

5. How to enter a string with spaces

6. Memory space contiguous for all elements in the array

==========================

Common operations of arrays (add, delete, check, change, reverse, sort)

1. Adding basic operations to array elements

eg: How to add a newly added student's height information

2. Deleting array elements basic operations

eg: How to delete an invalid student's height information

3. Find the basic Operation of the specified information in the array

eg: How to find the maximum and minimum height data information and determine the corresponding subscript value of the data in the array

4. Basic operation of element modification in array

Eg: swaps the first and last elements in an array

5. Reverse Array

Eg: (1) reverse the output of all elements in an array

(2) Save all elements of the array in reverse Order

==========================

Array sorting:

1. Sorting Basics

Requirement : You now need to find the smallest number and swap it with the first element

Implementation method

1) Find the smallest element

2) swap with the first element

Practice :

Enter three numbers 15, 8, 10, the smallest number is the first, and the maximum number is the last, after the related operation of the array.

2. Select Sort

(1) Sorting principle

find the smallest one from all elements , place it first, then find the smallest one in the rest of the numbers, and so on, and so on, the sequence is gradually formed from the go.

(2) sorting implementation

How many loops does 1> need to solve the problem 2> How to compare

3. Bubble sort

1) Sorting principle

Compare the adjacent two numbers, the smaller one to the front, 22 after the first round, the largest number is placed in the last face;

Then repeat the above operation from the beginning, the second largest number is placed in the penultimate position, and so on, and so on, the sequence is gradually formed from the back.

2) Sorting implementation

How many loops does 1> need to solve the problem 2> How to compare

==================

Two-dimensional arrays:

Demand:

(1) Save a student's five results

float score[5];    

(2) Save five results for the class 70

float score[70][5]

Thinking:

(1) can a one-dimensional array be implemented?

(2) If you need to find a student's grade is easy to query?

1. Two-d array declaration syntax

one-dimensional arrays: Data type array name [expression];

Two-dimensional array: Data type array name [expression 1][expression 2];                                                   

Eg: declare a two-dimensional array of words, numbers, and English scores that can hold the class's results

Float Score[5][3] Define a two-dimensional array keep five students in three grades

number of words outside

STU1 80 85 90

STU2 90 85 93

STU3 87 90 93

STU4 80 92 87

STU5 82 88 91

2. The nature of two-dimensional arrays

1) There is no real two-dimensional array in the C language (memory is one-dimensional)

2) A two-dimensional array can be understood as a special one-dimensional array, that is, each element in a two-dimensional array is a one-dimensional array

3) Think: is the two-dimensional array memory space continuous?

Two-dimensional array in memory storage, the principle of storage is "to store", that is, the first row and then store the second row ...

3. references to two-dimensional arrays

Reference form: array name [subscript 1][subscript 2]

   

4. initialization of two-D arrays

Method 1: Assign the initial value directly. Applies when data is low.

all initial values of an array element are written in a curly brace, and the compilation system assigns an initial value to each element in the order in which the array elements are stored in memory.

Centralized initialization:

Two-dimensional array initial

Method 2: The branch assigns a value to the two-dimensional array. The number of initial values that apply to each row is less than the number of array elements in each row .

Partial initialization: //Two-d array column subscript cannot be omitted

"Related Notes":

The length of the two-dimensional array cannot be omitted arbitrarily, and must obey the following rules:

1) using the first method, the length of the first dimension of the defined array can be omitted only if the initial value is assigned to all elements of the two-dimensional array. The system can determine the length of the first dimension based on the number of initialized data and the length of the 2nd dimension.

For example:

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

Generally, when you omit the definition of the first dimension, the size of the first dimension is determined by the following rules:

The number of initial values can be divisible by the second dimension, the quotient is the size of the first dimension; if not divisible, the size of the first dimension is quotient plus 1.

for example, int a[][3]={1,2,3,4};

equivalent to: int a[2][3]={1,2,3,4};

2) using the second method, either assigning an initial value to all elements of a two-dimensional array, or assigning an initial value to a partial element of a two-dimensional array, the length of the first dimension can be omitted when the array is defined.

3) Regardless of which method is used to assign an initial value to a two-dimensional array element, the length of the second dimension must not be omitted when defining the array.

Think: Is it possible to traverse a two-dimensional array using a one-dimensional array?

4).

1> How to access an element in a two-dimensional array

2> How to access all elements in a two-dimensional array

C--(ix) 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.