Below is the assignment description and the file I have writ
Below is the assignment description and the file I have written. I would love to test this file to make sure that everything is satisfied (vector with all modes in ascending order). I\'m having a really hard time constructing a main method to test it. Help!!
-------------------------------------------------------
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it\'s similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: \"std::sort(result.begin(), result.end());\".
-------------------------------------------------------
#include <algorithm> // needed for the sort() function
#include <vector> // needed to use the vector type
#include <iostream> // needed for the main function
using std::vector;
using std::sort;
vector<int> findMode(int array[], int size)
{
int maxFreq = 1; // initalizing the largest frequency to be 1
vector<int> result; // results is an empty vector
//Initializing the loop through the integer array
for (int i = 0; i < size; i++)
{
int count = 1; //started the counter at 1
//started the the loop after the 1st entry, looking for matches with the value at index i.
for (int j = i + 1; j < size; j++)
{
//if an entry matches another, then add one to the counter
if (array[i] == array[j])
{
count++;
}
}
//when the counter is equal to the max frequency, the integer is added to the result vector.
if (count == maxFreq)
{
result.push_back(array[i]);
}
//when the counter for i is greater than the max frequency, updates the maxFreq, clear the values stored in the result vector, and push the value to the empty vector.
if (count > maxFreq)
{
maxFreq = count;
result.clear();
result.push_back(array[i]);
}
}
//sort the modes in ascending order
sort(result.begin(), result.end());
return result;
Below is the assignment description and the file I have written. I would love to test this file to make sure that everything is satisfied (vector with all modes in ascending order). I\'m having a really hard time constructing a main method to test it. Help!!
-------------------------------------------------------
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it\'s similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: \"std::sort(result.begin(), result.end());\".
-------------------------------------------------------
#include <algorithm> // needed for the sort() function
#include <vector> // needed to use the vector type
#include <iostream> // needed for the main function
using std::vector;
using std::sort;
vector<int> findMode(int array[], int size)
{
int maxFreq = 1; // initalizing the largest frequency to be 1
vector<int> result; // results is an empty vector
//Initializing the loop through the integer array
for (int i = 0; i < size; i++)
{
int count = 1; //started the counter at 1
//started the the loop after the 1st entry, looking for matches with the value at index i.
for (int j = i + 1; j < size; j++)
{
//if an entry matches another, then add one to the counter
if (array[i] == array[j])
{
count++;
}
}
//when the counter is equal to the max frequency, the integer is added to the result vector.
if (count == maxFreq)
{
result.push_back(array[i]);
}
//when the counter for i is greater than the max frequency, updates the maxFreq, clear the values stored in the result vector, and push the value to the empty vector.
if (count > maxFreq)
{
maxFreq = count;
result.clear();
result.push_back(array[i]);
}
}
//sort the modes in ascending order
sort(result.begin(), result.end());
return result;
Below is the assignment description and the file I have written. I would love to test this file to make sure that everything is satisfied (vector with all modes in ascending order). I\'m having a really hard time constructing a main method to test it. Help!!
-------------------------------------------------------
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it\'s similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: \"std::sort(result.begin(), result.end());\".
The mode is the value that appears most often in a set of data. Write a function named findMode that takes as parameters an array of int and the size of the array, and returns a vector containing the mode(s). If there is just a single most frequent value, the vector will only contain that one value, but if multiple values tie for maximum frequency, the vector will need to contain all such values. This includes the case where every number in the array appears only once. Each mode should appear only once in the vector. The values in the vector that is returned must be in ascending order. If you need to sort a vector, it\'s similar to sorting an array, but specifying the beginning and end of the vector look a little bit different. If your vector is named result, then it would look like this: \"std::sort(result.begin(), result.end());\".
-------------------------------------------------------
#include <algorithm> // needed for the sort() function
#include <vector> // needed to use the vector type
#include <iostream> // needed for the main function
using std::vector;
using std::sort;
vector<int> findMode(int array[], int size)
{
int maxFreq = 1; // initalizing the largest frequency to be 1
vector<int> result; // results is an empty vector
//Initializing the loop through the integer array
for (int i = 0; i < size; i++)
{
int count = 1; //started the counter at 1
//started the the loop after the 1st entry, looking for matches with the value at index i.
for (int j = i + 1; j < size; j++)
{
//if an entry matches another, then add one to the counter
if (array[i] == array[j])
{
count++;
}
}
//when the counter is equal to the max frequency, the integer is added to the result vector.
if (count == maxFreq)
{
result.push_back(array[i]);
}
//when the counter for i is greater than the max frequency, updates the maxFreq, clear the values stored in the result vector, and push the value to the empty vector.
if (count > maxFreq)
{
maxFreq = count;
result.clear();
result.push_back(array[i]);
}
}
//sort the modes in ascending order
sort(result.begin(), result.end());
return result;
Solution
Here is your main method, Copy Paste This code and Replace your main method using the below Code.
int main()
{
//Case 1
// int arr[10]= {1,1,1,1,1,1,1,2,3,4};
//vector<int> result = findMode(arr,10);
//Case 2
// int arr[10]= {1,2,3,4};
//vector<int> result = findMode(arr,4);
//Case 3
// int arr[1]= {1};
//vector<int> result = findMode(arr,1);
//Case 4
// int arr[10]= {1,1,2,2,3,3};
//vector<int> result = findMode(arr,6);
int arr[10]= {1,1,2,2,3,3};
vector<int> result = findMode(arr,6);
for (std::vector<int>::iterator it = result.begin() ; it != result.end(); ++it)
std::cout << \' \' << *it;
}
I have written few more Tests for you, you can uncomment them and test your code using those inputs. I hope it helps.





