41.0k views
21 votes
You are to write a program that performs basic statistical analysis on a 500 element array of integers. Your program should include the following programmer defined functions:int* generateArray(int size){This function should dynamically create an array of 501 elements and provide random numbers to fill the array. The random numbers should range from 10 to 100. The function should return the created array to the array generated in the main program.}int findMode(int *arr, int size){This function should analyze the array to find the most occurring value in the array. You do not have to account for bi-modal occurrences, so the first modal value is enough to satisfy this function definition.}int findMedian(int *arr, int size){This function should determine the median of the set of numbers in the array. Since there are 501 elements, you should have a clean median. Do not forget that the list has to be sorted in order to properly find the median.}Of course, main is critical to the function of your program and should output the mode, median of the list. An output of the 501 element array is not necessary.

User Sabalaba
by
4.3k points

1 Answer

13 votes

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.

User Fkorsa
by
4.2k points