Advertisement
balrougelive

Untitled

Aug 10th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. /*
  2. Homework 7
  3.  
  4. Write a C program that generates two random numbers.
  5.  
  6. Print both numbers that are generated.
  7.  
  8. Then use a ternary (or conditional) operator to identify the largest of the 2 numbers and print the result.
  9.  
  10. Seed the random number generator so the numbers generated are not the same every time.
  11.  
  12. Submit the .c file only.
  13.  
  14. Sample Run:
  15.  
  16. Random 1:8809
  17. Random 2:27141
  18. The max of 8809 and 27141 is 27141.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h> // void srand(unsigned int); - int rand(); - NULL - RAND_MAX,
  23. #include <time.h> // time_t time(time_t*) - NULL
  24.  
  25. int main()
  26. {
  27. srand((unsigned int)time(NULL)); // seeds srand with the number of seconds since January 1st 1970
  28. int rand1 = rand(); //init the vars to random
  29. int rand2 = rand(); //init the vars to random
  30. int max = rand1 > rand2 ? rand1 : rand2; //Ternary (three) conditional operator which sets max = rand1 if true and max = rand2 if false
  31. printf("Random 1: %d \n", rand1);
  32. printf("Random 1: %d \n", rand2);
  33. printf("The max of %d and %d is %d \n", rand1, rand2, max);
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement