Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, The program is in C++ was wondering if someone could help me design some pes

ID: 3669933 • Letter: H

Question

Hi,

The program is in C++ was wondering if someone could help me design some pesudocode for it?

~Thanks!

/********************************
*Date: 02/15/16
*Author: Sam
*Description: This program takes as parameters an array of integers and the size of the array, and returns a vector containing the mode(s).
*******************************/

#include <iostream>
#include <algorithm> //for sort()
#include <vector>

using namespace std;

//finds the modes of the elements of the given array

vector<int> findMode(int array[], int size)

{

     vector<int> modesVector;
     int tempFreq = 1,maxFreq = 1,temp;

     //sort the given array

     for(int i=0;i<=size-1;i++)           // i <= size-1 NOT i < size-1

     {

          for(int j=i+1;j<=size-1;j++)    // Small optimization : Begin j from i+1 and not from 0 everytime and j <= size-1 NOT j < size-1

          {

              if(array[i]>array[j])

              {

                   temp=array[i];

                   array[i]=array[j];

                   array[j]=temp;

              }

          }

     }
     //find maximum Frequency

     for (int i = 0; i <= size - 1; i++) // i <= size-1 and NOT is < size-1
     {
          if (array[i] == array[i+1])

          {

              tempFreq++;

              //update maxFreq

              if ( tempFreq >maxFreq )
              {
                   maxFreq = tempFreq;

              }

          }

          else

              // reset tempFreq

              tempFreq= 1;

     }

     tempFreq = 1;

     //find the values those frequency equals to maximum frequency and
     //push them into modes vector

     for (int i = 0; i < size - 1; i++)

     {

          if (array[i] == array[i+1])
          {

              tempFreq++;

              if(maxFreq==tempFreq)
              {
                   modesVector.push_back(array[i]);
                   tempFreq = 1;
              }
          }
          else
          {
              if(maxFreq==tempFreq)
              {

                   modesVector.push_back(array[i]);

                   tempFreq = 1;

                   if(i==size-2)

                        modesVector.push_back(array[i+1]);

              }
          }
     }

     //sort the array before return

     sort( modesVector.begin(), modesVector.end());

     //return vector that contains modes

     return modesVector;

}

int main()

{

     //input arrays

     int numbers1[10] = {1,4,2,4,6,5,7,7,6,10};

     int numbers2[5] = {1,3,2,4,5};

     vector<int> modeVector;

     modeVector =findMode(numbers1,10);

     cout <<" The modes found for the first array: ";

     for ( int i = 0; i < modeVector.size(); i++)

     {

          cout<< modeVector[i] << " "; //printing vector mode details.

     }

     cout<<endl;

     modeVector =findMode(numbers2,5);

     cout <<" The modes found for the second array: ";

     for ( int i = 0; i < modeVector.size(); i++)

     {

          cout<< modeVector[i] << " "; //printing vector mode details.

     }

     cout<<endl;

     system("pause");

     return 0;

}

Explanation / Answer

1. declare & Initialize integer array numbers1[],numbers2[5]
2. Declare vector
3. Call function findMode() with array and array size to find vector for first array
4. Print vector mode details for all vector found using a loop.
5. Call function findMode() with array and array size to find vector for second array
6. Print vector mode details for all vector found using a loop.
7. Function findMode()
   a. Declare vector
b. Declare & initialize tempFreq to 1,maxFreq to 1,temp
c. sort the given array
       i. for each i from 0 to less than equal to size-1
           A. for each j from i+1 to less than equal to size-1
               1. if array[i] is greater than array[j]
                   a. temp=array[i]
                   b. array[i]=array[j]
                   c. array[j]=temp
                   d. Increase j by 1
           B. Increase i by 1
   d. find maximum Frequency
       i. for each i from 0 to less than equal to size-1
           A. if array[i] is equal to array[i+1]
               1. Increase tempFreq by 1
               2. if is tempFreq greater than maxFreq
                   a. maxFreq = tempFreq
           B.   else
               tempFreq = 1
           C. increase the value of i by 1

   e. tempFreq = 1;
   f. for each i from 0 to less than equal to size-1
       i. if (array[i] == array[i+1])
           A. Increase tempFreq by 1
               1. if maxFreq is equal to tempFreq
                   a. modesVector.push_back(array[i])
                   b. tempFreq = 1
       ii. else
           A. if maxFreq is equal to tempFreq
               1. modesVector.push_back(array[i])
               2. tempFreq = 1
               3. if i is equal to size-2
                   a. modesVector.push_back(array[i+1])
       iii. increase the value of i by 1
   g. sort the array before return using sort( modesVector.begin(), modesVector.end())
   h. return vector that contains modes

Hope it helps.

Please leave a comment if you have any query.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote