What does this following C code implement -


I am passing the previous exam where I have been asked to apply the following C code and write it with meaningful variables There are names and good programming methods.

The code is as follows:

  is zero (int ** a, int b) {for (int x = 1; x & lt; = (i / j) * (J / I);}   

I do not understand how this code works:

1) Y has never been declared in the loop goes.

2) Arithmetic I and Jammu I do not understand, if I and Jammu have not been declared.

I originally thought that it was an intake and I and Jammu are just X and Y. But if this is the case then this question is much easier than any other.

The code is probably intended for 1 diagonal with 1 and other identification matrix at 0 other places.

The first example of bad code in classic is given by Karnnighan and Plager for Fortran equivalent of that code.

Working from memory, Fortran code was broadly:

  DO 10 I = 1, n DO 10J = 1, I, J = (I / J) * (J / I)   

(May be two labels, say 10 and 20, and continue one or two statements, but I think that At that time, the columns were reserved for 1-5 numerical label, column 6 was a continuation indicator, and the program was captured in 772 columns, column 73-80 is an optional statement sequence number.)


Since y is used It is done but is not declared, if it is compiled, then y should be either an external variable (either a global variable or a variable with the file scope: extern int Y; or int y; or constant int y; outside the ceremony).

Since i and j are used but are not declared, if it is compiled then they should also have external variables And it did not give that neither i nor j has changed, the same value (either 0 or 1, that is i! = J ) is assigned to each element of the fact a . To implement the same (bad) algorithm, there must be a 'true' code:

  zero H (int ** a, int b) {for (int X = 1; x & lt; = b; x ++) for (int y = 1; y & lt; = b; y ++) a [x-1] [y- 1] = (x / y ) * (Y / X); }   

Of course, this is not like many, but it avoids split-to-zero problems, though, cleaner and simple code can be written:

  is zero (int ** a, int b) {for (int x = 0; x & lt; b; x ++) {for (int y = 0; y and lt; b; y ++ A) [x] [y] = 0; A [x] [x] = 1; }}   

The name of the function should be meaningful by changing the name.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -