20 how to store a batch of data and arrays in C,
An important reason for using software is that it can help us repeat many things. We have already talked about loops. Loop is to repeat one thing. Have we ever thought about how to store a batch of data that we need to repeat in a program?
For example.
We have an address book program that needs to save all the names in the address book. We can do this.
String name1 = "Mr. David ";
String name2 = "baby ";
String name3 = "mom ";
.
.
.
String name300 = "ex-girlfriend ";
Suppose there are 300 names in this address book, we will define three hundred variables to store them. So troublesome! Arrays respond to people's urgent needs at this time and are born. In the software field, many technologies are generated because of the need behind them. The array is like this, so are software products. A successful software product is due to a large number of requirements. Who solves the problem well and solves the problem early will be more likely to succeed.
Define and initialize an array
Data Type [] array name = new data type [n];
Continue with the above example. Let's define an array name and initialize the array to save the 300 strings.
String [] name = new string [300];
Access to array members:
The array can be accessed only after being defined and initialized. Members in the access array can use the indexer. What is an indexer? It is to write an integer in square brackets, [sequence number]. Note that the number of members in the array starts from 0.
For example, you need to assign values to the first and second names, third and last names in the address book.
Name [0] = "Thinking Jiang ";
Name [1] = "baby ";
Name [2] = "mom ";
.
.
.
Name [299] = "ex-girlfriend ";
Note that the number of members in the array starts from 0. Therefore, the first name is name [0] instead of name [1]. The last name is name [299] instead of name [300].
Arrays are usually processed in a program, and they are all made of loops. Let's start with a specific example to help you understand it.
You create a project. The solution name is Sloution20, the project name is Exercise20, and the project type is console program. Click Program. cs. Add the code to it.
This section briefly explains the Code to facilitate your understanding.
1. Row 13th defines an integer array number and initializes it to store five integers.
2. Rows 16-20 allow users to randomly enter 5 integers and store them in the number array in sequence.
3. Sort the array from row 21 to row 33 using the bubble sort method. This is the legendary algorithm. Nested loops are also used here. Nested loops can be used in loops. After the entire cycle of the inner layer is completed, the outer layer begins the next loop.
4. output results after sorting from 34 rows to 38 rows.
Running result
Exercise
1. Read this code and add comments to important statements. If you cannot understand it, contact me.
2. If an array int [] number = new int [5] is defined, can I store 6 integers in it? Try to save it and see what the result is.
3. modify the program so that the user can input 6 decimal places and then output them in ascending order.
4. Try to write a nested loop of three types to understand the meaning of the nested loop.
Pay-as-you-go answers to questions C # encountered during the course of getting started.
Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.
HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.
What do you need most at this time?
Yes! A professional teacher will help you!
If you have any questions during this course, you can ask the author for help via qq or email.
Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.
The payment method is very simple:
Scan the following QR code. Leave your QQ number or email address in the payment message.
Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.
Instructor qq: 2040227296
Instructor email: 2040227296@qq.com