Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.

Similar presentations


Presentation on theme: "1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik."— Presentation transcript:

1 1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik

2 2 Lecture Three Outline Scientific Notation of Floats Output Formatting Integer formatting Double formatting Eliminating leading blanks Programming Examples Arithmetic Operators Shortcut assignment Prefix form Postfix form

3 3 Lecture Three Outline Arithmetic Expression Precedence rules Evaluate the expression Assignment operator syntax What is stored? example-1,example-2 Explicit Type Conversion Math in C Simple equations Complex math Function Math library Math library examples

4 4 Scientific Notation for floats 2.7E4 means 2.7 x 10 4 = 2.7000 = 27000.0 2.7E-4 means 2.7 x 10 - 4 = 0002.7 = 0.00027

5 5 Output Formatting In C, you can control the APPEARANCE of number on the screen. Output formatting does not affect the VALUE of a variable, just how it appears to the user on the screen

6 6 Output Formatting Integer formatting Very simple: Add a number between the % and the d in the placeholder to specify the “field length”. Numbers will appear right-justified with preceding blanks if needed.

7 7 Integer Formatting Example int len = 234 ; printf(“ Length is %5d “, len); Length is  234 Output is: Note: The  stands for a blank

8 8 Integer Formatting Displaying X using different %d placeholder

9 9 Output Formatting Double formatting We must indicate both the field width and the EXACT number of decimal places: %7.3 f minimum total field length Exact number of decimal digits Note: The decimal part will be rounded The whole part may be padded with blanks REMEMBER: The value of the number does not change, only its appearance

10 10 Double Formatting Displaying X using different %6.2f placeholder

11 11 Output Formatting Eliminating Leading Blanks The simple placeholder %d will cause an integer value to be displayed with no leading blanks. A placeholder of the form %.mf has the same effect for values of type double.

12 12 Programming Examples Example-1 Write a program to ask the user for the width and length of a piece of land and then tell him how many orange trees he can grow on it. Given that each orange tree requires 4 m 2.

13 13 #include # define one_tree_space 4 int main(void) { int length,width, area, no_of_tree; printf(“Enter length of the land> ”); scanf(“%d”, &length); printf(“Enter width of the land> ”); scanf(“%d”, &width); area = length * width; no_of_tree = area / one_tree_space; printf(“The available number of trees is %d tress\n”, no_of_tree); return(0); } Programming Examples Example-1

14 14 Write a program to ask the user for the radius of a circle, and then display its area and circumference, displayed to 3 decimal digits. Programming Examples Example-2

15 15 #include # define PI 3.141593 int main(void) { double radius, area, circumference; printf(“Enterradius of the circle> ”); scanf(“%lf”, &radius); area = PI * radius * radius; circumference = 2 * PI * radius; printf(“The area of the circle = %.3f\n”, area); printf(“The circumference of the circle = %.3f\n”, circumference); return(0); } Programming Examples Example-2

16 16 Arithmetic Operators Arithmetic Operator MeaningExamples +addition5+2 is 7 5.0+2.0 is 7.0 -subtraction5-2 is 3 5.0–2.0 is 3.0 *multiplication5*2 is 10 5.0*2.0 is 10.0 /division5.0/2.0 is 2.5 5/2 is 2 %remainder5%2 is 1

17 17 Arithmetic Operators Division the result of the division operator depends on the type of its operands if one or both operands has a floating point type, the result is a floating point type. Otherwise, the result is an integer type Examples 11 / 4 has value 2 11.0 / 4.0 has value 2.75 11 / 4.0 has value 2.75 15 / 3 has value 5 16 / 3 has value 5

18 18 the modulus operator % can only be used with integer type operands and always has an integer type result its result is the integer type remainder of an integer division EXAMPLE 3 % 5 = 35 % 3 = 24 % 5 = 45 % 4 = 1 5 % 5 = 06 % 5 = 17 % 5 = 28 % 5 = 3 15 % 6 = 315 % 5 = 0 Arithmetic Operators Modulus

19 19 Arithmetic Operators Shortcut assignment

20 20 Prefix operator Arithmetic Operators Prefix form

21 21 Arithmetic Operators Postfix form

22 22 Arithmetic Expression An expression is a valid arrangement of variables, constants, and operators. in C each expression can be evaluated to compute a value of a given type the value of the expression 9.3 * 4.5 is 41.85

23 23 Arithmetic Expression Precedence Rules 1) Brackets : Left to right, inner to outer 2) Unary, Multiplication, division, modulus: left to right 3) Addition and subtraction : Left to right Binary Operators in the same level (such as + and -) are of equal priority and are evaluated left to right. Unary Operators in the same level (such as + and -) are of equal priority and are evaluated right to left.

24 24 z – (a + b / 2) + w * -y Given z = 8, a = 3, b = 9, w = 2, y = -5 8 – (3 + 9 / 2) + 2 * - -5 (Step-1)9/2 = 4 8 – (3 + 4) + 2 * - -5 (Step-2)(3+4) = 7 8 – 7 + 2 * - -5 (Step-3)- - 5 = 5 8 – 7 + 2 * 5 (Step-4)2 * 5 = 10 8 – 7 + 10 (Step-5) 8 – 7 = 1 1 + 10 (Step-6) 1 + 10 = 11 11 Arithmetic Expression Evaluate the expression

25 25 Variable = Expression 1)first, Expression on right is evaluated 2)then the resulting value is stored in the memory location of Variable on left NOTE: An automatic type conversion occurs after evaluation but before the value is stored if the types differ for Expression and Variable Arithmetic Expression Assignment operator syntax

26 26 ? float someFloat; someFloat someFloat = 12; // causes implicit type conversion someFloat 12.0 Arithmetic Expression What is stored?

27 27 ? int someInt; someInt someInt = 4.8; // causes implicit type conversion someInt 4 Arithmetic Expression What is stored?

28 28 Explicit Type Conversion (int)4.8has value4 (float)5has value5.0 (float)(7/4)has value1.0 (float)7 / (float)4has value1.75

29 29 Using Casts to Prevent Integer Division (Example) #include int main(void) { int total_score, num_students; double average; printf(“Enter sum of students’ scores> ”); scanf(“%d”, &total_score); printf(“Enter sum of students> ”); scanf(“%d”, &num_students); average = (double) total_score / (double) num_students; printf(“Average score is %.2f\n”, average); return(0); }

30 30 b 2 - 4ac a+b c+d 1 1 + x 2 Math in C Simple equations

31 31 How do you do |x| ? y ? Z 34 ? C provides no operators for that ! However, we can use some ready-made (predefined) functions to do them. Math in C Complex math

32 32 First: What is a function? A subprogram used to do a certain task. A function has zero or more inputs ( called parameters), and zero or one output (called return value) We will study functions in more detail later. function Inputs Output Math in C Functions

33 33 The C math library provides a lot of useful predefined math functions Before you use them, remember to include the math library in your code: #include function sqrt: y = sqrt ( x ); Math in C Math library

34 34 Math in C Examples of Math functions

35 35 Write a program to get the roots of a quadratic equation, given the 3 coefficients a, b, and c, a x 2 + b x + c = 0 Root 1 = Root 2 = disc = pow(b,2) – 4 * a * c; root_1 = (-b + sqrt(disc)) / (2 * a); root_2 = (-b - sqrt(disc)) / (2 * a); Math in C complex math example-1

36 36 Write a program to get the third side of a triangle (a), given the lengths of the other two sides (b, and c), and the angle  using the formula a 2 = b 2 + c 2 – 2bc cos  b rad_angle = alpha * PI / 180; a = sqrt(pow(b,2) + pow(c,2) – 2 * b * c * cos(rad_angle); c  a Math in C complex math example-2


Download ppt "1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik."

Similar presentations


Ads by Google