Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <mmsystem.h>
  4. #include <time.h>
  5. #include <iostream>
  6. #include <ctime>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. int n = 0;
  11. int i = 0;
  12.  
  13. int binarySearch(int *list1, int first, int last, int value) {
  14. cout << "please enter a value to search = ";
  15. cin >> value;
  16. int answer = -1;
  17.  
  18. if (first <= last)
  19. {
  20. Sleep(1);
  21. int mid = (first + last) / 2;
  22.  
  23. if (list1[mid] == value)
  24. {
  25. answer = mid;
  26. cout << "value found, the position in the array is" << mid << endl;
  27. cout << "value found, the value of the array is " << list1[mid] << endl;
  28. }
  29.  
  30. else
  31. if (list1[mid] > value)
  32.  
  33. answer = binarySearch(list1, first, mid - 1, value);
  34.  
  35.  
  36. else answer = binarySearch(list1, mid + 1, last, value);
  37.  
  38.  
  39. }
  40. else
  41. answer = -1;
  42.  
  43. return answer;
  44.  
  45. }
  46.  
  47. void findAverage(int* list1) {
  48. int total = 0;
  49. double average = 0;
  50. clock_t begin = clock();
  51. for (i = 0; i < n; i++) { total += list1[i]; Sleep(1); }
  52. average = total / (float)n;
  53. clock_t end = clock();
  54. double elapsed = end - begin;
  55. cout << "average of the array is " << average << endl;
  56. cout << "time taken to calculate average is " << elapsed << " milliseconds " << elapsed / 1000 << "seconds" << endl;
  57.  
  58. }
  59.  
  60. void main() {
  61. #pragma comment(lib, "winmm.lib")
  62.  
  63.  
  64. cout << "please enter the amount of integers you want to generate " << endl;
  65. cin >> n;
  66. clock_t begin1 = clock();
  67. cout << endl; cout << "generating first array of sorted integers" << endl;
  68. int* list1 = new int [n];
  69. for (int i = 0; i < n; i++) {
  70. list1[i] = i * 3 + 1;
  71. cout << " " << list1[i];
  72. Sleep(1);
  73. }
  74. clock_t end1 = clock();
  75. cout << endl; cout << "generating 2nd array of random integers" << endl;
  76. clock_t begin2 = clock();
  77. int* list2 = new int[n];
  78. for (int i = 0; i < n; i++) {
  79. list2[i] = rand() % 10000;
  80. cout << " " << list2[i];
  81. Sleep(1);
  82.  
  83. }
  84.  
  85.  
  86. clock_t end2 = clock();
  87. cout << endl;
  88. double elapsed1 = end1 - begin1;
  89. double elapsed2 = end1 - begin1;
  90. cout << "time taken to generate sorted array " << elapsed1 << " milliseconds " << elapsed1 / 1000 << "seconds" << endl;
  91. cout << "time taken to generate random array " << elapsed2 << " milliseconds " << elapsed2 / 1000 << "seconds" << endl;
  92. findAverage(list1);
  93.  
  94. binarySearch(list1, 0, n-1, -1);
  95.  
  96.  
  97. system("PAUSE");
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement