1 / 12

ICS 103 Lab 2-Arithmetic Expressions

ICS 103 Lab 2-Arithmetic Expressions . Lab Objectives. Learn different arithmetic operators Learn how to use arithmetic expressions Learn how to use math functions. Arithmetic operators.

long
Download Presentation

ICS 103 Lab 2-Arithmetic Expressions

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. ICS 103Lab 2-Arithmetic Expressions

  2. Lab Objectives • Learn different arithmetic operators • Learn how to use arithmetic expressions • Learn how to use math functions

  3. Arithmetic operators We need arithmetic operators to solve most programming problems, you will need to write arithmetic expressions that manipulate data types. basic arithmetic operators: (Binary) (Unary) addition (+) plus(+) subtraction (-) negation(-) multiplication (*) division (/) remainder (%)

  4. Binary operators • Each operator has 2 operands. • Operands may be constants, variables, or other arithmetic expressions. • If the operands are of different types, the one with weaker type will be raised to the level of the other type, then the operation will be performed. The order is char, int, long, float, double.

  5. Division • The division between two integer numbers results in an integer result (as you can see 5/2 gives 2 not 2.5). • The division by zero is undefined. Either you get a compilation warning or a run time error.

  6. Remainder • The remainder operator % is related to the division one. This operator will give the remainder produced by dividing the two numbers ( 5 % 2 = 1). Therefore, this operator is not used with double values. If one of the operand or both are double, you will get an error message from the compiler saying: Illegal use of floating point

  7. Arithmetic expressions Arithmetic expressions contain a combination of the arithmetic operations including brackets: x = z – (a + b / 2) + w * -y To evaluate the expression, we need to follow the precedence rule which is as follows: 1. ( ) expression within parentheses 2. +, - unary operators (plus and minus sign) 3. *, /, % multiplication, division and remainder are evaluated from left to right 4. +, - addition and subtraction are evaluated from left to right

  8. Mathematical Functions • To do some advanced mathematical functions in C, there is a header file called <math.h> that has different trigonometric and algebraic functions. Here are some frequently used functions: pow(x,y) xy pow(5,3) = 53 = 125 sqrt(x) (x > 0) sqrt(4) = = 2 log(x) ln x (x > 0) log(5) = 1.6094 log10(x) (x > 0) log10(5) = 0.698970 exp(x) ex exp(2) = 7.3891 sin(x) sin x (x in radian) sin(90) = 0.893997 cos(x) cos x (x in radian) cos(90) = -0.448074 tan(x) tan x (x in radian) tan(90) = -1.9952 asin(x) sin-1 x ( x in [-1,1]) asin (0) = 0 acos(x) cos-1 x (x in [-1,1]) acos(0) = 1.570796 atan(x) tan-1 x atan(0) = 0 • Note that you have to include the header file math.h before you use any of these functions. Also, the return type of these functions is double.

  9. Example • Write a program that finds the area of a triangle given the length of its sides: a, b, c. TEST CASES: a = 2, b = 3, c = 4 AREA= 2.90

  10. Lab Work

  11. Write a program that reads the lengths of two sides of a triangle and the angle between them and calculates the length of the third side using the following formula: • Write a program that reads the volume of a sphere and finds the radius of it. • Write a program to solve the quadratic equation using quadratic formulas: Your Program should prompt the user for the values of a, b and c.

  12. Test cases • Program 1: Input\ Side1 = 10, Side2 = 5, a = 45 Output\ Side3 = 8.51 • Program 2: Input\ V= 100 Output\ r = 2.87 • Program 3: Input\ a = 5 , b = 10 , c = 3 Output\ x1 = -0.36, x2 = -1.63

More Related