Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture-5 Miscellaneous. Random Numbers Can use ‘rand()’ function declared in the stdlib.h header file The seed for random number generation can be set.

Similar presentations


Presentation on theme: "Lecture-5 Miscellaneous. Random Numbers Can use ‘rand()’ function declared in the stdlib.h header file The seed for random number generation can be set."— Presentation transcript:

1 Lecture-5 Miscellaneous

2 Random Numbers Can use ‘rand()’ function declared in the stdlib.h header file The seed for random number generation can be set using srand() A good seed can be the ‘time()’ function defined in time.h header file Follow this link to test_ran.ctest_ran.c

3 In-class Exercise 6-1 Write a program to guess a number Generate a random number from 1 to 10 using: 1 + rand() % 10 Prompt the user to enter a number(from 1 to 10), find out of the number is equal to, less than, or greater than the random number Keep prompting user till he enters a negative value.

4 Decimal Number System Numbers with the base value of 10 Digits used: {0, 1, 2,…, 9} 1362: 1*10 3 + 3*10 2 + 6*10 1 + 2*10 0 In general, any number with ‘n’ number of digits can be represented as: d n.10 n +d n-1.10 n-1 +….d 0.10 0

5 Binary Numbers Numbers represented with base value of 2 Only two digits used: {0, 1} Binary digits are frequently called “bits”, and can be visualized as an “on-off” operation for a electronic switch 1011: 1.2 3 + 0.2 2 + 1.2 1 + 1.2 0 1011: 8 + 0 + 2 + 1 = 11(decimal)

6 Bit Operations C has bit operators with operate on integers as bit patterns Each bit from each integer is acted upon separately The answer is presented in integer format Follow this link to bit_opr.cbit_opr.c

7 Operator table XYAND(X&Y)OR(X|Y)XOR(X^Y) 00000 01011 10011 11110 Complement Operator(!): Inverts value Thus: !1 = 0, and !0 = 1

8 Shift operators Shift operators can shift bits of a variable either left or right Shifting bits to left is equivalent to multiplying by 2 Shifting bits to right is equivalent to dividing by 2 Follow this link to bit_shift.cbit_shift.c

9 In-class Exercise 6-2 Write a program to print out the multiplication table of 2 Use bit-shift operators for each number to multiply the number by two, and then display it. Use the range of number from 1 to 10


Download ppt "Lecture-5 Miscellaneous. Random Numbers Can use ‘rand()’ function declared in the stdlib.h header file The seed for random number generation can be set."

Similar presentations


Ads by Google