310 likes | 416 Views
Assignment Operators. Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) <math.h> math library functions Reading Sections 5A.5. Increment and Decrement Operators. The increment operator ++ The decrement operator --
E N D
Assignment Operators Topics • Increment and Decrement Operators • Assignment Operators • Debugging Tips • rand( ) • <math.h> math library functions Reading • Sections 5A.5
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
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
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.
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.
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.
Code Example Using ++ • #include <stdio.h> • int main ( ) • { • int i = 1 ; • /* count from 1 to 10 */ • while ( i < 11 ) • { • printf (“%d ”, i) ; • i++ ; /* same as ++i */ • } • return 0 ; • }
Same result as above, more concise • # include <stdio.h> • int main ( ) • { • int counter = 0 ; /* initialization */ • while ( ++counter <= 10 ) /* condition & inc. */ • { • printf (“%d ”, counter ) ; • } • return 0; • }
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
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
The Increment expression in the for The following are all equivalent in the incrementing portion of the for structure:counter = counter + 1counter += 1++countercounter++Incrementing occurs after the body of the for is executed, so counter++ seems more appropriate. for ( counter = 1 ; counter < 10 ; counter++ )
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.
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.
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.
A Hand Trace Example • int answer, value = 4 ; • CodeValue Answer 4 garbage • value = value + 1 ; • value++ ; • ++value ; • answer = 2 * value++ ; • answer = ++value / 2 ; • value - - ; • - -value ; • answer = - -value * 2 ; • answer = value - - / 3 ;
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?
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?
Assignment Operators • = += -= *= /= %= • StatementEquivalent 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 ;
Practice with Assignment Operators • int i = 1, j = 2, k = 3, m = 4 ; • ExpressionValue • i += j + k • j *= k = m + 5 • k -= m /= j * 2
Code Example Using /= and ++Counting the Digits in an Integer • #include <stdio.h> • 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 ; • }
Output of the code example: • numdigitsprintftemp • 4327 0 4327 4327 • 1 432 432 • 2 43 43 • 3 4 4 • 4 0 • There are 4 digits in 4327 “Boxes Indicate Printed Output”
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 loop4) The condition that tests for the final value of the control variable. continue?
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. • Confirm that the correct value(s) has been read in. • Check the results of arithmetic computations immediately after they are performed.
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
Commonly Used Header Files header file Contains function prototypes for <stdio.h> the standard input/output library functions & information used by them <math.h> the math library functions <stdlib.h> the conversion of number to text, text to number, memory allocation, random numbers and other utility functions <time.h> manipulating time and date <ctype.h> functions that test characters for certain properties and that can convert case others see page 154 of text
Math Library • double sqrt (double x); • returns the square root of x • double pow (double x, double y) • 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) • 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
Common stdlib functions • void exit (int x); • prematurely ends program execution • void srand (unsigned int x); • “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); • 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 • num = rand( );
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 • num = rand ( ) % 6 • How about 1 to 6? • num = 1 + rand( ) % 6; • How about 5 to 20? • num = 5 + rand ( ) % 16;
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
srand ( ) and rand ( ) #include <stdio.h> Since we are always #include <stdlib.h> using the value 67 to seed the #define SEED 67 generator, 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); } }
<time.h> • 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) ) ;