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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education