Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

There are 3 files which you’ll be modifying: stats.cpp , stats-sandbox.cpp , and

ID: 3669663 • Letter: T

Question

There are 3 files which you’ll be modifying: stats.cpp, stats-sandbox.cpp, and stats_test.h. stats.cpp will contain the statistic functions below, stats_test.h will contain the unit tests for stats.cpp, and stats-sandbox.cpp will be a sandbox for making the functions, and will turn into the file analyzer in Task 4.

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.

Be sure to write unit tests in stats_test.h first, then implement the functions. Be sure to have at least 32 unit tests. To minimize compilation headaches later, do 'make test' often. Also, be sure your unit tests cover all the code in your stats.cpp file.

1. arrSum - Given an int array and its length, return the sum. The sum of an empty array should be 0.

2. 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.

3. arrMin - Given an int array and its length, return the smallest value, or 0 if the array is empty.

4. arrMax - Given an int array and its length, return the largest value, or 0 if the array is empty.

5. 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.

6. 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.

7. 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.

8. 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.

We have given you code to read a file of integers, whose name was specified on the command-line. ./stats-sandbox small.txt. Modify stats-sandbox.cpp so it outputs statistics on the file. Do this by reading the values into an int array (set size to 100000) and feeding that into your library (stats.cpp) functions. Print count, sum, mean, min, max, stdev, number of primes, and the set size. Follow the order of output and text given in the example output below.

Example Output of stats-sandbox.cpp:

make
./stats-sandbox small.txt

Count: 10

Sum: 576

Mean: 57.6

Min: 9

Max: 99

StDev: 25.3306

Primes: 0

Set Size: 9

./stats-sandbox medium.txt

Count: 100

Sum: 1044368

Mean: 10443.7

Min: 1610

Max: 19089

StDev: 4188.26

Primes: 8

Set Size: 100

./stats-sandbox large.txt

Count: 10000

Sum: 50181083

Mean: 5018.11

Min: 6

Max: 10093

StDev: 2885.05

Primes: 1202

Set Size: 6333

./stats-sandbox xlarge.txt

Count: 100000

Sum: -688257

Mean: -6.88257

Min: -5000

Max: 4999

StDev: 2896.66

Primes: 6667

Set Size: 9999

stats.cpp

#include

using namespace std;

int arrSum(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

double arrMean(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

int arrMin(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

int arrMax(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

double arrStdDev(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

bool isPrime(int number){
// TODO: Fill me in
return 0;
}

unsigned arrNumPrimes(int array[], unsigned length){
// TODO: Fill me in
return 0;
}

unsigned arrSetSize(int array[], unsigned length){
// TODO: Fill me in
return 0;
}


unsigned arrNumCount(int array[], unsigned length, int num){
// TODO: Fill me in
return 0;
}

stats_test.h

#ifndef STATS_TEST_H
#define STATS_TEST_H

#include
#include

#include "stats.cpp"

using namespace std;

// This requires CxxTest to be installed!
// For this CPPVideos example, navigate your terminal to CPPVideos and type:
// git submodule add https://github.com/CxxTest/cxxtest.git
// This will add the public CxxTest repository INSIDE the current repo.
// The current Makefile in this directory assumes cxxtest is a folder one
// level down.


class MyTests : public CxxTest::TestSuite {
public:
void testSum1() {
int a[] = {1,2,3};
TS_ASSERT_EQUALS(arrSum(a, 3), 6);
}
  
void testSum2() {
int a[] = {};
TS_ASSERT_EQUALS(arrSum(a, 0), 0);
}
  
// TODO: Fill in more tests!
};

#endif

stats-sandbox.cpp

#include
#include
#include
#include "stats.cpp"

using namespace std;

int main(int argc, char* argv[]){
if(argc == 2){ // A parameter was given!
  
int numbers[100000];
int number = 0;
unsigned int count = 0;
  
// Create a file handle and open it.
ifstream inFile;
inFile.open(argv[1]);
// Make sure it is a valid handle
if(!inFile){
cout << argv[1] << " unable to be opened! ";
return 0;
}
// Pull off numbers while we can. This will stop at the end of the file.
while(inFile >> number){
// TODO: Change this loop slightly
cout << count << ":" << number << endl;
count++;
}
  
// TODO: Fill in here.
  
}else{
cout << "No parameters, or more than one parameter given. ";
}
  

return 0;
}

small.txt

52
26
54
60
58
92
9
68
99
58

Explanation / Answer

//stats.cpp
#include <iostream>
#include <math.h>
using namespace std;
int arrSum(int array[], unsigned length){
int sum=0;
for(int i=0;i<length;i++)
{
   sum+=array[i];
}
return sum;
}

double arrMean(int array[], unsigned length){
int sum=0;
for(int i=0;i<length;i++)
{
   sum+=array[i];
}
return sum/length;
}

int arrMin(int array[], unsigned length){
int min=array[0];
for(int i=1;i<length;i++)
{
   if(array[i]<min)
   {
       min=array[i];
   }
}
return min;
}

int arrMax(int array[], unsigned length){
int max=array[0];
for(int i=1;i<length;i++)
{
   if(array[i]>max)
   {
       max=array[i];
   }
}
return max;
}

double arrStdDev(int array[], unsigned length){
double sum=0;
for(int i=0;i<length;i++)
{
   sum+=array[i];
}
double mean=sum/length;
sum=0;
for(int i=0;i<length;i++)
{
   sum+=pow(array[i]-mean),2);
}
return sqrt(sum/length);
}

bool isPrime(int number){
int ct=0;
for(j=2;j<number;j++)
{
if(number%j==0)
{
ct=1;
break;
}
}
if(ct==0)
{
return true;
}
else
return false;
}

unsigned arrNumPrimes(int array[], unsigned length){
int primes=0;
for(int i=0;i<length;i++)
{
   if(isPrime(array[i]))
   {
       count++;
   }
}
return primes;
}

unsigned arrSetSize(int array[], unsigned length){
int size=length;
for(int i=0;i<length;i++)
{
   for(int j=i+1;j<length;j++)
   {
       if(array[i]==array[j])
       {
           size--;
           array[j]=-999;
       }
   }
}
return size;
}

unsigned arrNumCount(int array[], unsigned length, int num){
int count=0;
for(int i=0;i<length;i++)
{
   if(array[i]==num)
   {
       count++;
   }
}
return count;
}

///////////////////////////////////////////////////////////////////////////////

//stats-sandbox.cpp
#include <iostream>
//#include
//#include
#include "stats.cpp"
using namespace std;
int main(int argc, char* argv[]){
if(argc == 2){ // A parameter was given!
  
int numbers[100000];
int number = 0;
unsigned int count = 0;
  
// Create a file handle and open it.
ifstream inFile;
inFile.open(argv[1]);
// Make sure it is a valid handle
if(!inFile){
cout << argv[1] << " unable to be opened! ";
return 0;
}
// Pull off numbers while we can. This will stop at the end of the file.
int count=0;
while(!inFile.eof()){
inFile>>number;
numbers[count]=number;
cout << count << ":" << number << endl;
count++;
}

}else{
cout << "No parameters, or more than one parameter given. ";
}
  
return 0;
}