Question

COULD YOU PLEASE HELP ME************************** write a better mode computation function that uses the ideas in...

COULD YOU PLEASE HELP ME**************************

write a better mode computation function that uses the ideas in the word file and improves on the mode computation function in the cpp file.

===========source.cpp

#include <ctime>

#include <iomanip>

#include <iostream>

#include <string>

#include <random>

using namespace std;

default_random_engine e(static_cast<unsigned>(time(NULL)));

void fill(int a[], int size, int value)

{

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

a[i] = value;

}

void randomFill(int a[], int size, int lb, int up)

{

uniform_int_distribution<int> u(lb, up);

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

a[i] = u(e);

}

void show(int a1d[], int size)

{

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

cout << setw(2) << a1d[i] << ' ';

cout << endl;

}

int count(int a1d[], int size, int value)

{

int vcount = 0;

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

if(a1d[i] == value) vcount++;

return vcount;

}

int findLargest(int a1d[], int size)

{

int largest = a1d[0];

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

if(a1d[i] > largest) largest = a1d[i];

return largest;

}

/*

the mode of a set of things is that thing that appears the greater number of times in the set

a set may have several modes

*/

int computemodes(int source[], int size, int modes[], int& msize)

{

/*

1. fill the modes array with zeroes

*/

fill(modes, size, 0);

/*

2. store the number of times each source element appears in the modes array.

if an element appears more than once in the source array then its counts appears

more than once the modes array.

source and modes form a parallel array structure

*/

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

modes[i] = count(source, size, source[i]);

/*

3. calculate the largest number in the modes array. this number is the number of

times the mode or modes appears in the source array

*/

int modevalue = findLargest(modes, size);

/*

4. assign -1 to the mode array elements that are less than the mode value

now only mode values in the modes array are not equal to -1.

the corresponding elements in the source array are the modes.

*/

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

if(modes[i] != modevalue) modes[i] = -1;

/*

5. we use the modes array to identify the source elements that are modes:

any element in the modes array that is not -1 corresponds to a mode in the

source array. if the mode is 1 then every source element is a mode

and no element in the modes array is -1; if the mode is greater than 1 then

a. many modes array entries are -1

b. the number of times a mode appears in the source equals its corresponding modes value

c. the number of modes array entries that are not -1 are the number of times the modes

appear in the source array

the following nested for loop transforms the modes array into an array in which

the first apppearance of a mode in the source corresponds to a modes array entry

that is not -1 and subsequent appearances of this mode in the source correspond to

modes array entries that are -1.

*/

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

if(modes[i] != -1) //first appearance of the mode in the source

for(int j = i + 1; j < size; j++)

if(source[i] == source[j]) modes[j] = -1;//subsequent appearances

/*

at this point the usage of the modes array changes.

heretofore, an entry that is not -1 in the modes array is the number of times

a mode appears in the source array. now an entry in the modes array is a mode.

the loop adds modes from the source array to the modes array.

msize serves 2 purposes:

a. it is number of modes copied so far.

b. it is the next free modes array position.

*/

msize = 0;

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

if(modes[i] != -1) //first occurrence of a mode in the source

{

modes[msize] = source[i];

msize++;

}

return modevalue;

}

int main()

{

const int size = 24;

int a[size];

int m[size];

randomFill(a, size, 10, 16);

show(a, size);

int msize = 0;

int modevalue = computemodes(a, size, m, msize);

cout << "The mode value is " << modevalue << endl;

if (msize == 1)

cout << "The mode is ";

else

cout << "The modes are ";

show(m, msize);

system("pause");

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Code

#include <ctime>

#include <iomanip>

#include <iostream>

#include <string>

#include <random>

#include <utility> //for pair

#include <bits/stdc++.h> //for sort method

using namespace std;

default_random_engine e(static_cast<unsigned>(time(NULL)));

void fill(int a[], int size, int value)

{

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

a[i] = value;

}

void randomFill(int a[], int size, int lb, int up)

{

uniform_int_distribution<int> u(lb, up);

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

a[i] = u(e);

}

void show(int a1d[], int size)

{

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

cout << setw(2) << a1d[i] << ' ';

cout << endl;

}

//helper method to find the index of a key in a vector of key-value pairs

int findPosition(int key, vector< pair<int,int > > &modePairs){

//looping through vector

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

if(modePairs[i].first==key){

//found, returning index

return i;

}

}

//not found, returning -1

return -1;

}

//method needed to sort the pair vector by the count (second value)

bool sortByCount(const pair<int,int> &a,

const pair<int,int> &b)

{

return (a.second > b.second);

}

/*

the mode of a set of things is that thing that appears the greater number of times in the set

a set may have several modes.

this method will return the mode count and a sorted vector of pair of integers which

contains the mode value - count pairs in descending order of the count

*/

int computemodes(int source[], int size, vector< pair<int,int > > &modePairs)

{

int index;

//looping through the source array for only ONCE

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

//checking if the vector already contains the key

index=findPosition(source[i],modePairs);

if(index==-1){

//does not exist, creating a new pair

pair <int, int> p;

p.first=source[i];

p.second=1; //setting count as 1

//adding to vector

modePairs.push_back(p);

}else{

//found, incrementing the count of the found pair by 1

modePairs[index].second++;

}

}

//sorting by descending order of the count (check the sortByCount method)

sort(modePairs.begin(), modePairs.end(), sortByCount);

//the first element will always contain the mode count, assuming that the

//array contains atleast one element

return modePairs[0].second;

}

int main()

{

const int size = 24;

int a[size];

int m[size];

randomFill(a, size, 10, 16);

show(a, size);

int msize = 0;

//creating an empty vector of integer pairs

vector< pair<int,int > > modePairs;

//computing the mode(s)

int modevalue = computemodes(a, size, modePairs);

//displaying mode value (count)

cout << "The mode value is " << modevalue << endl;

//displaying the modes

cout<<"The mode(s): ";

//displaying all pairs with count modevalue

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

if(modePairs[i].second==modevalue){

cout<<setw(2)<<modePairs[i].first<<" ";

}else{

//since the modePairs is sorted, we dont have to iterate anymore as

//all the remaining elements will have less mode counts

break;

}

}

cout<<endl;

system("pause");

return 0;

}

/*OUTPUT*/

13 12 14 13 11 11 12 15 15 15 16 16 16 12 16 16 14 12 16 16 15 13 14 10

The mode value is 7

The mode(s): 16

Press any key to continue . . .

/*another OUTPUT*/

13 10 16 16 15 13 16 14 14 13 11 12 14 14 10 12 14 10 11 12 12 10 15 12

The mode value is 5

The mode(s): 14 12

Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
COULD YOU PLEASE HELP ME************************** write a better mode computation function that uses the ideas in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int...

    Who could write the array.cpp file ?   //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array...

    Write a C++ program that contains 2 functions. LastLargestIndex, that takes as paraneters an int array and // its size and returns the index of the first occurrence of the largest element II in the array. Also, write a function to display the array #include ·peh.h" #include <iostream> using namespace std const int ARRAY_SIZE = 15; int main int list[ARRAY SIZE56, 34, 67, 54, 23, 87, 66, 92. 15, 32, 5, 54, 88, 92, 30 cout < List elements: "...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT