### converting from no class to using a class

I made a program but I can't figure out how to convert it using a class called Stats. I would like to do it for my own practice and knowledge but I can't figure it out.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` `````` stats.h // Function prototype declaration float Mean(const int *arr, size_t size); float Median(int *arr, size_t size); void Sort(int *arr, size_t size); stats.cpp #include "stats.h" // Get the mean from the array float Mean(const int *arr, size_t size) { float sum = 0; for(size_t i = 0; i < size; i++) sum += arr[i]; return sum / size; } // Get the median from the array float Median(int *arr, size_t size) { Sort(arr, size); float median; if(size % 2==0) { int middle = size / 2; median = (float)arr[middle]; } else { //there are two middle elements in array int middle1 = size / 2; int middle2 = (size / 2)-1; // find average of the two middle elements median = (float)((arr[middle1] + arr[middle2])/2.0); } return median; } // Sort the array in ascending order using insertion sort void Sort(int *arr, size_t size) { for(size_t i = 1; i < size; i++) { int t = arr[i]; size_t j = i; while(j > 0 && t < arr[j - 1]) { arr[j] = arr[j - 1]; j--; } arr[j] = t; } } main.cpp #include #include #include "stats.h" using namespace std; // Entry point of the program int main() { // I/O is handled by function main() int arr[100]; size_t size = 0; cout << "Enter a sequence of numbers separated by spaces (end it with a non-digit): "; // Keep on entering numbers until a non-digit is detected while(!cin.eof() && size < 100) { string value; cin >> value; // Attempt to convert input to number if(stringstream(value) >> arr[size]) { size++; continue; } break; } cout << "Mean: " << Mean(arr, size) << endl; cout << "Median: " << Median(arr, size) << endl; cout << "After sort: "; for(int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; system("pause"); return 0; } ``````
If you wanted to objectify this I suppose I would make a "DataSet" array paired with the various statistical methods.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````class DataSet { int arr[100]; int size; public: DataSet() : size(0) {} void add(int val) { arr[size++] = val; } int getData(int pos) { return arr[pos]; } int Size() { return size; } bool full() { return size >= 100; } float Mean() { // Your algorithms here int total = 0; for (int i = 0; i < size; ++i) total += arr[i]; return ((float)total) / ((float)size); } float Median() { /*...*/ } void Sort() { /*...*/ } }; int main() { DataSet ds; cout << "Enter a sequence of numbers ..."; while(!cin.eof() && !ds.full() ) { int value; cin >> value; ds.add(value); } cout << ds.Mean(); cout << ds.Median(); ds.Sort(); cout << "After sort: "; for (int i = 0; i < ds.Size(); ++i) cout << ds.getData(i) << ' '; }``````

Last edited on
gotcha, im really wanting to separate the code though... like stats.h, stats.cpp, main.cpp
Hi there,

.h files (header files) generally contain declarations only for instance:

 ``1234`` ``````class my_class { void my_method(int number); //declaration only };``````

.cpp file contain the actual definitions:

 ``1234`` ``````void my_class::my_method(int number) { std::cout << number++; }``````

main.cpp generally only contains the main function.

Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.