Lesson 3 - Simple Expressions
3.1 Integer Expressions
Example ex3-1.cpp introduces some more complex arithmetic expressions. It also introduces the idea of a compile-time constant. The program calculates how many boxes of tiles you need to tile a floor. It knows the size of each tile, inputs the size of the room, and does a simple calculation.
The instruction 'const int TILES_PER_SQ_METRE=9;' looks like a variable declaration, which it is, but with a special characteristic. This instruction defines a memory location called TILES_PER_SQ_METER and fills that location with the integer 9. However that location is also declared as 'const', i.e. constant. This means the content of the variable cannot be changed while the program is running. The value of TILES_PER_SQ_METER will always be 9. The only way for the value to be changed is to change the number in the source file before the file is compiled.
In the last output instruction in the program you can see a more complex arithmetic expression: the symbol '*' means multiply, and the symbol '/' means divide. Parentheses can be used to control the order in which the expression is evaluated.
3.2 Floating Point Expressions
The previous example is modified in example ex3-2.cpp. Here the actual sizes of the tiles is specified, and the fact that we need a whole number of tiles to fit across and along the room is explicitly acknowledged.
The constant TILE_SIZE is set to the fractional number 0.3. The width and the length of the room are now fractional numbers also. To find the number of tiles needed to fit across the room we need the whole number that is bigger than width/TILE_SIZE. We can find this by adding 1 to the whole number part of the fraction width/TILE_SIZE. To take the whole number part we 'cast' the fractional expression into the form of an integer with the operation '(int)' in front of the expression. Thus if width = 3.15 meters, then width/TILE_SIZE would be equal to the value 10.5, and (int)(width/TILE_SIZE) would equal 10, and nwide would equal 11 - the number of tiles we would need to fit across the room.
3.3 String expressions
Example ex3-3.cpp introduces some string expressions: some operations that you can do with string variables. In particular the example shows how a whole line of text can be read from the keyboard, how a specific character can be searched for, how a sub-sequence of characters can be copied out from a string, and how strings can be concatenated to form new strings.
The instruction 'getline(cin,string-variable)' reads a whole line of text from the keyboard up to but not including the newline character.
The instruction 'fullname.find(" ")' makes a request of the fullname string object to search itself for a space character, and to return the sequence number of the first one it finds. The location of the space is stored in the integer variable nsep.
The instruction fullname.substr(0,nsep) requests of fullname to take a copy of the first nsep characters: thus if nsep was 4, then substr(0,nsep) would consist of the first four characters of the string. Character positions are indexed from 0. The instruction 'fullname.substr(nsep+1)' also requests a sub string from fullname, this time starting at sequence 'nsep+1', i.e. one after the space, and continuing until the end of the string.
3.4 Exercisesa. Write a program (ftoc.cpp) that reads in a temperature in Fahrenheit and converts it to a whole number of Celsius (the conversion formula is C = (F-32)*5/9), as in:
ftoc Enter Fahrenheit value : 70 70 Fahrenheit is 21 Celsius.
hours Enter a fractional number of hours : 2.5 2.5 hours is 2 hours and 30 minutes.
Enter a sentence (no full stop) : The cat sat on the mat
The last word was: 'mat'