Below this question was asked and answered with the output to prove it works from an expert, but for some reason when I put the code into my MS Visual Studio 2019, C++ console for Windows 10 there appeared to be a warning at line 6 - int main(){. I received a warning under the word main (green squiggly line) and my output don't work for me as yours did for you. Do you think you can help me please.
Below this question was asked and answered with the output to prove it works from an expert, but for some reason when I put the code into my MS Visual Studio 2019, C++ console for Windows 10 there appeared to be a warning at line 6 - int main(){. I received a warning under the word main (green squiggly line) and my output don't work for me as yours did for you.
Do you think you can help me please.
Could you please write in C++ according to instructions and criteria a program for the following:
In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:
A) an array of integers
B) An integer that indicates the number of elements in the array and returns a
C) You may or may not sort the array
The function should determine the mode of the array or modes. That is, it should determine which value or values in the array occurs most often. The mode is the value or values the function should return. If the array has no mode (none of the values occur more than once), the function should return an empty vector. (assume the array will always contain nonnegative values).
Write functions that fill the array with random numbers
Test your function thoroughly.
Criteria
1) program compiles
2) program solves problem according to specification
3) program declares, creates, or initializes static or dynamic array correctly
4) if required program defines function or functions with array parameters or array returns
5) program uses arrays to solve problem
6) program destroys any dynamic arrays
Thank you.
Expert Answer
Let's understand step by step :
Here declaring static array
1. Declare an array of integers maximum size 100000 as a[100000] also declare n which stores number of elements in array.
2. declare a vector answer which stores modes.
3. Now ask user to input n.
4. Now run a for loop from i=0 to n-1 and take input in array.
5. Pass array a and n to function mode() and call it.
6. Inside mode() function:
declare integer variable greater which stores number of high frequency values.
declare a vector v.
declare map m1and m2 where m1 stores frequency of elements
7. run a for loop for n times and in parallel increment map value m1 for array element as key
which means frequency of element is incrementing.
if more than greater value is obtained then update greater with this value.
8. Now run another loop from i=0 to n-1
this is because there could be more than one mode have greater frequency so have to push all the modes.
so if any m1 map value is equal to greater also check if greater is more than 1 because if 1 then this will not be the mode and also check if this value is already pushed into vector v by pushing previous value in m2 map
if all true then push it into vector v.
9. Now return vector v.
10. Inside main function:
check if returned vector is empty or not.
if empty then print "There are no modes"
else print all the modes.
Here is C++ code :
#include <iostream>
#include <vector>
#include <map>
using namespace std;
vector<int> mode(int[],int);
int main()
{
int n,a[100000];
vector<int> answer;
vector<int>::iterator it;
cout<<"Enter number of elements in array:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
answer=mode(a,n);
if(answer.size()==0)
{
cout<<"There are no modes"<<endl;
}
else if(answer.size()==1)
{
cout<<"Mode is:"<<endl;
}
else
{
cout<<"Modes are:"<<endl;
}
for(it=answer.begin();it!=answer.end();it++)
{
cout<<*it<<" ";
}
}
vector<int> mode(int a[],int n)
{
int greater=0;
vector<int> v;
map<int,int>m1,m2;
for(int i=0;i<n;i++)
{
m1[a[i]]++;
if(m1[a[i]]>greater)
greater=m1[a[i]];
}
for(int i=0;i<n;i++)
{
if(m1[a[i]]==greater && greater>1 && m2[a[i]]!=1)
{
v.push_back(a[i]);
m2[a[i]]=1;
}
}
return v;
}
So program:
(1) declares static array
(2) it uses function mode() to calculate modes with parameters array and number of elements
(3) it uses array to solve problem
(3) there is no dynamic array to delete
OUTPUT :
Step by step
Solved in 2 steps with 1 images