he function should determine the mode of the array or modes. That is, it should determine which value or values in the array occurs most often. The mode is the value or values the function should return. If the array has no mode (none of the values occur more than once), the function should return an empty vector. (Assume the array will always contain nonnegative values). Write functions that fill the array with random numbers. Test your function thoroughly. Outcome Criteria 1) program compiles 2) program solves problem according to specification 3) program declares, creates, or initializes static or dynamic array correctly 4) if required program defines function or functions with array parameters or array returns 5) program uses arrays to solve problem 6) program destroys any dynamic arrays

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter14: Exception Handling
Section: Chapter Questions
Problem 11SA
icon
Related questions
icon
Concept explainers
Question

Hello, can you please help me. Can you check the code below with the instructions I may have missed something and please check line 69 because I get an error there? If possible, once complete please run the program and see that it works. I have not been able to because of the error.

---------------------------------------------------------------------------

In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:

  1. an array of integers.
  2. B) An integer that indicates the number of elements in the array and returns a vector of integers.
  3. You may or may not sort the array.

The function should determine the mode of the array or modes. That is, it should determine which value or values in the array occurs most often. The mode is the value or values the function should return. If the array has no mode (none of the values occur more than once), the function should return an empty vector. (Assume the array will always contain nonnegative values).

Write functions that fill the array with random numbers.

Test your function thoroughly.

Outcome Criteria

1) program compiles
2) program solves problem according to specification
3) program declares, creates, or initializes static or dynamic array correctly
4) if required program defines function or functions with array parameters or array returns
5) program uses arrays to solve problem
6) program destroys any dynamic arrays

 

#include <iostream>

using namespace std;

int detectMode(int* data, int size)

{

      if (size == 0)

            return 0;

      int i, j, mode, frequency = 0, count = 0;

      int** freq_array = (int**) (2 * sizeof(int*));

      for (i = 0;i < 2;i++)

            freq_array[i] = (int*) (size * sizeof(int));

      for (i = 0;i < size;i++)

            freq_array[1][i] = 0;

      for (i = 0;i < size;i++)

      {

            for (j = 0;j < count;j++)

                  if (freq_array[0][j] == data[i])

                  {

                        freq_array[1][j]++;

                        break;

                  }

            if (j == count)

            {

                  freq_array[0][count] = data[i];

                  freq_array[1][count] = 1;

                  count++;

            }

      }

      for (i = 0;i < count;i++)

            if (freq_array[1][i] > frequency)

            {

                  mode = freq_array[0][i];

                  frequency = freq_array[1][i];

            }

      for (i = 0;i < 2;i++)

            free(freq_array[i]);

      if (frequency == 1)

            return -1;

      else

            return mode;

}

int main()

{

      int n, i, result;

      cout << "Enter size of array ";

      cin >> n;

      int data[n];  //Gives error under the n [n]

       for (i = 0;i < n;i++)

            cin >> data[i];

      result = detectMode(data, n);

      if (result == -1)

            cout << "Mode not found\n";

      else

            cout << "Mode is " << result << endl;

      return 1;

}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Operators
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning