Simple learning C-day 4 and day 4

Source: Internet
Author: User
Tags array definition

Simple learning C-day 4 and day 4

Array

Before learning the array, it is necessary to review the previous knowledge. Of course, I will only mention it. For you, you should take a serious look at the previous knowledge points, baidu, haha.

We have learned roughly 1. define variables, 2. data input and output, 3. judgment statement if... else if... else, 4. loop statement while, do while,.

 

However, all the above is nonsense... The following describes arrays.

What is an array? Arrays are constructed data types. Is it hard to understand? To put it bluntly, arrays are the result of the aggregation of many elements of the same data type.

Of course, when using an array, you also need to define it. Do you still remember how to define a variable? Now, define an integer variable. If you have carefully reviewed the class content above, I believe it can be easily written.Int;Such a statement. Now, I am wondering how to define 1000 Integer Variables. Do we need to take one thousand variable names, suchInt a, B, c, d, e ......;Is that true? That's too much trouble. Therefore, the concept of array is introduced. The array definition is very convenient, as follows:

# Include <stdio. h> int main () {// defines the integer variable with one thousand spaces. // at this time, 1000 integer-sized memory space will be opened in the computer. // each space can represent an integer variable int [1000];}

The above program defines an integer array with a size of 1000. Of course, the appearance of arrays is for the convenience of input and storage. Arrays are generally combined with loops in the previous article.

 

1. Enter a number a in the first line of the keyboard, enter a number in the second line, press enter, and then the result of each number plus one is displayed.

Code:

# Include <stdio. h> int main () {// to prevent the occurrence of an excessively large value of n, generally, the value of int n, I, a [100000] in the array is relatively large. scanf ("% d", & n); // enter a number from the keyboard for (int I = 0; I <n; I ++) {// input n numbers from the keyboard cyclically scanf ("% d", & a [I]); // I represents the subscript of the array, array subscript starts from 0} for (int I = 0; I <n; I ++) {a [I] + = 1; // Add 1 to each number in the array through a loop // output result printf ("% d", a [I]);} return 0 ;}

Running result

Of course, you can use the while loop to perform the above operations. You can try it yourself and I will not describe it here. It is worth noting that 1. The array must explain in advance how much space has been opened up and how much space is needed. For example, if n is 1001 In the first line

Obviously, I only open 1000 spaces and can only store 1000 numbers. This phenomenon is called array out-of-bounds. Although the program can run, no error is reported, but it is wrong. 2. The first number of accessed arrays is 0, that is, a [0] represents the first number of arrays, and a [n-1] represents the last number of arrays. 3. the array space is the logical address that is consecutive, that is, it can pass through 0 ~ N-1 to access all elements in the array.

 

2. observe the numbers 1, 2, 3, 5, 8, 13, 21... after finding the rule, enter a number m smaller than 50 on the keyboard and press enter to output the number m in the series.

We can see that the sum of the first two numbers is equal to this number. That is, a (n) = a (n-1) + a (n-2) n> = 3

If the rule is discovered, how can we implement it using the program:

# Include <stdio. h> int main () {/* uses an array to store this sequence. Because the first two numbers are known, the next number can be known, so we can use the recursive idea to solve */int n, I, a [50]; // The subscript of the array starts from 0, and a [0] = 1; a [1] = 1; for (I = 2; I <= 50; I ++) {// recursive a [I] = a [I-1] + a [I-2];} scanf ("% d", & n ); /* Since the array subscript starts from 0, a [0] is the first number of the series, so the nth number is a [n-1]. */printf ("% d", a [n-1]);}

Running result

 

3. Not only statements can be nested, but Arrays can also be used. For example, if two arrays a [100] and B [100] exist, If array B is an integer array, you can use a certain number in array B to represent the subscript of array a, for exampleA [B [I]; B [I] is used as the subscript of array.

 

However, arrays use not only numbers, but also character arrays, which define a character array: char a [100]. Each space can store one character, for example, 'A', 'B ','. '. '? ', '8'... and so on. The differences between character arrays and strings must be distinguished here. Generally, a string has an ending character '\ 0', which is placed at the end of the character to be stored. end of the string. for example, if you want to store "good, time" with a total of nine characters, you need to open up 10 spaces in the array. The storage conditions are 'G', 'O', 'O ', 'D ', 't',' I ', M', 'E',' \ 0 '. the last one is the Terminator. in addition, operations on character arrays c define many library functions, such as copying syrcpy and finding the length of strlen. strcmp functions are quite common and need to be mastered.

Example: input a string from the keyboard without spaces and output it backwards.

Code:

# Include <stdio. h> // header file # include <string. h> int main () {int I, l; char a [10000]; // input string scanf ("% s", ); // evaluate the string length by using the library function l = strlen (a); // reverse output for (I = l-1; I> = 0; I --) printf ("% c ", a [I]);}

 

Because we only want to give everyone a simple understanding of the C language, and limited to the individual level, we all write some basic things. If you want to learn more deeply, you need to work hard.

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.