fork download
  1. //Devin Scheu CS1A Chapter 9, P. 538, #8
  2. //
  3. /**************************************************************
  4. *
  5. * DETERMINE MODE OF INTEGER ARRAY
  6. * ____________________________________________________________
  7. * This program finds the mode of a set of integer values
  8. * in an array, which is the value occurring most frequently.
  9. * ____________________________________________________________
  10. * INPUT
  11. * arraySize : The number of elements in the array
  12. * values : The array of integers
  13. *
  14. * OUTPUT
  15. * modeValue : The mode of the array or -1 if no mode exists
  16. *
  17. **************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21.  
  22. using namespace std;
  23.  
  24. // Function to find mode using pointer notation
  25. int findMode(int* arr, int size) {
  26. if (size <= 0) return -1;
  27.  
  28. int* count = new int[1000](); // Assume max value is 999, initialize to 0
  29. int maxCount = 0; // Track the highest frequency
  30. int modeValue = -1; // Track the mode value
  31.  
  32. // Count frequency of each value and find the mode
  33. for (int* ptr = arr; ptr < arr + size; ptr++) {
  34. count[*ptr]++;
  35. if (count[*ptr] > maxCount) {
  36. maxCount = count[*ptr];
  37. modeValue = *ptr; // Set mode to the first value with the new max count
  38. }
  39. }
  40.  
  41. // Check if a mode exists (more than one occurrence)
  42. if (maxCount <= 1) {
  43. delete[] count;
  44. return -1;
  45. }
  46.  
  47. // Count unique values with max count using a flag array
  48. bool* seen = new bool[1000](); // Flag to mark seen values with max count
  49. for (int i = 0; i < 1000; i++) seen[i] = false;
  50. int uniqueMaxCount = 0;
  51.  
  52. for (int* ptr = arr; ptr < arr + size; ptr++) {
  53. if (count[*ptr] == maxCount && !seen[*ptr]) {
  54. uniqueMaxCount++;
  55. seen[*ptr] = true;
  56. if (uniqueMaxCount > 1) {
  57. delete[] count;
  58. delete[] seen;
  59. return -1; // Multiple unique modes, no single mode
  60. }
  61. }
  62. }
  63.  
  64. delete[] count;
  65. delete[] seen;
  66. return modeValue;
  67. }
  68.  
  69. int main () {
  70.  
  71. //Variable Declarations
  72. int arraySize; //INPUT - The number of elements in the array
  73. int* values; //INPUT - The array of integers
  74. int modeValue; //OUTPUT - The mode of the array or -1 if no mode exists
  75.  
  76. //Prompt for Input
  77. cout << "Enter the number of elements: ";
  78. cin >> arraySize;
  79. cout << arraySize << endl;
  80.  
  81. //Allocate dynamic array
  82. values = new int[arraySize];
  83.  
  84. //Input Values
  85. for (int i = 0; i < arraySize; i++) {
  86. cout << "Enter value " << (i + 1) << ": ";
  87. cin >> *(values + i);
  88. while (*(values + i) < 0) {
  89. cout << "\nError: Please enter a non-negative value: ";
  90. cin >> *(values + i);
  91. }
  92. cout << *(values + i) << endl;
  93. }
  94.  
  95. //Calculate Mode
  96. modeValue = findMode(values, arraySize);
  97.  
  98. //Separator and Output Section
  99. cout << "-------------------------------------------------------" << endl;
  100. cout << "OUTPUT:" << endl;
  101.  
  102. //Output Result
  103. if (modeValue == -1) {
  104. cout << left << setw(25) << "Mode:" << right << setw(15) << "No mode (-1)" << endl;
  105. } else {
  106. cout << left << setw(25) << "Mode:" << right << setw(15) << modeValue << endl;
  107. }
  108.  
  109. //Clean up memory
  110. delete[] values;
  111.  
  112. } //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:
Mode:                                  4