How to Write C code -- exercise solution 1 (Chapter 1 Exercise 7)

Source: Internet
Author: User

Since primary school, people are told that questions should be reviewed before they are answered. Although this principle is simple, it is extremely important.
However, this is precisely because the truth is too simple, so it is inevitable to be despised-or even forgotten. There have been countless failures or detours in human history due to contempt for simple truths. Ignoring seemingly simple truth is always punished. In the era of lack of common sense, we should pay more attention to common sense.
This is also true for programming. People tend to think that writing program code is the most difficult task, and whether the problem to be solved by programming is true or reasonable is often ignored. It is obvious. In software engineering, demand analysis is incorrectly considered as the simplest step in a long history. In recent years, some studies have pointed out that more than 50% of Software defects are caused by requirement analysis errors, and Software defects caused by coding errors only account for about 1/4 of all defects. At this time, many people suddenly realized that it is more important to correctly raise a question than to solve the problem. If the problem fails to be correctly raised, it cannot be solved correctly.
The truth is easy to understand, but it is another thing to take the demand analysis seriously. This also requires well-trained professional cultivation and professional habits. It is definitely not just recognizing the importance of demand analysis in mind that we can develop good professional habits. The cultivation of good habits requires a lot of conscious self-training. Good habits can be formed only after repeated exercises.
The earlier the training, the better. Once you start learning programming, you should take the initiative to cultivate and train good programming habits, rather than until you learn software engineering.
When learning programming, one of the ways to develop good programming habits is to actively train yourself by copying questions. When copying a question, check whether the question is clear and reasonable. Another benefit of doing so is that the person who reads your code can clearly understand what the program is going to solve.

The following example shows how to review the question:
"Enter two numbers to obtain the maximum common number"
Here, the question requires that it is unclear what the two numbers are. Is it an integer? Score? Real number? Plural? Such problems cannot be solved. Because the problem itself is not thorough and strict.

When copying a question, you can also think about the specific requirements of the program and clarify the functions of the program. If necessary, you can further clarify it. There is a set of so-called "One hundred typical C language examples" circulating on the Internet. The first question is a good counterexample:
[Procedure 1]
Question: How many three numbers can be composed of 1, 2, 3, and 4 numbers that are different from each other and have no repeated numbers? What is it?
1. Program Analysis: It can be filled in hundreds of digits, ten digits, and one digit is 1, 2, 3, and 4. Make up all the arrays and then remove those that do not meet the conditions.
2. program source code:
Main ()
{
Int I, J, K;
Printf ("\ n ");
For (I = 1; I <5; I ++)/* The following is a triple loop */
For (j = 1; j <5; j ++)
For (k = 1; k <5; k ++)
{
If (I! = K & I! = J & J! = K)/* Ensure that I, j, and K are different */
Printf ("% d, % d, % d \ n", I, j, k );
}
}

Aside from many other issues in this Code, the question "How many three numbers can be composed of different and no repeated numbers" is obviously ignored by programmers. Such code has no value.

Therefore, we strongly recommend that you copy the question before writing the code.
Some people may think that the question cannot be compiled. It doesn't matter. This can be an excellent comment. In a sense, writing comments is more important than writing code. Although the problem cannot be solved, it is extremely helpful to solve the problem correctly or to locate and correct the error more easily.

The complete programming process of Chapter 1 Exercise 7 (page 21) is as follows:

0. Description of program functions

/*
Description: output the following pattern.

/\
/\
/\
/\
/________\
*/

1. Write out main ()

Many beginners write code with a single word from start to end. This method of writing code is very bad.
Just like building scaffolding before building a house, you should build the necessary framework before writing code. Generally, this framework is main (). All construction workers know this truth, but many programmers do not.
Code writing should be considered as the process of filling, perfecting, and completing main () in different ways-every program is like doing so, just like decorating a house. The house is always first built and then renovated. Isn't someone stupid enough to build a house while decorating it?

/*
Description: output the following pattern.

/\
/\
/\
/\
/________\
*/

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{

System ("pause ");
Return 0;
}

At this point, compile it. If there is a code error, the sooner you find it, the better. Many beginners are eager for success. They like to move forward with "illness" and try to eliminate bugs after all the code is complete. This is silly. As debugging becomes more difficult, the workload may increase several hundred times or more.

You should develop a good habit of compiling immediately after writing one or two codes. This habit is more meaningful for beginners. Just like rock climbing, we must always base on a solid and correct basis to move from victory to greater victory. It is unrealistic to try to complete the program in a hurry and overnight manner.

2. Fill in main ()
After the compilation is passed, consider adding main () gradually ().
The function of the program is to output 5 lines of characters on the standard output device. The function of outputting character sequences can be implemented by calling the printf () function.

/*
Description: output the following pattern.

/\
/\
/\
/\
/________\
*/

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Printf ("\ n"); // write the necessary parts first
Printf ("\ n"); // copied and pasted
Printf ("\ n"); // copied and pasted
Printf ("\ n"); // copied and pasted
Printf ("\ n"); // copied and pasted
System ("pause ");
Return 0;
}

After compilation, go to the next step.

3. Gradually refine

Now, the copied questions have more use. Copy and paste the corresponding part into the "" of the printf () function call.
Many beginners do not pay attention to using the "edit" function, which is not professional. Use the "edit" function in the editor whenever possible to improve programming efficiency.
/*
Description: output the following pattern.

/\
/\
/\
/\
/________\
*/

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Printf ("// \ n"); // write the necessary parts first
Printf ("// \ n"); // copied and pasted
Printf ("// \ n"); // copied and pasted
Printf ("// \ n"); // copied and pasted
Printf ("/________ \ n"); // copy and paste
System ("pause ");
Return 0;
}

Compile. The compiler may give a warning.

4. Improved

The "\" in "" cannot be directly written because it is an escape character. Rewrite "\" in "" to "\". This should be done through "Search" and "replace. During the replacement process, note that some "\" are replaced, while some "\" are not replaced.
Familiar with IDE editing can improve code writing efficiency and correctness.

/*
Description: output the following pattern.

/\
/\
/\
/\
/________\
*/

# Include <stdio. h>
# Include <stdlib. h>

Int main (void)
{
Printf ("// \ n"); // write the necessary parts first
Printf ("// \ n"); // copy and paste
Printf ("// \ n"); // copy and paste
Printf ("// \ n"); // copy and paste
Printf ("/________ \ n"); // copy and paste
System ("pause ");
Return 0;
}


So far, the code is complete. Compile, run the program, and check whether the running result is correct.

Advice:
Programmers should not only complete the correct code, but also complete the code in the correct way. Following a good process is an important guarantee of code quality, and the ingenuity of programmers is not.

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.