Presentation is loading. Please wait.

Presentation is loading. Please wait.

CST-115 Introduction to Computer Programming

Similar presentations


Presentation on theme: "CST-115 Introduction to Computer Programming"— Presentation transcript:

1 CST-115 Introduction to Computer Programming
Lecture 5 - Arithmetic Operators Prof. M. Higazi

2 What are Arithmetic Operators?
take numerical values (either literals or variables) as their operands and return a single numerical value used to perform many of the familiar arithmetic operations that involve the calculation of numeric values a mathematical function that takes two operands and performs a calculation on them You can use an arithmetic operator with one or two arguments to negate, add, subtract, multiply, and divide numeric values.

3 Arithmetic Operators – Functions Palette Arithmetic Operators are found in the Functions Palette: Functions Palette Numeric

4

5 Arithmetic Operators – Code Design
Front Panel Block Diagram Control Terminals Indicator Terminals Wires Nodes All wires must be connected to the appropriate nodes and all nodes must be wired; failure to do so will give you inaccurate results or prompt you with an error message

6 Arithmetic Operators – Code Design
With LabVIEW, there are more than one way to write a specific piece of code. Consider the two examples shown below using simple add/subtract/product/division operators verses the Compound Arithmetic operator to build equations:

7 Example 1 Considering the following values for the Controls:
Number1 = 125 Number2 = 75 What would the indicator “Result” output?

8 Example 2 Considering the following values: Numeric = 3 Numeric2 = 5
What would the indicator “Result” output? Wires

9 Example 3 Considering the following values: Integer1 = 9 Integer2 = 4
What would the indicators “Sum”, “Difference”, and “Product” output? Although we didn’t cover this function yet, can you figure out what the “Remainder” and “Quotient” indicators would output?

10 Numeric Conversion Numerics default to double-precision (8 bytes) or long integer (4 bytes) LabVIEW automatically converts to different representations As an example, a “For Loop” (which will be covered later) count terminal always converts to a long integer Gray coercion dot on terminal indicates conversion The default representation of a numeric control or indicator is a double-precision floating-point number. LabVIEW can represent a number as: Byte (8-bit) signed or unsigned integer. Word (16-bit) signed or unsigned integer. Long (32-bit) signed or unsigned integer. Single-precision (32-bit) floating-point number. Double-precision (64-bit) floating-point number. Extended-precision (96-bit*) floating-point number. * Depends upon the operating system When two terminals of different data types are wired together, LabVIEW will convert one numeric to the same representation as the other. This is signified by a gray “coercion dot.” Class participation: Ask why the while loop MUST convert an input to an integer. <hint: can you have 1.5 loops?>

11 Arithmetic Operators - Numeric Conversion
LabVIEW chooses the representation that uses more bits. If the number of bits is the same, LabVIEW chooses unsigned over signed. To choose the representation, right-click on the terminal and select Representation. When LabVIEW converts floating-point numerics to integers, it rounds to the nearest integer. LabVIEW rounds x.5 to the nearest even integer. For example, LabVIEW rounds 2.5 to 2 and 3.5 to 4. If you wire two different numeric data types to a numeric function that expects the inputs to be the same data type, LabVIEW converts one of the terminals to the same representation as the other terminal. PREPARE for this slide. Carefully read and think through how to explain the last bullet point. A good idea is to use LabVIEW to demonstrate this point. Place a For Loop on the block diagram (note that they have not been introduced to for loops at this time, so use your best judgment about doing this first), wire a double precision numeric to the number of iterations input. Wire the index output through an increment and then out of the For loop, disabling indexing. Run the VI to show that 3.4 rounds to rounds to rounds to rounds to rounds to 4!

12 Example – Calculations Slope
Arithmetic Operators can be used to compute to perform various calculations and computations Shown below is a code for the slope formula: m = (Y2 – Y1) / (X2 – X1)

13 Hands-on Exercise For your experience, take a few minutes and design the code shown below. This will help you with the familiarization of coding with Arithmetic Operators. Try a few numbers for A,B,C and see how your answers look. Do not spend more than 10 minutes on this exercise.

14 Pretty soon…things will get more complex ;)
Instructor: Build this example subVI slope.vi on your computer and demonstrate creating an icon/connector and calling the VI from another VI along with the next four slides.

15 Pre-Lab Assignment Note: You will be assigned your first Lab Assignment next week. You must complete this pre-lab assignment and demonstrate it prior to starting the lab. The lab assignment will not be given to those who don’t complete the pre-lab, thus resulting to a grade of 0. Pre-Lab Assignment Develop a program that requests the user to enter in a temperature in degrees Celsius (°C). It should then take this value and convert it to degrees Fahrenheit (°F). Your control should be called “Temp (C)” and your indicator should be called “Temp (F)”. Make sure you test your program and confirm that it is accurate. Test using 25 °C, your output should be 77 °F. This pre-lab will be due by the beginning of the next class.


Download ppt "CST-115 Introduction to Computer Programming"

Similar presentations


Ads by Google