110 likes | 128 Views
Engineering 1020. Introduction to Programming Peter King peter.king@mun.ca www.engr.mun.ca/~peter Winter 2010. ENGI 1020: Fun with Vars. Lets look at some practical examples of using variables with functions The following example has a function to calculate speed In Class Example 4
E N D
Engineering 1020 Introduction to Programming Peter King peter.king@mun.ca www.engr.mun.ca/~peter Winter 2010
ENGI 1020: Fun with Vars • Lets look at some practical examples of using variables with functions • The following example has a function to calculate speed • In Class Example 4 • We can use variables in many ways • Cleaning up functions • Calling functions • Storing data
ENGI 1020: Fun with vars • Which type should we use: • to count the number of students in a class. • to represent the slope of a roof. • to represent the response to a multiple choice exam question, the answer for which is a, b, c, d or e. • to represent the speed of a car. • to represent Avagadro's number (which if you don't know, you can Google).
ENGI 1020: Fun with vars • When is a variable not a variable?
ENGI 1020: Fun with vars • When is a variable not a variable? When it's a constant
ENGI 1020: Fun with vars • Constants have many uses • Many engineering problems use big numbers that don't change • pi = 3.14159265 • e = 2.71828183 • g = 9.80665 m/s • We want these numbers in memory so we can use them • BUT! They shouldn't change
ENGI 1020: Fun with vars • C++ allows us to create a non-variable variable const float PI = 3.14159265 • The const is a special keyword that tells the compiler to create a variable as before • Name, address, type, value • … but keep it locked so that nobody can change it • For style we put their names in capitals
ENGI 1020: Expressions • What is an expression? • A combination of variables, constants, operators and functions which is progressively evaluated an operation at a time until it is reduced to a final value. • No matter what's included, an expression will be progressively reduced (evaluated) to a single value!
ENGI 1020: Expressions • Arithmetic expressions • Two flavours: • Integer • Floating point (real) • The computer handles each in a very different manner • Integer operations are much faster (in computer time) • The floating point operations tend to be more powerful and useful
ENGI 1020: Expressions • Operators • Two types • Unary – only have one operand • Binary – two operands • For Integers • Unary operators are: +, - • Binary operators are: +, -, *, / % • For real numbers • Unary operators are: +, - • Binary operators are: +, - *, /
ENGI 1020: Expressions • Integer operators • Take integer operands and return an integer • Don't round • 11/7 would return 1 • Real operators • Take real operands and return a real