Question

In: Computer Science

write a function that accept an array and its size than return an array call modeAry...

write a function that accept an array and its size than return an array call modeAry that store the following information:    modeAry[0] = Number of modes; modeAry[1] = Frequency of the modes ; modeAry[>=2] = All the modes you found   ;        EXP:if the array input is [1,1,2,2,4,3,3], the function modAry should be [3,2,1,2,3]. In c++

Solutions

Expert Solution

modeAry.cpp

#include<iostream>
#include<cstdlib>
using namespace std;

int *modeAry(int arr[],int size)
{
    int max=INT_MIN,i,freqOfModes,noOfModes,modesInd;
    int *modeArr = new int[size]; //dynamic mode array
    //find maximum element in array
    // maximum lement is required to define size of count array
    for(i=0;i<size;i++)
    {
        if(arr[i] > max)
        max=arr[i];
   }
   int count[max]; //Array to count frequency of each element
  
   //initialize frequency of each element to 0
   for(i=0;i<size;i++)
   count[i] = 0;
  
   //find frequency of each element
   for(i=0;i<size;i++)
   count[arr[i]]++;
  
   //modes of an array are most frequent numbers
   //find modes of an array
   freqOfModes = 0;
   for(i=0;i<max;i++)
   {
       if (count[i] > freqOfModes)
       freqOfModes = count[i];
      
   }
  
   // find no of modes
   noOfModes = 0 ;
   modesInd = 2;
   for(i=0;i<max;i++)
   {
       if (count[i] > 0 && count[i] == freqOfModes)
       {
       noOfModes++;
       modeArr[modesInd] = i;
       modesInd++;
   }
   }
  
   modeArr[0] = noOfModes;
   modeArr[1] = freqOfModes;
     
   return modeArr;
     
}
int main()
{
   int n,i,modeArrSize;
   int arr[100]; //array of size 100 to store numebrs;
   cout<<"Enter size of Array : ";
   cin>>n;
   cout<<"\nEnter " <<n<<" elements of array : ";
   for(i=0;i<n;i++)
   cin>>arr[i];
  
   int *modeArr = modeAry(arr,n); //modeArr is a mode Array return by modeAry function;
  
   modeArrSize = modeArr[0] + 2; //all modes and modeArr[0] , modeArr[1] i.e +2
  
   //print mode array
   cout<<"\nMode Array : [ ";
   for(i=0;i<modeArrSize;i++)
   cout<<modeArr[i]<<" ";
   cout <<"]";
}

OUTPUT


Related Solutions

Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
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.
C++ ProgramWrite 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 forth. The function...
In this third part, write a function that accepts an array of integers and its size as arguments.
in C++In this third part, write a function that accepts an array of integers and its size as arguments. The function should create a new array that is of half size the argument array (round up the fraction if the size of the argument array is odd). The value of the first element (Element 0) of the new array should be the sum of the first two elements of the argument array (Element 0 and 1). Element 1 of the...
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,...
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
In C++ Write a function that took in SIZE and array [size] and counted the number...
In C++ Write a function that took in SIZE and array [size] and counted the number of elements >100
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT