Presentation is loading. Please wait.

Presentation is loading. Please wait.

L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.

Similar presentations


Presentation on theme: "L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections."— Presentation transcript:

1 L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections 5A.5

2 L132 Increment and Decrement Operators The increment operator ++ The decrement operator -- Precedence: lower than (), but higher than * / and % Associativity: right to left Increment and decrement operators can only be applied to variables, NOT to constants or expressions

3 L133 Precedence of Operators in this Lect. ( )left to right ++ -- right to left * / %left to right + - left to right >=left to right == !=left to right = += -= *= /= %= right to left

4 L134 Increment Operator If we want to add one to a variable, we can say: count = count + 1 ; Programs often contain statements that increment variables, so to save on typing, C provides these shortcuts: count++ ; OR ++count ; Both do the same thing. They change the value of count by adding one to it.

5 L135 Post-Increment Operator The position of the ++ determines when the value is incremented. If the ++ is after the variable, then the incrementing is done last (a postincrement). int amount, count ; count = 3 ; amount = 2 * count++ ; amount gets the value of 2 * 3, which is 6, and then 1 gets added to count. So, after executing the last line, amount is 6 and count is 4.

6 L136 Pre-Increment Operator If the ++ is before the variable, then the incrementing is done first (a preincrement). int amount, count ; count = 3 ; amount = 2 * ++count ; 1 gets added to count first, then amount gets the value of 2 * 4, which is 8. So, after executing the last line, amount is 8 and count is 4.

7 L137 Code Example Using ++ #include int main ( ) { int i = 1 ; /* count from 1 to 10 */ while ( i < 11 ) { printf (“%d ”, i) ; i++ ; /* same as ++i */ } return 0 ; }

8 L138 Same result as above, more concise # include int main ( ) { int counter = 0 ; /* initialization */ while ( ++counter <= 10 ) /* condition & inc. */ { printf (“%d ”, counter ) ; } return 0; }

9 L139 A while loop with ++i i=0; while ( + + i < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10

10 L1310 A while loop with i++ i=0; while ( i ++ < 11 ) { printf(“%d ”, i ); } Output is: 1 2 3 4 5 6 7 8 9 10 11

11 L1311 The Increment expression in the for The following are all equivalent in the incrementing portion of the for structure: counter = counter + 1 counter += 1 ++counter counter++ Incrementing occurs after the body of the for is executed, so counter++ seems more appropriate. for ( counter = 1 ; counter < 10 ; counter++ )

12 L1312 Decrement Operator If we want to subtract one from a variable, we can say: count = count - 1 ; Programs often contain statements that decrement variables, so to save on typing, C provides these shortcuts: count-- ; OR --count ; Both do the same thing. They change the value of count by subtracting one from it.

13 L1313 Post-Decrement Operator The position of the -- determines when the value is decremented. If the -- is after the variable, then the decrementing is done last (a postdecrement). int amount, count ; count = 3 ; amount = 2 * count-- ; amount gets the value of 2 * 3, which is 6, and then 1 gets subtracted from count. So, after executing the last line, amount is 6 and count is 2.

14 L1314 Pre-Decrement Operator If the -- is before the variable, then the decrementing is done first (a predecrement). int amount, count ; count = 3 ; amount = 2 * --count ; 1 gets subtracted from count first, then amount gets the value of 2 * 2, which is 4. So, after executing the last line, amount is 4 and count is 2.

15 L1315 A Hand Trace Example int answer, value = 4 ; Code Value Answer 4garbage value = value + 1 ; value++ ; ++value ; answer = 2 * value++ ; answer = ++value / 2 ; value - - ; - -value ; answer = - -value * 2 ; answer = value - - / 3 ;

16 L1316 Practice Given int a = 1, b = 2, c = 3 ; What is the value of this expression? ++a * b - c - - What are the new values of a, b, and c?

17 L1317 More Practice Given int a = 1, b = 2, c = 3, d = 4 ; What is the value of this expression? ++b / c + a * d++ What are the new values of a, b, c, and d?

18 L1318 Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a = a % 2 ;a %= 2 ; b = b + ( c + 2 ) ;b += c + 2 ; d = d * ( e - 5 ) ;d *= e - 5 ;

19 L1319 Practice with Assignment Operators int i = 1, j = 2, k = 3, m = 4 ; Expression Value i += j + k j *= k = m + 5 k -= m /= j * 2

20 L1320 Code Example Using /= and ++ Counting the Digits in an Integer #include int main ( ) { int num, temp, digits = 0 ; temp = num = 4327 ; while ( temp > 0 ) { printf (“ %d \n ”, temp) ; temp /= 10 ; digits++ ; } printf (“There are %d digits in %d.\n”, digits, num) ; return 0 ; }

21 L1321 Output of the code example: num digits printf temp 4327 04327 4327 1 432 432 2 43 43 3 4 4 4 0 There are 4 digits in 4327 “Boxes Indicate Printed Output”

22 L1322 Counter Controlled Repetition requires: 1) The name of a control variable (loop counter) 2) The initial value of the control variable. 3) The increment (or decrement) by which the control variable is modified each time through the loop 4) The condition that tests for the final value of the control variable. continue?

23 L1323 Debugging Tips Trace your code by hand (a hand trace), keeping track of the value of each variable. Insert temporary printf() statements so you can see what your program is doing. o Confirm that the correct value(s) has been read in. o Check the results of arithmetic computations immediately after they are performed.

24 L1324 Header Files Header files contain function prototypes for all of the functions found in the corresponding library It also contains definitions of constants and data types used in that library

25 L1325 Commonly Used Header Files header fileContains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions manipulating time and date functions that test characters for certain properties and that can convert case otherssee page 154 of text

26 L1326 Math Library double sqrt (double x); o returns the square root of x double pow (double x, double y) o x raised to the y power pow (3.0, 2.0) is 9.0 pow (8.0, 1.0 / 3) is 2.0 double sin (double x) o trigonometric sine of x (x in radians) All math library functions take doubles as arguments and return doubles others on page 145 - 146 of text

27 L1327 Common stdlib functions void exit (int x); o prematurely ends program execution void srand (unsigned int x); o “seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudo- random number srand (200); int rand (void); o returns an unsigned pseudo-random integer in the range of 0 to 65535 or 0 to 4294967295 depending on the size of an integer on the system your on o num = rand( );

28 L1328 Manipulating what rand() returns Since rand( ) returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes Suppose we want only random numbers in the range from 0 to 5 o num = rand ( ) % 6 How about 1 to 6? o num = 1 + rand( ) % 6; How about 5 to 20? o num = 5 + rand ( ) % 16;

29 L1329 srand ( ) and rand ( ) The pseudo-random number generator needs an unsigned int as it’s seed Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers To get different random numbers each time we run our program, we have to give a different seed each time

30 L1330 srand ( ) and rand ( ) #include Since we are always #include using the value 67 to seed the #define SEED 67generator, the same numbers will be produced whenever we main ( ) run our program. { int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%d\n”, num); }

31 L1331 One of the most useful functions in the time library is the time( ) function It returns the time of day as seconds Since this number is different every time we call it, a common use is as a seed for the random number generator Each time we run our program, a different sequence of random numbers will be produced srand (time ( NULL) ) ;


Download ppt "L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections."

Similar presentations


Ads by Google