I have some words that relatively beginners of programming want to say. I write here, hoping to give some help to entry-level programmers who are destined to see these contents.
Is C difficult?
First question, is C language difficult? Or is programming difficult? In the current environment, most of the people who come into contact with C language for the first time should be college students. Most of you have experienced China's college entrance examination, and most of you have achieved good results in the college entrance examination. To be honest, programming is simpler than many high school courses. Give two real examples:
Bill Gates was born in 1955, but in 1971, he began to write programs for Lakeside middle school, including a curriculum arrangement software [source Baidu Encyclopedia]. You can calculate that he should be 16 years old in 1971. Another person is even more powerful. Linus from Finland wrote the original version of the Linux operating system in high school. (Android can be said to be a branch of Linux on mobile phones)
Although these two people are geniuses, we may not be able to compare with them, but think about the problem you are doing now. Is the problem to be solved really difficult?
General steps for writing code
Today, let's talk about the general steps of writing code with the topic of a small partner in the group. I excerpt the content of the topic here:
"Enter the data of an integer two-dimensional array a[3][4] with three rows and four columns on the keyboard to find the saddle point. The so-called saddle point is the number at this position. It is the largest in its row (no number equal to it) and the smallest in its column (no number equal to it). Note: there may also be no saddle point. If there is no saddle point, output:" not exist ""
This problem involves multiple arrays, some students are a little afraid, but if you put aside writing code, if you are a program, if others give you a two-dimensional array, can you find the saddle point? I believe most people can? Even find a student in grade four or grade five of primary school. As long as you explain to him what is a saddle point, he can find out the saddle point in a given number for you.
If you are a program yourself, let me say something out of the topic. I hope you can put yourself in the position of the computer and consider the problem. For example, scanf ("% d,%d", & A, & b); The "% d,%d" in this code is called a format string, which means that the computer wants you to input in this format. For example, the format in this place is a comma between two numbers, so it's best to enter "3,4" when entering the values of a and b.
When you see a topic, the first thing to do is to think about ideas rather than write code directly. For example, when I see this question, the idea in my mind is as follows:
- Find the largest number in the first row and see if it is the smallest in the column.
- Find the largest number in the second row and see if it is the smallest in the column.
- Find the largest number in the third row and see if it is the smallest in the column.
Of course, because everyone thinks differently, everyone has different ideas, which leads to different codes written by each of us. Therefore, there is no standard answer to the problem of programming.
Thinking is the most important step in writing code. Without thinking, you can't write code. For example, the idea of this question, I believe most people can think of it, but they just can't write it, can they? This is another ability, the ability to convert your ideas into code. There is no shortcut to this ability, which can only be achieved by writing more and practicing more. What I fear most is that my eyes are high and my hands are low. If I think I have ideas, I won't write the code. In addition, it is also very important to think more. The brain must be used more. Don't ask questions in the group as soon as you encounter problems. Maybe you are a little close to success.
Write code
The next step is to write our ideas into code. It's difficult to say, it's not difficult to say.
#include <stdio.h> int main() { // For readability, the variables line and column are defined here to represent the number and the number of columns. int lineCount = 3, columnCount = 4; // Define array int array[lineCount][columnCount]; // Enter a value for the two-dimensional array for (int line = 0; line < lineCount; ++line) { for (int column = 0; column < columnCount; ++column) { scanf("%d", &array[line][column]); } } // Defines whether a variable stores saddle points int exist = 0; // Find saddle points row by row for (int line = 0; line < lineCount; ++line) { // Define a variable to represent the column with the largest value in a row int maxCol = 0; // Define a variable to represent whether there is more than one maximum value in this row int hasEqualInLine = 0; // Loop through the number in a row to find the maximum value for (int col = 1; col < columnCount; ++col) { if (array[line][col] > array[line][maxCol]) { // This condition holds, indicating that a larger number is found in this line maxCol = col; hasEqualInLine = 0; } else if (array[line][col] == array[line][maxCol]) { // Another number is found equal to the current maximum value hasEqualInLine = 1; } } // When the program comes to this point, it will cycle through a line. At this time, it can judge whether there is a saddle point in the current line if (!hasEqualInLine) { // This variable indicates whether the number is the smallest decimal in the column int isMin = 1; // Defines whether a variable represents whether the column has equal values int hasEqualInColumn = 0; for (int line2 = 0; line2 < lineCount; ++line2) { if (line2 == line) { // There's no need to compare it with this number itself continue; } if (array[line2][maxCol] < array[line][maxCol]) { // The number in the other rows in this column is smaller than the number we found isMin = 0; break; } else if (array[line2][maxCol] == array[line][maxCol]) { // Other rows in this column have numbers equal to those we found hasEqualInColumn = 1; break; } } if (isMin && !hasEqualInColumn) { // This number is the saddle point exist = 1; printf("a[%d][%d]=%d\n", line, maxCol, array[line][maxCol]); } } } if (!exist) { // The saddle point was not found at the end of the program printf("not exist\n"); } }
This is the code I wrote according to my ideas. Before writing this code, I didn't expect to write so many lines. In order to ensure readability, many comments are added. You can read it and verify it.
Write at the end
If you want to learn the C language well, you should read the textbooks issued by the school at least once. After reading the books once, you can basically master the grammar. Some people who ask questions don't even know how to write the switch statement, so they come to ask how to change it. When they meet such people, they really don't know where to start. In addition, before asking questions, you can try to check whether there are ready-made solutions on the Internet. After all, everyone's time is precious. I hope everyone who asks questions will ask questions after their own attempts, rather than asking others if they are too lazy to think or do. No one has the obligation to help you think.