fork download
  1. //Devin Scheu CS1A Chapter 9, P. 538, #9
  2. //
  3. /**************************************************************
  4. *
  5. * DETERMINE MEDIAN OF SORTED INTEGER ARRAY
  6. * ____________________________________________________________
  7. * This program determines the median of a set of integer values
  8. * in a sorted array.
  9. * ____________________________________________________________
  10. * INPUT
  11. * arraySize : The number of elements in the array
  12. * values : The array of integers
  13. *
  14. * OUTPUT
  15. * medianValue : The median of the array
  16. *
  17. **************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21.  
  22. using namespace std;
  23.  
  24. // Function to swap two integers using pointers
  25. void swap(int* a, int* b) {
  26. int temp = *a;
  27. *a = *b;
  28. *b = temp;
  29. }
  30.  
  31. // Function to perform bubble sort using pointers
  32. void bubbleSort(int* arr, int size) {
  33. for (int i = 0; i < size - 1; i++) {
  34. for (int j = 0; j < size - i - 1; j++) {
  35. if (*(arr + j) > *(arr + j + 1)) {
  36. swap(arr + j, arr + j + 1);
  37. }
  38. }
  39. }
  40. }
  41.  
  42. // Function to find median using pointer notation
  43. double findMedian(int* arr, int size) {
  44. if (size % 2 == 1) {
  45. return static_cast<double>(*(arr + size / 2));
  46. } else {
  47. double mid1 = static_cast<double>(*(arr + size / 2 - 1));
  48. double mid2 = static_cast<double>(*(arr + size / 2));
  49. return (mid1 + mid2) / 2.0;
  50. }
  51. }
  52.  
  53. int main () {
  54.  
  55. //Variable Declarations
  56. int arraySize; //INPUT - The number of elements in the array
  57. int* values; //INPUT - The array of integers
  58. double medianValue; //OUTPUT - The median of the array
  59.  
  60. //Prompt for Input
  61. cout << "Enter the number of elements: ";
  62. cin >> arraySize;
  63. cout << arraySize << endl;
  64.  
  65. while (arraySize <= 0) {
  66. cout << "\nError: Please enter a positive number: ";
  67. cin >> arraySize;
  68. cout << arraySize << endl;
  69. }
  70.  
  71. //Allocate dynamic array
  72. values = new int[arraySize];
  73.  
  74. //Input Values
  75. for (int i = 0; i < arraySize; i++) {
  76. cout << "Enter value " << (i + 1) << ": ";
  77. cin >> *(values + i);
  78. cout << *(values + i) << endl;
  79. }
  80.  
  81. //Sort the array
  82. bubbleSort(values, arraySize);
  83.  
  84. //Calculate Median
  85. medianValue = findMedian(values, arraySize);
  86.  
  87. //Separator and Output Section
  88. cout << "-------------------------------------------------------" << endl;
  89. cout << "OUTPUT:" << endl;
  90.  
  91. //Output Result
  92. cout << fixed << setprecision(2);
  93. cout << left << setw(25) << "Median:" << right << setw(15) << medianValue << endl;
  94.  
  95. //Clean up memory
  96. delete[] values;
  97.  
  98. } //end of main()
Success #stdin #stdout 0.01s 5308KB
stdin
6
1
2
2
4
4
4
stdout
Enter the number of elements: 6
Enter value 1: 1
Enter value 2: 2
Enter value 3: 2
Enter value 4: 4
Enter value 5: 4
Enter value 6: 4
-------------------------------------------------------
OUTPUT:
Median:                             3.00