I would improve a little. Like that:
void computeSomething() { int min = 0; int id = 0; for (int i = 1; i < m; i+=2) { min = matrix[i][0]; for (int j=1; j < n; j++) { if (matrix[i][j] < min) { // [оптимизация] min = matrix[i][j]; // [оптимизиация] } } if ( min < 0) { result[id] = min; id++; } }
The rest has not yet decided. Code questions:
- If they call you when you are at sea and ask "what is m, n, id in your code?". Can you answer without hesitation? Do you have any common names that you always use?
- Rename the matrix to more meaningful!
- Rename m, n, id to something adequate
What you did not consider:
- Min initialization at the very beginning
- They gave the name to such that when searching for bugs in your code a person tormented himself as much as possible. Imagine that in your company, when the committees in the repository, the full address of the authors and code modifiers is affixed to each source, and that the following programmers will know where to run and use the bat;
- Not put braces. Even for single actions it matters. Helps with debugging, easy to put printf and bryak;
If you really quite be meticulous to chew on trivial things, then:
- A piece of code with for (int j .... Can be fully rendered into a separate function, imagine that you are writing a comment. So call it
- Inside the same code, the section with if (matrix [] [] <min .. to put into a separate method conditionalSomething ();
- A piece of code with if at the very end is also in a separate method;
The question "Are the min, id right?"
Answer : Yes. They are declared as needed. Metric : as a rule, the variable should be declared within 5 lines, if further, then you have written too many letters and the code should be broken down into smaller functions.
No need to be like the Soviet period and its names "Glavstroynahmash" or "Sovreitrakh" or something else. Write as easy as possible!
PS: About optimization: "matrix [i] [j]", you can pull in: "int * curLine = matrix [i];" Then you can be addressed as: "curLine [j]"
}at the end, it is generally compiled (g ++). - avparray[index]. “Indexing” by means of address arithmetic (*(array + index)) is considered unreadable code and just a bad style. I do not advise adapting to a teacher with a qualification at the level of a freshman. The fact that, in principle, you can write does not mean what you need . There must be very good reasons for writing unreadable code. - VladD