Best way to explain declarative error in C ++?

How can you explain why the following code is incorrect, since the author is trying to write C ++ code declaratively, rather than procedurally?

const double NEWTONS_PER_POUND = 4.448; int main() { double pounds, newtons; pounds = newtons/NEWTONS_PER_POUND; /* pounds equals 'unassigned variable'/4.448 */ newtons = 10.0; cout << pounds << endl; /* a big number, not 10.0/4.448 */ return 0; } 

The author expected cout display the correct calculation, but instead get a "crazy number".

I would explain it as "C ++ is procedural, and therefore during declaration

 pounds = newtons/NEWTONS_PER_POUND; 

newtons not assigned a value.

Any best deals? Or an explanation why C ++ is not smart enough to execute the behavior that the user mistakenly expected?

+4
source share
10 answers

Tell the author that

 pounds = newtons/NEWTONS_PER_POUND; 

CPU command on

  • take value at an address called "newtons"
  • take the value at the address designated as "NEWTONS_PER_POUND"
  • separate them
  • save the result to the address designated as "pounds"

what he is looking for is most likely a function in imperative terms:

 double newtons_to_pounds(double newtons) { return newtons/NEWTONS_PER_POUND; } ... newtons = 10.0; cout << newtons_to_pounds(newtons) << endl; /* a big number, not 10.0/4.448 */ return 0; 
+19
source

C ++ is a required programming language, not an equation solver.

C ++ executes instructions in the order in which you write them. C ++ does not initialize variables unless it is said. C ++ allows you to use a variable whose value has not been initialized, but when you do this, the result is not specified. Unspecified means that everything can happen, including bad things like making crazy numbers.

Here is a detailed explanation:

 double pounds, newtons; pounds = newtons/NEWTONS_PER_POUND; newtons = 10.0; 

The first statement declares two variables without initializing them. At the moment, their values ​​are not indicated.

The second statement reads the newtons value (which could be anything) and divides it by NEWTONS_PER_POUND . The result (which can be any) is assigned to pounds .

The third statement initializes newtons , but it is too late to affect the calculation just performed.

+17
source

Well, that shouldn't be too difficult to explain, no matter what the students are against: just everything that C ++ evaluates programs one at a time, statement after statement (despite compiler artifacts such as reordering ...).

Theres absolutely nothing special for C ++ - the way to handle this, and it is not even limited to computer programming, but rather its everyday way of dealing with an ordered list of instructions.

+8
source

This is not a lazy assessment of newtons

Thus, the calculation is performed during the announcement, and not during the request. It is after functional code, not what C ++ will do.

+4
source

If the person is not too technical, you can try:

"The statements in this C ++ program are similar to the steps needed to create a cake. You must follow the steps one by one, and they must be executed in a specific order for it to be successful."

+4
source

Explain that the pound is assigned a value in the line with the assignment operator:

 pounds = newtons/NEWTONS_PER_POUND; 

If this were not so, but this pound was estimated when it was used (as in the case of the cout instruction), then if the value of the newtons changed, then the value of the pounds would also change. Since pounds are not pointers of any type, but are a simple integer, this is not possible.

+1
source

How about passing code in a debugger?

IME is nothing like this to understand the execution of a program written in a procedural language (i.e., it simulates how the processor actually executes the code).

+1
source

You are trying to get the listener to go through a paradigm shift - to change your whole approach to understanding this code.

"pounds" is just a number. He did not receive the concept of its creation. You say "pounds" how it is created; it will not be remembered. He simply remembers that this is not how it is created.

The anthropomorphism of the memory block may seem a little strange. :-)

0
source

Here is an example that explains this quite accurately; devx.com/tips . A simple answer initializes everything before you read it, after all, the only thing it will cost is a couple of additional key touches. This is not something that is really worth spending on the brain!

0
source

Take a slightly more complex example where a variable of type newtons reused and assigns values ​​more than once. For instance:

 double pounds, newtons; newtons = 10.0; pounds = newtons/NEWTONS_PER_POUND; cout << pounds << endl; newtons = 15.0; pounds = newtons/NEWTONS_PER_POUND; cout << pounds << endl; return 0; 

Show him the code and output. Then ask him to explain how and why the program produces different numbers for each line. I think this should help push it towards viewing the program as a procedure that runs from top to bottom.

0
source

All Articles