Create (fill-in) the following 8 functions in stats.cpp. Be sure to create at le
ID: 3669303 • Letter: C
Question
Create (fill-in) the following 8 functions in stats.cpp. Be sure to create at least 4 unit tests for each function for a total of at least 32 tests.
arrSum - Given an int array and its length, return the sum. The sum of an empty array should be 0.
arrMean - Given an int array and its length, return the mean of the values as a double. The mean of an empty array should be 0.
arrMin - Given an int array and its length, return the smallest value, or 0 if the array is empty.
arrMax - Given an int array and its length, return the largest value, or 0 if the array is empty.
arrStdDev - Given an int array and its length, return the population standard deviation as a double (divide by N not N-1). Return 0.0 if the array is empty.
arrNumPrimes - Given an int array and its length, return the number of prime numbers in the array. Return 0 if the array is empty. Remember 0, 1, and negative numbers are not considered prime.
arrSetSize - Given an int array and its length, return the size of the array if duplicates were removed, as in if it was a set (no duplicates). Return 0 if the array is empty.
arrNumCount - Given an int array, its length, and a value, return the number of times value is in the array. Return 0 if the array is empty.
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
//Given an int array and its length, return the sum. The sum of an empty array should be 0.
int arrSum(int arr[], int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
//Given an int array and its length, return the mean of the values as a double. The mean of an empty array should be 0.
double arrMean(int arr[], int n)
{
if(n == 0)
return 0;
else
return arrSum(arr, n)/n;
}
//Given an int array and its length, return the smallest value, or 0 if the array is empty.
int arrMin(int arr[], int n)
{
int min = 0;
for(int i = 0; i < n; i++)
if(arr[i] < min)
min = arr[i];
return min;
}
//Given an int array and its length, return the largest value, or 0 if the array is empty.
int arrMax(int arr[], int n)
{
int max = 0;
for(int i = 0; i < n; i++)
if(arr[i] > max)
max = arr[i];
return max;
}
//Given an int array and its length, return the population standard deviation as a double (divide by N not N-1).
//Return 0.0 if the array is empty
double arrStdDev(int arr[], int n)
{
double mean = arrMean(arr, n);
double sum = 0;
if(n == 0)
return 0;
for(int i = 0; i < n; i++)
sum += pow((arr[i]- mean), 2);
double std = sum / n;
std = sqrt(std);
return std;
}
//Given an int array and its length, return the number of prime numbers in the array.
//Return 0 if the array is empty.
//Remember 0, 1, and negative numbers are not considered prime.
int arrNumPrimes(int arr[], int n)
{
int primeCount = 0;
for(int i = 0; i < n; i++)
{
int flag = true;
for(int i = 2; i <= (int)sqrt(arr[i]); i++)
if(arr[i] % i == 0)
flag = false;
if(flag == true)
primeCount++;
}
return primeCount;
}
//Given an int array and its length, return the size of the array if duplicates were removed,
//as in if it was a set (no duplicates).
//Return 0 if the array is empty.
int arrSetSize(int arr[], int n)
{
int uniqueNumbers = n;
for (int j = 0; j < n; j++)
{
for(int k = 0; k < n; k++)
{
if((arr[k] == arr[j]) && (k!=j))
{ uniqueNumbers--; }
}
}
return uniqueNumbers;
}
//Given an int array, its length, and a value, return the number of times value is in the array.
//Return 0 if the array is empty.
int arrNumCount(int arr[], int n, int val)
{
int numOfTimes = 0;
for(int i = 0; i < n; i++)
if(val == arr[i])
numOfTimes++;
return n-numOfTimes;
}
int main()
{
int list1[] = {1, 2, 3, 6, 1, 7, 9, 8}; //List of 8 elements.
int list2[] = {3, -9, 4, 6, 8}; //List of 5 elements.
int list3[] = {6, 3, 8, 1, 3, 9, 3, 8, 7, 3}; //List of 10 elements.
int list4[] = {}; //List of 0 elements.
cout<<"The sum of elements in list1 is: "<<arrSum(list1, 8)<<endl;
cout<<"The mean of elements in list1 is: "<<arrMean(list1, 8)<<endl;
cout<<"The min of elements in list1 is: "<<arrMin(list1, 8)<<endl;
cout<<"The max of elements in list1 is: "<<arrMax(list1, 8)<<endl;
cout<<"The stdD of elements in list1 is: "<<arrStdDev(list1, 8)<<endl;
cout<<"The num of primes in list1 is: "<<arrNumPrimes(list1, 8)<<endl;
cout<<"The num of unique elements in list1 is: "<<arrSetSize(list1, 8)<<endl;
cout<<"The num of times 1 appeared in list1 is: "<<arrNumCount(list1, 8, 1)<<endl<<endl;
cout<<"The sum of elements in list2 is: "<<arrSum(list2, 5)<<endl;
cout<<"The mean of elements in list2 is: "<<arrMean(list2, 5)<<endl;
cout<<"The min of elements in list2 is: "<<arrMin(list2, 5)<<endl;
cout<<"The max of elements in list2 is: "<<arrMax(list2, 5)<<endl;
cout<<"The stdD of elements in list2 is: "<<arrStdDev(list2, 5)<<endl;
cout<<"The num of primes in list2 is: "<<arrNumPrimes(list2, 5)<<endl;
cout<<"The num of unique elements in list2 is: "<<arrSetSize(list2, 5)<<endl;
cout<<"The num of times 1 appeared in list2 is: "<<arrNumCount(list2, 5, 1)<<endl<<endl;
cout<<"The sum of elements in list3 is: "<<arrSum(list3, 10)<<endl;
cout<<"The mean of elements in list3 is: "<<arrMean(list3, 10)<<endl;
cout<<"The min of elements in list3 is: "<<arrMin(list3, 10)<<endl;
cout<<"The max of elements in list3 is: "<<arrMax(list3, 10)<<endl;
cout<<"The stdD of elements in list3 is: "<<arrStdDev(list3, 10)<<endl;
cout<<"The num of primes in list3 is: "<<arrNumPrimes(list3, 10)<<endl;
cout<<"The num of unique elements in list3 is: "<<arrSetSize(list3, 10)<<endl;
cout<<"The num of times 1 appeared in list3 is: "<<arrNumCount(list3, 10, 1)<<endl<<endl;
cout<<"The sum of elements in list4 is: "<<arrSum(list4, 0)<<endl;
cout<<"The mean of elements in list4 is: "<<arrMean(list4, 0)<<endl;
cout<<"The min of elements in list4 is: "<<arrMin(list4, 0)<<endl;
cout<<"The max of elements in list4 is: "<<arrMax(list4, 0)<<endl;
cout<<"The stdD of elements in list4 is: "<<arrStdDev(list4, 0)<<endl;
cout<<"The num of primes in list4 is: "<<arrNumPrimes(list4, 0)<<endl;
cout<<"The num of unique elements in list4 is: "<<arrSetSize(list4, 0)<<endl;
cout<<"The num of times 1 appeared in list4 is: "<<arrNumCount(list4, 0, 1)<<endl<<endl;
}
If you need any further refinements, just get back to me.