Please provide the code in C++ for the following problem. . Problem statement: I
ID: 656110 • Letter: P
Question
Please provide the code in C++ for the following problem. .
Problem statement:
In statistics:
The mode of a set of values is the value that occurs most often or with the greatest frequency.
When a set of values is sorted in ascending or descending order, its median is the middle value. If the set contains an even number of values, the median is the mean, or average, of the two middle values.
Write a C++ application that do the following:
Define an array of size 10 and sore the values {1, 2, 3, 3, 3, 2, 2, 1, 3, 4, 5}. (Use array initializer.
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
The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode (none of the values occur more than once), the function should return ?1. (Assume the array will always contain nonnegative values.)
Write a function to sort the array. (use selection sort algorithm)
Write a function that determine the median of the array. This value should be returned as a double.
Write a function to display the contents of the array
NOTE: for all the above functions, demonstrate your pointer prowess by using pointer notation instead of array notation.
This is the code I have so far for this problem if this helps but it has errors in it.
My question is I've got this much code but I need to have the 5 show up at the end of it and have that calculated with the mean and the mode. this is the code I have below.
#include <iostream>
using namespace std;
double calculateMedian(int* ,int);
void selection_sort(int* ,int);
void display(int* ,int);
int findMode(int* ,int);
int main()
{
int number[] = {1, 2, 3, 3, 3, 2, 2, 1, 3, 4, 5 };
int *pNumbers;
int *pN;
pNumbers=number;
pN=number;
int n=10;
for(int i=0;i<n;i++)
cout << *(pNumbers + i) <<"," ;
double dMedian = calculateMedian(pN,n) ;
cout << "Median is" << dMedian << endl;
selection_sort(pN,n);
display(pN,n);
cout <<"Mode of numbers is"<<findMode(pN,n) << endl;
return 0;
}
double calculateMedian(int* ptr,int iN)
{
double dResult;
int iHalf = iN/2;
if(iN%2==0)
{
dResult = (*(ptr + iHalf-1)+ *(ptr + iHalf))/double(2);
}
else
{
dResult = *(ptr + iHalf);
}
return dResult;
}
void selection_sort(int *pointer, int size)
{
int *i, *j, swap;
int *end = NULL;
if(size < 2 || pointer == NULL)
return;
end = pointer + size - 1;
for(i = pointer; i < end; i++)
{
for(j = i + 1; j <= end; j++)
{
if(*j < *i)
{
swap = *i;
*i = *j;
*j = swap;
}
}
}
}
void display(int* ptr1 ,int num)
{
for(int i=0;i<num;i++)
cout << *(ptr1 + i) <<"," ;
cout<<endl;
}
int findMode(int *numPtr, int num)
{
int mode = -1, position = 0, highest;
int frequency[num];
for(int i = 0; i < num; i++)
*(frequency + i) = 0;
for (int k = 0; k < num; k++)
{
for (int i = 1; i < num; i++)
{
if (*(numPtr + k) == *(numPtr + i) && &*(numPtr + k) != &*(numPtr + i))
*(frequency + k) += 1;
}
}
highest = *(frequency + 0);
for (int count = 1; count < num; count++)
{
if (*(frequency + count) > highest)
{
highest = *(frequency + count);
position =+ count;
}
}
for(int i = 0; i < num; i++)
if(*(frequency + i) != *(frequency + (i + 1)) && (i + 1) < num)
{
mode = *(numPtr + position);
return mode; //returns number that occurs most
}
return mode; //returns -1 if all same frequency
}
Explanation / Answer
#include double GetMedian(double daArray[], int iSize) { // Allocate an array of the same size and sort it. double* dpSorted = new double[iSize]; for (int i = 0; i 0; --i) { for (int j = 0; j dpSorted[j+1]) { double dTemp = dpSorted[j]; dpSorted[j] = dpSorted[j+1]; dpSorted[j+1] = dTemp; } } } // Middle or average of middle values in the sorted array. double dMedian = 0.0; if ((iSize % 2) == 0) { dMedian = (dpSorted[iSize/2] + dpSorted[(iSize/2) - 1])/2.0; } else { dMedian = dpSorted[iSize/2]; } delete [] dpSorted; return dMedian; } double GetMode(double daArray[], int iSize) { // Allocate an int array of the same size to hold the // repetition count int* ipRepetition = new int[iSize]; for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.