Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE1H421 Procedural Programming LECTURE 3 Variables (2)

Similar presentations


Presentation on theme: "SE1H421 Procedural Programming LECTURE 3 Variables (2)"— Presentation transcript:

1 SE1H421 Procedural Programming LECTURE 3 Variables (2)

2 Lecture Slides are unsuitable for learning how to program!
You just get an illusion of learning! Programming is a craft. You have to do it to learn it.

3 Last Lecture: How To…. declare a variable int anIntOfMine;
float aFloatOfMine; initialise a variable anIntOfMine = 5; aFloatOfMine = 5.25; declare and initialise a variable in one line declare (&initialise) multiple variables in one line int anIntOfMine = 5; float aFloatOfMine = 5.25; copy one variable into another one int anIntOfMine = 5, anotherInt = 3; float aFloatOfMine = 5.25, anotherFloat; calculate with variables anIntOfMine = anotherInt; anotherFloat = aFloatOfMine; anIntOfMine = 5 * anotherInt ; anotherFloat = aFloatOfMine ; input from keyboard int x; cout << “Enter an integer ”; cin >> x; cout << "you entered: “ << x << endl;

4 …also Naming Rules for Variables
Programmers can use any name for a variable, provided these rules are observed: the name must be one word only, no spaces helloWorld hello World always start with a letter not a number counter YearsOld avoid special signs such as £ ^ & % + etc. twoAndFour two&Four Try to start with a lowercase letter, then "uppercasing" goodMorning Goodmorning

5 Check your knowledge now:
Spot the mistakes / problems in the code: int counter = 2.57; float hello=2.1, you, there; there = hello + you; int mine=3; float theirs=4.5; mine = mine + theirs; myFloat = 2.7; 2.57 is a float not an int you not initialised Mixing different types. Could lead to problems type decl. float is missing

6 Check your knowledge now:
Declare two int variables called noOfStudents and roomsAvailable now and initialise them to 100 and 3 respectively. Figure out how many students fit into a room and store the result in a float called studsPerRoom. int noOfStudents=100, roomsAvailable= 3; float studsPerRoom; studsPerRoom = noOfStudents / roomsAvailable; What is the content of studsPerRoom now? 33.0 (not …)

7 So Far the Revision, New Stuff Now !
type casting modulus operator variable limits bit representation of numbers unsigned & negative numbers char & strings constants

8 Converting float int int var1; float var2 = 3.99;
Sometimes it is necessary to temporarily convert ints to floats and vice versa. This is done by a process called 'type casting' int var1; float var2 = 3.99; var1 = var2; // type casting here float int: stuff after the 'dot' gets chopped of ! var1 is now 3, NOT 4 !! It doesn't get rounded.

9 Type Casting Can be Useful:
Declare a double called penceAmount and initialise it to the value 255. Declare two ints called pounds and pence. Split penceAmount into Pounds and Pence and store the result in the respective variables. double penceAmount=255.0; int pounds, pence; pounds = penceAmount /100.0; pence = penceAmount - pounds*100;

10 The Modulus Operator An alternative way to split a pence amount into its Pound and Pence components makes use of the modulus operator %. The modulo operator calculates the rest after division: 10 % 3 is 1, % 4 is 0 The last line, however, will create an error message by the compiler: double penceAmount=255.0; int pounds, pence; pounds = penceAmount /100.0; pence = penceAmount %100;

11

12 When type casting be aware of the potential loss of precision

13

14 Variable Types and their Limits

15 More on Variable Limits
This is how the number 14 is represented in binary in a 'short': 16 bit 1 byte 1 byte 1 1 bit 16,384 128 16 8 4 2 1 214 27 22 21 20 = 14

16 More on Variable Limits
If we add 1 to the 14, the resulting 15 is stored like this: 16 bit 1 byte 1 byte 1 16,384 128 16 8 4 2 1 214 27 22 21 20 = 15

17 More on Variable Limits
Adding 1 to the 15 results in 16. All '1' bits flip to '0' and bit 4 flips to '1' 16 bit 1 byte 1 byte 1 1 16,384 128 16 8 4 2 1 214 27 22 21 20 = 16 = 15

18 More on Variable Limits
And this is the highest positive number that can be stored in a 'short' 16 bit 1 byte 1 byte Really? What about this one? 1 16,384 128 16 8 4 2 1 214 27 22 21 20 16, , = 32,767

19 Variable Types and their Limits
We are going to explore more in the labs: but what happens if the limits are exceeded?

20 More on Variable Limits
What happens if we add one more?

21 More on Variable Limits
Remember? Adding '1': All '1' bits flip to '0' and the next highest bit flips to '1' 16 bit 1 byte 1 byte 1 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, …… = - 32,768 214 27 22 21 20 16, , = 32,767

22 More on Variable Limits
Therefore -1 is represented as: 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, , …… = -1 214 27 22 21 20

23 More on Variable Limits
And -2 is : 16 bit 1 byte 1 byte 1 The Sign Bit 16,384 128 16 8 4 2 1 -32, , …… = -2 214 27 22 21 20

24 The 'unsigned' Type Modifier
If we declare a short like this: unsigned short x = 40000; This is a normal bit now 16 bit 1 byte 1 byte 1 32,768 4,096 2,048 1,024 64 4 2 1 215 212 211 210 26 22 21 20 32, , , , = 40,000

25 The 'unsigned' Type Modifier
Likewise: unsigned short x = 40000; unsigned int y = ….; unsigned long z= ….; Now, what about float and double ?

26 Limits of 'float' & 'double'
Floating point numbers are stored in two parts: mantissa and exponent 32 bit 'float' Mantissa (23 bits) Exponent (8 bits) Sign of mantissa (1 bit) From:

27 Limits of 'float' & 'double'
Example: 1 x 10-3 Sign of Mantissa Mantissa Exponent From:

28 Limits of 'float' & 'double'
This process is complex. Full knowledge not required but understanding is. Rough guide: Floating point numbers may be 100% accurate but usually they are not (you never know). The more bits (i.e. doubles), the more likely it is that the number is accurate (size matters….) cout << "Enter a float - > "; cin >> aFloat; cout << "You typed: " << aFloat;

29 Integral vs. Floating Point
Integral Types Floating Point Types char, short, int, long float, double precise rounded fast to compute slow to compute "sparse" "all numbers" limited range

30 Some special chars Some chars have a special meaning and can control screen output: char newLine = '\n'; cout << "Hello" << newLine << “World"; Will produce: Hello World The same could be produced by writing: cout << "Hello\nWorld";

31 Some special chars (cont.)
Another char control characters is: char tabStop = '\t'; cout << "Hello" << tabstop << "world"; Will produce: Hello World The same could be produced by writing: cout << "Hello\tWorld";

32 Strings Strings are "lots of characters together"
A string is an array of characters (more on arrays in a couple of weeks)

33 Using Strings

34 Constants Constants are variables that can't change
any more once they have been initialised. Constants are declared and initialised like normal variables - but there is something else: the keyword 'const' const int MY_CONST = 5; Look! All uppercase !

35 Why Constants ? Imagine a CAD package that uses 'inches' to
output measurements. At the beginning of the code you would write: const float MEAS_UNITS = 2.54; // 2.54 cm = 1 inch In the program 'MEAS_UNITS' is now used 400 times for calculations. 1. MEAS_UNITS can not be changed by accident in the code 2. If you want to sell the program to France where 'cm‘ is used, only 1 line needs to change.

36 Summary Today we have learned … about the limits of variable capacity
the internal bit representation of numbers about unsigned variable types (a bit) about the mantissa & exponent format how to use chars how use strings how to declare and initialise constants

37 Revision This was a lot (again) ! NOW:
Intro to Assignment (next week in labs) IN LABS More example programs AT HOME Revise, then program, program, program,…

38 END OF LECTURE 3

39 Demos Code::Blocks intro Type casting Using variables
Creating a project, compiling, running Examining compiler warnings Editing aids Colour coding Auto-layout Code completion Type casting int  float, float  int Modulus operator: % Using variables Signed vs. unsigned char as letter and/or number Reading strings from keyboard In-Class Test #1 introduction next slide

40 In-Class Test #1 In Labs as scheduled (be there on time)
30 minutes time A small programming task Variable & constant declarations  input  calculation  output Example task + marking scheme on Blackboard (‘Learning Schedule’, week 4, middle column) Open Book anything written or printed and/or on BB may be used no search on internet (PCs are monitored during test) Code Demo immediately afterwards (and possibly next week) mark & feedback no code demo = no mark Missed it? illness, accident, etc. >> get an "extenuating circumstances" form forgot >> "tough luck" - next week but capped at 40%


Download ppt "SE1H421 Procedural Programming LECTURE 3 Variables (2)"

Similar presentations


Ads by Google