220 likes | 291 Views
Chapter 5. 5.1 INTRODUCTION Data Types and Arithmetic Calculations. 5.2 BASIC ARITHMETIC OPERATORS. Unary arithmetic operators require one operand. Unary plus +, unary minus -, increment operator ++, decrement operator --. Binary arithmetic operators require two operands.
E N D
Chapter 5 • 5.1 INTRODUCTION • Data Types and Arithmetic Calculations.
5.2 BASIC ARITHMETIC OPERATORS • Unary arithmetic operators require one operand. • Unary plus +, • unary minus -, • increment operator ++, • decrement operator --.
Binary arithmetic operators require two operands. • The computer computes an integer value for an expression in which all operands are integer.
5.3 INCREMENT AND DECREMENT OPERATORS • ++ and --, • count ++; • ++ count; • count --; • -- count;
count=3 • print f (“%d”, count ++); • This statements print 3. • print f (“%d”, ++ count); • statements print 4.
5.4 COMPOUND ASSIGNMENT OPERATORS • += -= *= /= %= • count += first; • count = count + first • They are equivalent.
5.5 TYPE OF ARITHMETIC EXPRESSIONS • Example 5.8 • double first = 4.7, • int third = 27, • First + third; • Computes to 31.7, • third is converted to a floating-point value
Example 5.10 • Third = first + third; • A value of 31.7 is computed. • However, because the variable third is of type int, • The value of the assignment is 31.
Example 5.11 • X = a / 3 + b; • (a / 3) is evaluated first. • Result is 2. • The value computed for the expression is 14.5,
Explicit Type Conversions: The Cast Operator and Casting • The remainder operator requires integer operands. • Cast operator changes only the type of its value in temporary storage. No_of fifties = (int) amount /50; Remnant = ((int) (amount * 100) % 5000) / 100.0;
5.6 MATHEMATICAL LIBRARY FUNCTIONS • Floating-point type mathematical function declarations need the directive. • Integer type mathematical functions require • The pair of parentheses, (), is known as the function call operator. #include <math.h> #include <stdlib.h>.
Ceil (x) • Floor (x) • Abs (x) • Fabs (x) • Sqrt (x) • Pow (x, y)
Sin (x) • Tan (x) • Exp (x) • Log (x) • Log 10 (x) • Figure 5.1 Some important mathematical library functions
5.7 EXAMPLE PROGRAM 1:A C Program that • Solves Quadratic Equations • Requirements Specification: • Analysis: ax*x + bx+c=0 root1= root2=
Integer Data Types • Int : the range of –32768 through 32767 • Long integer : the range of -2,147,483,648 through 2,147,483,647. • Float : 1.175494e-38 to 3.402823e+38, • Long double: 2.22507e-308 to 1.79769e+308
5.10 ARITHMETIC ERRORS AND INAACCURACIES • Important arithmetic errors • 1.Division by zero • 2.Arithmetic overflow • 3.Arithmetic underflow
5.11 AUTOMATIC CONVERSION OF NUMERIC DATA TYPES • Arithmetic Conversions • The “shorter” type is converted to the “longer” type.
5.12 C FEATURES FOR RANDOM NUMBER GENERATION • A random number generator is an algorithm that generates a sequence of numbers in a given range with no repeating cycles. • The first random number depends on an initial value, called the seed.
Rand and srand functions are in the stdlib.h header file. • Rand returns a uniform pseudorandom number in the range zero through RAND_MAX. • RAND_MAX is also found in the header file stdlib.h.
Every time execute program in the same environment get the same result because the function rand is a pseudorandom number generator. • Srand function is to provide a seed for the next generation of a pseudorandom number. • Time (0) is the current time.
#include <stdio.h> • #include <stdlib.h> • #include <time.h>
int main (void) { • int I = 1; • unsigned int seed; • seed = time (0); • srand (seed); • while (i <= 5) { • print f (“Throw #%d: %d\n”, I, (rand() $ 6+1)); • I++; • } /* end while */ • return 0; • } /* end function main */