In my teaching process, found that many students think that programming is a difficult thing, even a classmate say I use the prehistorical powers, only “c” on the final exam last semester for 60 points, and listen to a lot of senior said that the course “data structure” the average passing rate of less than 50%, so many classmates chose to face difficult away, the idea of having the change major. So is this really the case? Is programming really hard?
Do programming is difficult, if you want to become an excellent development engineers do need to have a lot of conditions, such as your basic knowledge of computer science (algorithms and data structures, operating system, compiling principle, principle of database, computer network, etc.), English, the ability of analyzing and resolving problems and so on, these are not going to practice, It takes a long time to learn and understand. But if your goal isn’t that high and you’re just trying to learn the basics of C or data structures, it’s not as hard as you think. So why do so many students find programming so difficult? The reason lies in the lack of good learning methods.
Today, let’s talk about some things that beginners should pay attention to.
1 Code Format
We all wrote essays when we were kids, and if your writing is sloppy and sloppy, you’re going to get a bad grade, no matter how informative and logical your essay is. Because the teacher saw the composition for the first time, do not want to continue to read, so the low score is expected.
Writing code is like writing an essay. No matter how good your code is and how logical it is, the first thing you need to do is format your code nicely. Indent what you need to indent and wrap what you need to wrap.
Code 1
Code 2
Which of the two codes would you rather see? Code 1 has no format, no indentation, no line breaks. Imagine how you would feel if you read code like this.
So write code is for people to read, the first thing to do is a good format, I believe this is every student should do.
2 Order of Precedence
Do everything in order, what to do first, what to do next, what to do last. It’s all about priorities. It’s not about doing what comes to mind.
For C programming, the first thing we need to write is a program template.
#include <stdio.h>
int main(){
// Your code
return 0;
}
C language is a process-oriented language, that is to say, a C language program is composed of independent functions, among which there is a very special function, that is, the main function. Why is it special? Because it’s the entry point to the whole program.
With the above basic theory, we know that writing C programs is actually writing a function.
So, how a function is defined and what its components are, I believe this is something that every student can do.
Don’t eat fat all at once
Do not do anything in a hurry, step by step, impatient is not hot tofu, the same for programming is no exception.
The more code a program has, the more likely it is to have hidden bugs, and the longer it will take you to fix them. I’m sure most of you will agree with the above statement, so if a program only prints “Hello,world”, the program will not have any problems.
The above statement tells us a basic truth about programming: all large programs are not built in one go, but in small increments. I would only add a new feature if I made sure the previous one was correct.
Here’s a simple example:
Input a group of information of 5 students, including student number, name, mathematics achievement, computer achievement, get the average score and total score of each student, and then sort according to total score from high to low.
This is an assignment I give students in class, many students have realized the function, but in the realization of the process encountered a lot of problems, next I will introduce, if it is me, I how to achieve.
Step 1: Write the template.
#include <stdio.h>
int main(){
return 0;
}
Step 2: Analyze the topic and determine how many tasks there are.
Through the analysis of the topic, we find that there are three main tasks:
1) Input the information of 5 students;
2) Calculate the average score of 5 students;
3) Rank 5 students according to their total scores.
That’s a very, very important step, you have to be very aware of what you’re doing, right? Break a big task down into smaller tasks, and then tackle each of those smaller tasks. When all the small tasks are solved, the big tasks will be solved automatically. This is the famous “divide and conquer” idea.
With the task breakdown above in mind, let’s tackle the first task first: “Input information for five students”.
Each student contains the student number, name, math score and computer score, which structure should be used to save it? Now, if you’ve learned C before, it should immediately occur to you that we should use constructs.
struct student{
int no;
char name[20];
float math_score;
floatcomputer_score;
};
Input the information of 5 students, and then save it in where? We can think of structures very quickly.
struct student students[5];
Finally, there is the problem of how to input, which is to store the user’s input information in the structure array above.
With the above analysis, we can quickly write the following code.
#include <stdio.h>
struct student {
int no;
char name[20];
float math_score;
floatcomputer_score;
};
struct student students[5];
int main(){
int i = 0;
for(; i<2; i++){
scanf(“%d,%s,%f,%f”, &students[i].no, students[i].name,&students[i].math_score, &students[i].computer_score);
}
return 0;
}
But one thing you should notice about this is that my code is done, but I didn’t verify it, so I should add some validation code.
#include <stdio.h>
struct student {
int no;
char name[20];
float math_score;
floatcomputer_score;
};
struct student students[5];
int main(){
// Implement the code for task 1
int i = 0;
for(; i<5; i++){
scanf(“%d,%s,%f,%f”, &students[i].no, students[i].name,&students[i].math_score, &students[i].computer_score);
}
// Additional code written to verify the correctness of task 1
for(i = 0; i <5; i++){
printf(“%d, %s, %f,%f”, students[i].no, students[i].name,students[i].math_score, students[i].computer_score);
}
return 0;
}
I won’t start coding task 2 until I finish the code for Task 1 and verify that it’s correct.
The above is the basic method of programming for you to analyze, only after the previous task is completely correct premise, will start a new task.
conclusion
Hopefully, these three tips will help you write better code in the future.
In the process of learning programming, what problems you encounter, welcome to leave a message below, we will provide you with the programming knowledge you need according to your message.