SE1H421 Procedural Programming LECTURE 3 Variables (2)

Slides:



Advertisements
Similar presentations
Types and Variables. Computer Programming 2 C++ in one page!
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
If You Missed Last Week Go to Click on Syllabus, review lecture 01 notes, course schedule Contact your TA ( on website) Schedule.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Input & Output: Console
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSC 107 – Programming For Science. Announcements.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 2 Variables.
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Basic Computation
Chapter 1.2 Introduction to C++ Programming
Chapter 2 More on Math More on Input
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
Exercise 1 – Datentypen & Variablen
Programming Fundamentals
Computing Fundamentals
Revision Lecture
Other Kinds of Arrays Chapter 11
Student Book An Introduction
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
CS1S467 GUI Programming Lecture 2 Variables 1.
Java LESSON 1 Variables.
CS1S467 GUI Programming Lecture 3 Variables 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Number and String Operations
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Introduction to Java, and DrJava part 1
MSIS 655 Advanced Business Applications Programming
Chapter 2 Variables.
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Introduction to Java, and DrJava
CS150 Introduction to Computer Science 1
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
B065: PROGRAMMING Variables 1.
Fundamental Programming
Summary of what we learned yesterday
Primitive Types and Expressions
SE1H421 Procedural Programming LECTURE 2 Variables (1)
Introduction to Java, and DrJava
B065: PROGRAMMING Variables 2.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

SE1H421 Procedural Programming LECTURE 3 Variables (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.

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 + 7.0 - 3.2; input from keyboard int x; cout << “Enter an integer ”; cin >> x; cout << "you entered: “ << x << endl;

…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 counter17 17YearsOld avoid special signs such as £ ^ & % + etc. twoAndFour two&Four Try to start with a lowercase letter, then "uppercasing" goodMorning Goodmorning

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

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 33.3333…)

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

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.

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;

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, 20 % 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;

When type casting be aware of the potential loss of precision

Variable Types and their Limits

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 8 + 4 + 2 = 14

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 8 + 4 + 2 +1 = 15

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 + 0 + 0 + 0 + 0 = 16 8 + 4 + 2 +1 = 15

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,384 + 8,192 + . . . . + 2 + 1 = 32,767

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

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

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,768 + 0 + 0 + ……. + 0 + 0 = - 32,768 214 27 22 21 20 16,384 + 8,192 + . . . . + 2 + 1 = 32,767

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,768 + 16,384 + ……. + 2 + 1 = -1 214 27 22 21 20

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,768 + 16,384 + ……. + 2 + 0 = -2 214 27 22 21 20

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,768 + 4,096 + 2,048 + 1,024 + 64 = 40,000

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

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: www.atkinson.yorku.ca/~pkhaiter/Lectures1000/Lecture1000-5.ppt‎

Limits of 'float' & 'double' Example: - 0.0009876 1 -0.9876 x 10-3 Sign of Mantissa Mantissa Exponent From: www.atkinson.yorku.ca/~pkhaiter/Lectures1000/Lecture1000-5.ppt‎

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;

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

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";

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";

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

Using Strings

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 !

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.

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

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

END OF LECTURE 3

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

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%