In statistics the mode of a set of values is the value that occurs most often. I
ID: 3621479 • Letter: I
Question
In statistics the mode of a set of values is the value that occurs most often. I need to write code that finds the mode of a user-specified array size. The code needs to be written inside the function arrayMode().
#include <iostream>
using namespace std;
int arrayMode(int *, int);
int main()
{
int *people;
int numppl;
cout << "How many people? ";
cin >> numppl;
people = new int[numppl];
cout << " Enter how many slices of pie each person eats in a year: " << endl;
for (int i = 0; i < numppl; i++)
{
cout << " Person " << (i + 1) << ": ";
cin >> people[i];
}
cout << " -----------------" << endl;
cout << " Mode: " << arrayMode(people, numppl) << endl << endl;
return 0;
}
int arrayMode(int *array, int size)
{
// Instead of code for the average, write code to find the mode of the array here.
int mode;
int total = 0;
for (int i = 0; i < size; i++)
total += array[i];
mode = total / size;
return mode;
}
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int arrayMode(int *, int);
int main()
{
int *people;
int numppl;
cout << "How many people? ";
cin >> numppl;
people = new int[numppl];
cout << " Enter how many slices of pie each person eats in a year: " << endl;
for (int i = 0; i < numppl; i++)
{
cout << " Person " << (i + 1) << ": ";
cin >> people[i];
}
cout << " -----------------" << endl;
cout << " Mode: " << arrayMode(people, numppl) << endl << endl;
system("pause");
return 0;
}
int arrayMode(int *array, int size)
{
// Instead of code for the average, write code to find the mode of the array here.
int max=1,i=0,count=1,j,mode=0;
j=-1;
for(i=0;i<size-1;i++)
{count=1;
for(j=i+1;j<size;j++)
{if(array[i]==array[j])
count++;
}
if(count>max)
{max=count;
mode=i;
}
}
return array[mode];
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.