90 likes | 187 Views
This code will print a “random” value b etween what and what?. #include < stdio.h > #include < stdlib.h > int main() { int temp; temp = rand()%6; printf ("temp=%d", temp ); }. 2 . Between 0 and 5. 1. Between 1 and 6. 3 . Between 0 and 6. 4 . Between 1 and 5.
E N D
This code will print a “random” value between what and what? #include <stdio.h> #include <stdlib.h> int main() { int temp; temp = rand()%6; printf("temp=%d", temp); } 2. Between 0 and 5 1. Between 1 and 6 3. Between 0 and 6 4. Between 1 and 5
What is a reasonable output for this code? #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } 1. temp=1 temp=4 temp=3 temp=1 temp=5 temp=1 2. temp=0 temp=4 temp=7 temp=1 temp=5 temp=1 4. temp=-1 temp=4 temp=3 temp=10 temp=5 temp=1 3. temp=-1 temp=4 temp=3 temp=1 temp=5 temp=1
If you ran this code multiple times would you get the same answer? #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } YES! Because rand is “pseudorandom” – it needs a different seed number every time to be more random
Will these two pieces of code have the same output? #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; srand(198); /* seed rand */ while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; srand(98); /* seed rand */ while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } NO! Different seed numbers.
Will these two pieces of code have the same output? #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; srand(98); /* seed rand */ while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } #include <stdio.h> #include <stdlib.h> int main() { int temp, counter = 0; srand(98); /* seed rand */ while (counter <= 5) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } Yes! Same seed numbers.
Will this code give me the same ouput if I ran it at 10am versus noon? #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int temp, counter = 0; srand(time(NULL)); /* seed with time */ while (counter <=10) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } It will give you different output because I am seeding it with time.
Do these two codes doing the same thing? #include <stdio.h> #include <stdlib.h> #include <time.h> int silly(void); int main() { int temp, counter = 0; srand(time(NULL)); while (counter <=10) { printf("temp=%d\n", silly()); counter++; } } int silly(void) { return rand()%6; } #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int temp, counter = 0; srand(time(NULL)); while (counter <=10) { temp = rand()%6; printf("temp=%d\n", temp); counter++; } } Yes, the one on the right is just using a function.
Keepin’ ya on your toes…What is the output? #include <stdio.h> #include <stdlib.h> #include <time.h> #define TRUE 1 #define FALSE 0 int main() { int temp, counter = 0, loop=TRUE; srand(time(NULL)); while (loop) { printf("temp=%d\n", rand()%6); counter++; if (counter == 5) { loop = FALSE; } } } 1. temp=3 temp=1 temp=0 temp=5 temp=0 2. temp=0 temp=4 temp=7 temp=1 temp=5 temp=1 4. Never ends 3. temp=-1 temp=4 temp=3 temp=1 temp=5 temp=1
Something you can mull over? Program.c boolean.h #include <stdio.h> #define FALSE 0 #define TRUE 1 #include "boolean.h" int main(void) { int num1, num2; boolean result; printf("Input 2 numbers "); scanf("%d %d", &num1, &num2); if (isMultiple(num1, num2)) { printf("They are multiples\n"); } else { printf("Not multiples\n"); } } /* Define a type called boolean as an int */ #define booleanint /* Define the prototype */ booleanisMultiple(int a, int b); /* The function code */ booleanisMultiple(int a, int b) { int answer; if (a % b == 0) { answer = TRUE; } else { answer = FALSE; } return answer; }