Question

In: Computer Science

In C++, Write the definition of the function MaximumCount() whose header is int MaximumCount(Array<double>& data) It...

In C++, Write the definition of the function MaximumCount() whose header is

int MaximumCount(Array<double>& data)

It returns the amount of times the maximum value of data appears in data. If data is empty, it returns 0. For instance, if data = [7, 1, 4, 9, 6, 7, 7, 3, 2, 6, 9, 5, 9], it will return 3 since 9 appears three times

Solutions

Expert Solution

code:

#include <bits/stdc++.h> 
using namespace std; 

int MaximumCount(double data[15]) 
{ 
    //size of array data
    int n = 13;
    int maxcount=0;
        
        // consider all elements are not visited as false
        vector<bool> visited(n, false); 

                // count frequencies 
        for (int i = 0; i < n; i++) { 

                // if present skip
                if (visited[i] == true) 
                        continue; 

                // Count frequency 
                int count = 1; 
                for (int j = i + 1; j < n; j++) { 
                        if (data[i] == data[j]) { 
                                visited[j] = true; 
                                count++; 
                        } 
                } 
                if(count>maxcount) 
                maxcount=count;
        }
        //return maximum count
        return maxcount;
} 

int main() 
{ 
        double data[15] = { 7,1,4,9,6,7,7,3,2,6,9,5,9}; 
        int c=MaximumCount(data); 
        cout<<"Maximum count is :"<<c;
        return 0; 
} 

screenshot

output:

if array is empty : n means length of array is 0;

#include <bits/stdc++.h> 
using namespace std; 

int MaximumCount(double data[15]) 
{ 
    //size of array data
    int n = 0;
    int maxcount=0;
        
        // consider all elements are not visited as false
        vector<bool> visited(n, false); 

                // count frequencies 
        for (int i = 0; i < n; i++) { 

                // if present skip
                if (visited[i] == true) 
                        continue; 

                // Count frequency 
                int count = 1; 
                for (int j = i + 1; j < n; j++) { 
                        if (data[i] == data[j]) { 
                                visited[j] = true; 
                                count++; 
                        } 
                } 
                if(count>maxcount) 
                maxcount=count;
        }
        //return maximum count
        return maxcount;
} 

int main() 
{ 
        double data[15] = {}; 
        int c=MaximumCount(data); 
        cout<<"Maximum count is :"<<c;
        return 0; 
} 

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


Related Solutions

answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array&...
answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array& data,const T& value,int n) template It returns the index of the nth occurrence of value in data. If n is not positive, value appears less than n times in data or data is empty, it returns -1.
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
Write a C++ function which takes an int array and its size as parameters. It returns...
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype: // int array[]: array to search // size: number of elements in the array int countMult(const int array[], int...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT