For this assignment you may use regular pointers.
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.
// C++ program that performs basic statistical analysis on a 501 element array of integers
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
int* generateArray(int size);
int findMode(int *arr, int size);
int findMedian(int *arr, int size);
void sort(int *arr, int size);
int main()
{
srand(time(0));
int size = 501;
// create an array of 501 random elements between [10,100]
int *numbers = generateArray(size);
// determine and print the first modal value
cout<<"Mode : "<<findMode(numbers,size)<<endl;
// calculate and print the median value
cout<<"Median : "<<findMedian(numbers,size)<<endl;
return 0;
}
// function to create and return an array of size filled, with random elements between [10,100]
int* generateArray(int size)
{
int *numbers = new int[size];
for(int i=0;i<size;i++)
numbers[i] = rand()%91+10;
return numbers;
}
// function to determine and return the first modal value from the array arr
int findMode(int *arr, int size)
{
int countMode = 0;
int mode ;
int count;
// loop to count the frequency of each number in the array
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 of ith number > current mode count, update the mode and its count
if(count > countMode)
{
countMode = count;
mode = arr[i];
}
}
return mode; //return first modal value
}
// function to calculate and return the median value from the array arr
int findMedian(int *arr, int size)
{
sort(arr,size);
int median;
// if number of elements is even, median is half of the sum of the middle two values
if(size%2 == 0)
median = (arr[int((size-1)/2)]+arr[size/2])/2;
else // else return the middle value
median = arr[(size-1)/2];
return median;
}
// function to sort the elements of the array in ascending order
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;
}
}
}
//end of program
Output:
Get Answers For Free
Most questions answered within 1 hours.