• No se han encontrado resultados

Análisis e interpretación de la encuesta

6. RESULTADOS

6.1. Análisis e interpretación de la encuesta

If you have run the example code on page 6 it will ask you to supply a principal, interest rate and duration of an investment. It then computes the interest that will be accrued.

The mathematics is just compound interest. If the principal is P , the interest rate is i per annum, and the duration of the investment is T , then the interest accrued will be P (1 + i)T− P . Note that we’re using i here for an interest rate compounded once per year.

The first few lines of code are all what is called “boiler plate” code. This is a term for boring code that you have to write for technical reasons but which doesn’t do much. It is called “boiler plate” because boilers often come with a steel plate attached saying who made them and perhaps containing some warnings about how to operate the boiler. Nobody ever reads this, but it has to be there “for legal reasons”. The same is true for the first few lines of our code. Its dull, and for the time being we’ll skip it.

The first interesting line of code is:

//␣I n t e r e s t i n g␣c o d e␣s t a r t s

This is an example of a comment. Once you write //, C++ ignores the rest of the line. This allows you to put in helpful comments to guide others through your code.

The last interesting line of code is /*␣I n t e r e s t i n g␣c o d e␣e n d s␣*/

C++ also ignores any text sandwiched between the character combinations /* and */. It even ignores new lines. For this reason this is called a multi-line comment.

The first lines that actually do anything are the lines:

int␣p r i n c i p a l ;

d o u b l e␣i n t e r e s t R a t e ; int␣n u m b e r O f Y e a r s ;

These lines tell the computer to make room in memory for three variables called principal, interestRate, and numberOfYears.

In order to work out how much space it will need to store the data, C++

needs to know what sort of data we will store in these variables. We will store an integer in the variable principal and in the variable numberOfYears. We will store a real number in the variable interestRate.

The phrase “int␣principal;” means: “please make room in memory for a variable called principal that will store an integer”.6

6If you want to be really picky, it means make room for an integer within a certain range

The phrase “double␣interestRate;” means: “please make room in mem-ory for a variable called interestRate that will store a real number”. There are two keywords you can use in C++ to store a real number. You can use float which stores a floating point number to a certain precision, or you can use double which uses twice as much memory but is much more precise. Com-puter memory is cheaper now than it was in 1969, so float isn’t used much any more. The strange name double lives on for backwards compatibility.

If you think of double as meaning “real number” you won’t get into much trouble.

Notice the semi-colon at the end of the statement “int␣principal;”. Ev-ery statement in C++ ends with a semi-colon. You can think of it as the C++

equivalent of a full stop in English. However, C++ is much fussier than the English language—if you forget a single semi-colon, the program won’t work.

The line

c o u t␣< <␣" How␣m u c h␣are␣you␣i n v e s t i n g ?\ n " ;

writes the text “How much are you investing?” to the screen and then starts a new line. Anything enclosed in quotation marks is interpreted as text rather than computer code by C++. The special sequence of characters \n means insert a new line.

The line

cin␣> >␣p r i n c i p a l ;

means read a number typed by the user and store it in the variable principal.

The next few lines behave much the same.

The mathematical heart of our calculation is given by the lines:

d o u b l e␣f i n a l B a l a n c e␣=

pow ( 1 . 0␣+␣i n t e r e s t R a t e␣*␣0.01 ,␣n u m b e r O f Y e a r s )

*␣p r i n c i p a l ;

d o u b l e␣i n t e r e s t␣=␣f i n a l B a l a n c e␣-␣p r i n c i p a l ;

These lines make room in memory for two variables called finalBalance and interest and immediately assign values to these variables.

The computation of the interest is simple. It is just the finalBalance minus the principal.

The final balance is computed using the formula P (1 + i)T where P is short for principal, etc. Our code just expands the variable names, replaces multiplication with *, and uses the function pow to raise a number to a given power. In general pow(a,b)= ab.

Notice that C++ doesn’t care if your statements go over multiple lines.

This is great if you want to write a long formula. The price you have to pay is that you must end all statements with a semi-colon, since C++ doesn’t look

of values that depends upon whether you have a 32 bit or a 64 bit computer, but that really isn’t important right now.

at the spacing of your code to guess where one statement ends and another begins. We have indented our code to indicate that the first three lines should be read as a group. You should always space your code carefully to make it easy to read.

Unlike many other languages, C++ doesn’t have a special symbol for rais-ing a number to a given power. You might think that the symbol ^ would be a good choice, but C++ in its wisdom uses that for another purpose.

Notice that in mathematics we normally use single letters for variable names such as the formula P (1 + i)T. When writing software, it is usually better to use long variable names. This is because you can then just read the code without having to go somewhere else to find what all the letters stand for.

The next few lines should now be self-explanatory. They print out the answer.

c o u t␣< <␣" You␣w i l l␣e a r n␣" ; c o u t␣< <␣i n t e r e s t ;

c o u t␣< <␣" \ n " ;

This example shows why C++ makes you specify the line breaks by hand:

Sometimes it can be useful to print out only part of a line.

The last few lines are more boiler plate:

r e t u r n␣0;

}

If you are curious about what the boiler plate code means, don’t worry. All will be explained shortly.

Exercises

One of the most difficult parts of learning C++ is learning how to cope with compiler errors. So this section is very important. I recommend you refer back to it whenever an error happens in your code.

The exercises below show what actually happens if we make some small mistakes in our code.

1.4.1. In the line

int␣p r i n c i p a l ;

remove the semi-colon and then compile the code (recall that on Visual Studio that means press CTRL F5 and on Unix you should type make␣all). What error message is reported? Do you find this error message helpful?

Note that if you are working on Windows, you will have to use the scroll bar to move the text in the Output window to the right. Better yet, find the button on the toolbar of the Output window to enable word wrap.

Examine the error message. What part of it do you find most helpful? Can you see how the compiler reports which line contains the error? Has it got this right?

Put the semi-colon back, and make sure you can compile and run the code once again.

1.4.2. Repeat the exercise above but removing the ) symbol in the calculation of the final balance instead. Make sure you get everything working again before moving onto the next exercise.

1.4.3. Repeat the exercise above but removing the { symbol at the end of the line

int␣m a i n ()␣{

Don’t panic! Just fix the problem.

1.4.4. Repeat the exercise above, but this time instead of just deleting a character, insert a whole new first line of code that just contains the letter x.

Thus the code should start x

#include␣<iostream>

Don’t panic! Just fix the problem.

1.4.5. As well as compilation errors, code can contain programming errors that the compiler does not spot. Arguably our example contains one already.

What happens if you type “1000$” as the amount you would like to invest?

This happens because our code assumes you will only type in numbers.

1.4.6. What error do you get if you completely delete the line int␣principal;?

This happens because before you can use a variable in C++ you must tell the compiler what type the variable is.

Tip: Dealing with compilation errors

C++ is very sensitive to tiny punctuation errors. When you get a screen full of errors, don’t panic just: scroll up to the first error, fix that, and try again.

Once C++ is confused it starts misinterpreting all of your code completely.

So one tiny error can look like a disaster. This is why you should only ever try and fix one error at a time.

You should treat the error messages as the compiler’s best guess of what you’ve got wrong. As the examples above show it can get both the line numbers wrong and the description of the error wrong.

In particular the line number might be slightly below the actual line con-taining an error, or if you are really unlucky it might think the error is in a different file. The line numbers reported as containing errors actually mark the points where it realised that there was an error. This is why they are often below where the real error occurred.

If the compiler tells you that the error is not in your code but in a library it has definitely go it wrong!

You will find it very hard to work out where you have made an error in your code. So always write programs a couple of lines at a time, constantly testing that they compile and run. Then you know that any error that appears must be in the lines you’ve just written.

If you made the mistake of writing an enormous chunk of code and can’t work out where the errors are, then delete it all (or comment it out) and put it back piece by piece. Learn from your mistake and start writing code a little bit at a time.