Answer:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int* generateArray(int size)
{
int *numbers = new int[size];
for(int i=0;i<size;i++){
numbers[i] = (rand()%100) + 10;
}
return numbers;
}
int findMode(int *arr, int size)
{
int countMode = 0;
int mode ;
int count;
for(int i=0;i<size;i++) {
count = 1;
for(int j=i+1;j<size;j++) {
if(arr[j] == arr[i] ){
count++;
}
if(count > countMode) {
countMode = count;
mode = arr[i];
}
}
}
return mode;
}
int findMedian(int *arr, int size)
{
sort(arr,size);
int median;
if(size%2 == 0){
median = (arr[int((size-1)/2)]+arr[size/2])/2;
} else{
median = arr[(size-1)/2];
}
return median;
}
void sort(int *arr, int size)
{
int min;
for(int i=0;i<size-1;i++) {
min = i;
for(int j=i+1;j<size;j++){
if(arr[j] < arr[min]){
min = j;
}
if(min != i) {
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
}
int main()
{
srand(time(0));
int size = 501;
int *numbers = generateArray(size);
cout<<"Mode : "<<findMode(numbers,size)<<endl;
cout<<"Median : "<<findMedian(numbers,size)<<endl;
return 0;
}
Step-by-step explanation:
The C++ program is able to generate 500 integer items into the defined array and statistically analyze and provide the average and modal number in the array of integer dataset.