1 / 9

This code will print a “random” value b etween what and what?

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.

Télécharger la présentation

This code will print a “random” value b etween what and what?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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

  9. 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; }

More Related