Array definition and reference ZZ

Source: Internet
Author: User
Tags array definition
Lesson 1

Subject:Array definition and reference

Purpose:Understanding the definition and reference methods of Arrays

Teaching focus:Array initialization and array element reference methods

Teaching difficulties:Initialization, reference

Course content:

I. Why use arrays?

1. Benefits of using Arrays

It facilitates batch data processing.

2. When do I need to use arrays?

There is a large amount of data of the same type to be processed.

2. How to Use arrays?

1. Define an array first

The definition of a one-dimensional array is as follows:

Type specifier array name [constant expression]

For example, int A [10];/* an array named a with 10 integer elements */

If we process the scores of 42 students in a class, we can define an integer array with 50 elements: int maths [50];

After an array is defined in the program, the compiler allocates storage space for it in the memory:

Data element name Memory Address
Maths [0] 2000:0001
Maths [1] 2000:0002
The right table is the memory storage image ------->
...
Maths [49] 2000: 0099.
 
89
97
0
0
0
0

The array size in C is determined during definition. That is to say, only constant expressions can be used in.

Int A = 50;

Int maths [a];

The above definition is incorrect!

2. How to access array elements? (Array element reference)

Store data in an array or use the data in an array to reference only one element. That is to say, a statement can only process data in one array element. The elements in the array are referenced in the following format: array name [the position number of the element in the array]

For example, if we want to place the first student's score in the first position of the array, we will use the following MARK: Maths [0].

Example:

Main () <br/>{< br/> int maths [50];/* used to store class mathematics scores */<br/> maths [0] = 89; /* the score of the first student is 89, which is placed in the first position of the array */<br/> maths [1] = 97; /* Second student */<br/>}

Note: In C, the number of array elements starts from 0, so the first element number is 0, and the second element number is 1. Do not forget!

3. How to assign an initial value to an array element?

You can use a value assignment statement to assign an initial value to an array element, but it takes up the running time. If the value is assigned during definition, the group element has an initial value during compilation and can be directly used during runtime.

Example:

Main () <br/>{< br/> static int A [10] = {,};/* 10 elements, specify the initial values. */<Br/> static int B [10] = {0, 1, 2, 3 }; /* assign initial values to some elements of the array */<br/> static int C [10] = {0, 0, 0, 0, 0, 0, 0 }; /* all 0 */<br/> static int d [10]; /* Static static storage class automatically assigned the initial value 0 */<br/> static int e [] = {1, 2, 4, 4, 5};/* the array size is not specified during definition, assign several values to several elements. <br/> the number of elements in this array is 5 */<br/>}

3. check what the array can do.

There are 10 integers, which are, and.

Sort them in ascending order.

Main () <br/>{< br/> int A [11]; <br/> int I, j, T; <br/> printf ("input 10 numbers: \ n "); <br/> for (I = 1; I <11; I ++) <br/> scanf (" % d ", & A [I]); <br/> printf ("\ n"); <br/> for (j = 1; j <= 9; j ++) <br/> for (I = 1; I <= 10-j; I ++) <br/> if (a [I]> A [I + 1]) <br/> {T = A [I]; A [I] = A [I + 1]; A [I + 1] = T ;} <br/> printf ("the sorted numbers: \ n:"); <br/> for (I = 1; I <11; I ++) <br/> printf ("% d", a [I]); <br/>}< br/>/* --------------- <br/> results: <br/ > Input 10 numbers: <br/> 4 56 43 3 7 88 46 52 32 14 return! <Br/> the sorted numbers: </P> <p> */</P> <p>

4. Let me give it a try.

Write a program to calculate the average score of a course in a class, calculate the number of people in each segment, and output the ranking table.

5. Notes for using Arrays

1. General Format of array Definition

2. The starting value of the array element subscript is 0.

Vi. Features of two-dimensional arrays

If you think of a one-dimensional array as a table, the table can have several rows but only one column. If we think of a two-dimensional array as a table, the table can have several rows and each row can have several columns.

One-dimensional array a [6] Two-dimensional array B [6] [3]
A [0]
25
2
85
2
A [5]
B [0]
B [0] [0] 21 22
88 54 62
25 32 32
88 54 15
2 B [4] [1] 3
8 55 B [5] [2]
B [1]
B [2]
B [3]
B [4]
B [5]

We can regard a two-dimensional array as a one-dimensional array. Each element of this one-dimensional array is a one-dimensional array.

The storage sequence of elements in a two-dimensional array in the memory is Row-based, that is, elements in the first row are stored in sequence, and elements in the second row are stored.

7. Two-dimensional array Definition

The two-dimensional array is generally defined

Type specifier array name [constant expression] [constant expression]

Example:

Int class [50] [7];/* A total of 50 rows and 7 columns. We can use it to store scores of several courses for all students in a class. */

8. Access elements of a two-dimensional array

Access to a two-dimensional array is also based on elements. to reference an element, you must specify its row and column number: array name [row subscript] [column subscript]

Example:

Main () <br/>{< br/> int class [50] [7];/* each row has a student score, the first column contains the mathematical score */<br/> int sum = 0; <br/> class [0] [0] = 89; /* the first student's mathematical score is 89 */<br/> class [1] [0] = 98; <br/> sum = Class [0] [0] + class [1] [0]; <br/> printf ("% d", sum ); <br/>}< br/>

9. Two-dimensional array Initialization

Two-dimensional array initialization example:

Main () <br/>{< br/> static int A [3] [4] ={{ 1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11,12 }}; </P> <p> static int B [3] [4] = {1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; </P> <p> static int C [3] [4] = {1}, {5}, {9 }}; </P> <p> static int d [3] [4] = {1}, {0, 6}, {0, 11 }}; </P> <p> static int e [3] [4] ={{ 1}, {5, 6 }}; </P> <p> static int f [3] [4] ={{ 1}, {}, {9 }}; </P> <p> static int G [] [4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; </P> <p> static int H [] [4] ={{ 1 },{}, {9 }}; <br/>}

The above initialization method is correct. Note the use of curly brackets and the definition of the number of rows in the brackets.

10. Look at the procedure using a two-dimensional array.

There is a matrix of 3*4. You need to compile the program to find the value of the element with the largest value in the period and its row number and column number.

Main () <br/> {int I, j, row, Colum, Max; <br/> static int A [3] [4] = {1, 2, 3, 4 }, {9, 8, 7, 6}, {-10, 10,-5, 2 };< br/> max = A [0] [0]; <br/> for (I = 0; I <= 2; I ++) <br/> for (j = 0; j <= 3; j ++) <br/> if (a [I] [J]> MAX) <br/> {max = A [I] [J]; <br/> ROW = 1; <br/> Colum = J; <br/>}< br/> printf ("max = % d, row = % d, Colum = % d \ n", Max, row, Colum); <br/>}< br/>/* ------- <br/> results: <br/> max = 10, row = 2, colum = 1 <br/> */

11. Let me give it a try.

The two-dimensional array is used to store the scores of multiple courses for the students in the class. Then, the average score of each student and the average score of each course are obtained.

12. Notes

1. Initialization

2. The meaning of two-dimensional array columns.

13. Special array: character array

An array used to store character data is a character array. An element in the character array contains one character. Character arrays are usually used to store strings.

14. Definition and reference of character Arrays

Define character array:

Char C [10];

Initialize character array:

Static char C [10] = {'I', '', 'A', 'M','', 'h', 'A', 'P ', 'P', 'y '};

If the number of initial values provided in curly braces is greater than the array length, an error occurs. If the length is smaller than the array length, the remaining elements are automatically set as spaces.

You can also assign an initial value to a two-dimensional character array using the same method as an integer two-dimensional array. Note that character constants are enclosed in single quotes.

Character array reference example:

Main () <br/> {static char C [10] = {'I', '', 'A', 'M','', 'h ', 'A', 'P', 'P', 'y'}; <br/> int I; <br/> for (I = 0; I <10; I ++) <br/> printf ("% C", C [I]); <br/> printf ("\ n "); <br/>}< br/>/* -------- <br/> results: <br/> I am happy <br/> */

15. What is a string?

A string is a finite sequence of several characters.

16. Basic processing of strings: Input and Output

In C, strings are always processed as character arrays. A string must be stored in a character array. The difference between it and the character array is that it always ends with a special character '\ 0.

When defining character arrays to store strings, you should first estimate the length of the string so that the array length must always be greater than the actual length of the string.

String "How do you do? "The actual storage image is as follows:

H
O
W
D
O
Y
O
U
D
O
?
\ 0

String Input and Output Methods:

1. Character-by-character input and output. It is in the format of "% C.

For (I = 0; I <10; I ++)
Printf ("% C", C [I]);/* the output element of the character array */

2. The entire string is input or output once. In the format of "% s.

Printf ("% s", c);/* Note that the name of the character array is used here */

Determine the result when the following character array content is output by string:

A
B
C
\ 0
E
F

How to input a string?

Scanf ("% s", c);/* Note that the length of character array C must be large enough */

When the program runs the preceding statement, it will stop and wait for the user to input a string from the keyboard. When the input is made, it will end with a space as the string or press Enter. After the input, the system automatically adds the '\ 0' flag after the input string.

Please judge the following program execution results:

Static char STR [13];

Scanf ("% s", STR );

How are you? <Return>/* This is the content entered by the user. It will be displayed in this color later */

What is the string of the STR character array?

17. Other string processing functions

See the following example:

Main () <br/>{< br/> static char STR [40]; <br/> static char str2 [40] = {"tail "}; <br/> gets (STR); <br/> puts ("\ n"); <br/> puts (STR); <br/> strcat (STR, str2); <br/> puts (STR); <br/> strcpy (STR, str2); <br/> puts (STR ); <br/> If (strcmp (STR, "tail") = 0) <br/> printf ("Equal \ n "); <br/> else <br/> printf ("not equal \ n"); <br/> printf ("% d", strlen (STR )); <br/> strupr (STR); <br/> puts (STR); <br/>}

18. See how to use character Arrays

The following program is used to enter a line of characters and count the number of words. Words are separated by spaces.

# Include "stdio. H "<br/> main () <br/> {<br/> char string [81]; <br/> int I, num = 0, word = 0; <br/> char C; <br/> gets (string); <br/> for (I = 0; (C = string [I])! = '\ 0'; I ++) <br/> If (C = '') word = 0; <br/> else if (WORD = 0) <br/>{< br/> word = 1; <br/> num ++; <br/>}< br/> printf ("there are % d Words in the line \ n", num ); <br/>}< br/>/* ----- <br/> results: <br/> I am a boy. <return> <br/> there are 4 words int the line <br/> */

May I give it a try?

Write a program and change the first letter of each sentence in the input string to uppercase.

20. Homework

Back directory

 

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.