Zoo tutorials: [ SQL | Linux | XML ]
ProgZoo: [ Java | C# | VB | C++ | Perl ]
Log in

A Gentle Introduction to
C++ Programming

Tutorial: Letters for numbers

 
Using variables. We must learn how to use variables in expressions. Just as in algebra we can use letters to represent numbers - they may be added, subtracted and multiplied.
Some integer operators
OperatorMeaningExpression value when x is 7
+Addx+310
-Minusx-34
*Multiplyx*321
/Dividex/32
%Modulusx%31

1. Twice


Big

The program fragment shown defines a function that doubles it's input. When the program is executed we see the result of the function for a handful of values.

The function definition is embedded in a larger program that is responsible for testing the function at values 1, 2, 3, 4, 5 and 6.

Notice what happens if you change the function so that it multiplies by 3 instead of 2.

2. Half


Big

Change the function so that it returns half the input value. This program only accepts an integer result so we ignore any remainder.

3. Modulus


Big

Change the function so that it returns the remainder when divided by two. For even numbers this is 0, for odd numbers this is 1.

Data types

There are several different types of number available. These include float, double, int, long, short

The double data type allows us to use floating point numbers.

4. Add VAT


Big

Change the function f so that it adds VAT to it's input. VAT is 17.5%

Hint: