Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. /***************************************************************
  2. CSCI 240 Program 1 Spring 2019
  3.  
  4. Programmer: Debojit Roy
  5.  
  6. Section: 1
  7.  
  8. Date Due: 2/19/2019
  9.  
  10. Purpose: random number generator
  11. ***************************************************************/
  12. #include <iostream>
  13. #include <iomanip>
  14. #include <cstdlib>
  15. #include <ctime>
  16. using namespace std;
  17.  
  18. int main()
  19.  
  20. {
  21. int j, high, low, val, sum;
  22. double aveg, mycou;
  23. #define MAX_NUM 10
  24. #define MIN_NUM 2
  25. #define MAX_RANGE 50
  26. cout << fixed << setprecision(2);
  27. srand(24);
  28. sum=0;
  29. mycou = MIN_NUM + (rand() % (MAX_NUM - MIN_NUM + 1));
  30. cout << "The for loop will generate 10 numbers ... " << endl << endl;
  31. cout << "The numbers are ";
  32. //for loop
  33. for(j=1 ; j <= mycou ; j++)
  34. {
  35. val = rand() % 50+1;
  36. cout << " " << val;
  37. sum += val;
  38. aveg = sum/mycou;
  39. if (j==1)
  40. {
  41. high=val;
  42. low=val;
  43. }
  44. if(val > high)
  45. {
  46. high = val;
  47. }
  48. if( val < low)
  49. {
  50. low = val;
  51. }
  52. }
  53. cout << endl << "Smallest: " << setw(20) << low <<endl;
  54. cout << "Largest: " << setw(20) << high << endl;
  55. cout << "Sum: " << setw(24) << sum << endl;
  56. cout << "Average: " << setw(20) << aveg << endl;
  57.  
  58.  
  59.  
  60.  
  61. //while loop
  62.  
  63. mycou = MIN_NUM + (rand() % (MAX_NUM - MIN_NUM + 1));
  64. cout << "The for loop will generate 10 numbers ... " << endl << endl;
  65. cout << "The numbers are ";
  66. j = 1;
  67. while (j <= mycou )
  68. {
  69. val = rand() % 50+1;
  70. cout << " " << val;
  71. sum += val;
  72. aveg = sum/mycou;
  73. j++;
  74. if (j==1)
  75. {
  76. high=val;
  77. low=val;
  78. }
  79. if(val > high)
  80. {
  81. high = val;
  82. }
  83. if( val < low)
  84. {
  85. low = val;
  86. }
  87. }
  88. cout << endl << "Smallest: " << setw(20) << low <<endl;
  89. cout << "Largest: " << setw(20) << high << endl;
  90. cout << "Sum: " << setw(24) << sum << endl;
  91. cout << "Average: " << setw(20) << aveg << endl;
  92.  
  93. //do while loop
  94. mycou = MIN_NUM + (rand() % (MAX_NUM - MIN_NUM + 1));
  95. cout << "The for loop will generate 10 numbers ... " << endl << endl;
  96. cout << "The numbers are ";
  97. j = 1;
  98. do
  99. {
  100. val = rand() % 50+1;
  101. cout << " " << val;
  102. sum += val;
  103. aveg = sum/mycou;
  104. j++;
  105. }
  106. while (j <=mycou);
  107. if (j==1)
  108. {
  109. high=val;
  110. low=val;
  111. }
  112. if(val > high)
  113. {
  114. high = val;
  115. }
  116. if( val < low)
  117. {
  118. low = val;
  119. }
  120. cout << endl << "Smallest: " << setw(20) << low <<endl;
  121. cout << "Largest: " << setw(20) << high << endl;
  122. cout << "Sum: " << setw(24) << sum << endl;
  123. cout << "Average: " << setw(20) << aveg << endl;
  124.  
  125.  
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement