I am trying to implement and test the function templates Mean, Median, Selection
ID: 3568984 • Letter: I
Question
I am trying to implement and test the function templates Mean, Median, SelectionSort, and Swap. I have written all the code for both files, but I need help with a couple of compile errors. I have listed the compile errors I received when I tried to run the program. I have included parts of the program. The first file, stattest.cpp is the client program to test Stats Templates and was created by the instructor. It should not be changed or modified. The problem is in my stat.t program that I created. Can someone help me figure out why mean and median is not defined. Thanks!
These are the compile errors:
g++47 -std=c++11 -Wall -Wextra -I. -ostattest1.x stattest1.cpp
stattest1.cpp: In function int main(int, char**):
stattest1.cpp:104:40: error: Mean was not declared in this scope
stattest1.cpp:105:42: error: Median was not declared in this scope
make: *** [stattest1.x] Error 1
parts of stattest.cpp
Testing Mean and Median from stats.t
Reads numbers until non-number is read, then sends to standard output:
the numbers read
the mean
the median
the numbers sorted
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stats.t>
// uncomment one of these to define the type of data:
// typedef signed char NumberType; // 1
// typedef short NumberType; // 2
// typedef int NumberType; // 3
// typedef long NumberType; // 4
// typedef unsigned char NumberType; // 5
// typedef unsigned short NumberType; // 6
// typedef unsigned int NumberType; // 7
// typedef unsigned long NumberType; // 8
//typedef float NumberType; // 9
// typedef double NumberType; //10
// typedef long double NumberType; //11
ReadData(input, ifs);
}
// show data in original object:
std::cout << "Data as entered: ";
DisplayData(input, std::cout, ' ');
std::cout << ' ';
// */
// display mean and median:
std::cout << "Mean: " << Mean(input) << ' ';
std::cout << "Median: " << Median(input) << ' ';
// display data after calls:
std::cout << "Data after sort: ";
DisplayData(input, std::cout, ' ');
std::cout << ' ';
// */
return 0;
}
parts of stat.t
#include <iostream>
#include <cstdlib>
#include <vector>
#ifndef _STATS_T
#define _STATS_T
//template <typename TStats>;
const size_t max = 100; //Array max
template <typename T>
class TStats
{
public:
TStats () ;
~TStats () ;
TStats (const TStats<T>&) ;
TStats<T>& operator = (const T&) ;
long double Mean ()const;
long double Median ();
void Sort ();
size_t Size () const ;
void SetData ( const T* data, T size ) ;
void RetrieveData ( T* data ) const ;
private:
unsigned int size_;
T* data_;
bool sorted_;
static void Swap (T& x, T& y);
};
template <typename T>
TStats<T>::TStats(): size_(0), data_(0), sorted_(0)
{
data_ = new T[max];
}
template <typename T>
TStats<T>::~TStats()
{
delete [] data_; //destructor
}
template <typename T>
TStats<T>::TStats(const TStats<T>& a)
{
data_ = new T [size_];
for (size_t i = 0; i < size_; ++i)
data_[i] = a.data_[i];
}
...............
template <typename T>
long double TStats<T>::Mean() const
{
T sum = 0;
for (size_t j = 0; j < size_; ++j)
{
sum += data_[j]; //Loops through each element in array and sums them up.
}
if (size_ == 0) //checks for division by zero
{
std::cout << "Cannot divide by zero!" << std::endl;
return 0;
}
T mean = sum/size_; //calulates mean
return mean;
}
template <typename T>
long double TStats<T>::Median()
{
if (sorted_ != 1) Sort();
if(size_ % 2 == 1)
return data_[(size_ - 1) / 2];
else
return (data_[size_ / 2 - 1] + data_[size_ / 2]) / 2.0;
}
Explanation / Answer
Hello, I am an online tutor and freelance writer. I am willing to do this work for you. My work is 100% original and on time. Email me at ashioya.george@gmail.com
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.