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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
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 <iostream>
#include <sstream>
#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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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:

1
2
3
4
class my_class
{
    void my_method(int number); //declaration only
};


.cpp file contain the actual definitions:

1
2
3
4
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.