/
This code will print a “random” value This code will print a “random” value

This code will print a “random” value - PowerPoint Presentation

fauna
fauna . @fauna
Follow
65 views
Uploaded On 2023-11-07

This code will print a “random” value - PPT Presentation

b etween what and what include lt stdioh gt include lt stdlibh gt int main int temp temp rand6 printf tempd temp 1 Between 1 and 6 ID: 1029823

int temp include counter temp int counter include rand printf stdio main time stdlib srand seed code define boolean

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "This code will print a “random” valu..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. This code will print a “random” valuebetween what and what?#include <stdio.h>#include <stdlib.h>int main(){ int temp; temp = rand()%6; printf("temp=%d", temp);}1. Between 1 and 62. Between 0 and 53. Between 0 and 64. 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=12. temp=0 temp=4 temp=7 temp=1 temp=5 temp=13. temp=-1 temp=4 temp=3 temp=1 temp=5 temp=14. temp=-1 temp=4 temp=3 temp=10 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(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(198); /* 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 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.#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;}

8. Keepin’ ya on your toes…What is the output?#include <stdio.h>#include <stdlib.h>#include <time.h>#define TRUE 1#define FALSE 0int 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=02. temp=0 temp=4 temp=7 temp=1 temp=5 temp=13. temp=-1 temp=4 temp=3 temp=1 temp=5 temp=14. Never ends

9. Something you can mull over?#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 boolean int/* Define the prototype */boolean isMultiple(int a, int b);/* The function code */boolean isMultiple(int a, int b) { int answer; if (a % b == 0) { answer = TRUE; } else { answer = FALSE; } return answer;}Program.cboolean.h