1 / 11

Engineering 1020

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

equade
Télécharger la présentation

Engineering 1020

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Engineering 1020 Introduction to Programming Peter King peter.king@mun.ca www.engr.mun.ca/~peter Winter 2010

  2. 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

  3. 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).

  4. ENGI 1020: Fun with vars • When is a variable not a variable?

  5. ENGI 1020: Fun with vars • When is a variable not a variable? When it's a constant

  6. 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

  7. 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

  8. 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!

  9. 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

  10. 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: +, - *, /

  11. 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

More Related