C++ function array

C++, how do I add function where it can find the mean/med of an array?
Please check my code, it gives me wrong output in the median function.

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

#include<iostream>
using namespace std;


class Search_Dynamic_Array
{
    public:
        Search_Dynamic_Array();

        int getArrSize();
        void getArray();
        void setArrsize(int);
        void setArray(int*);
        int getSum(int*);
        int getMed(int*);

    private:
        int arr_size;
        int arr[];
};


Search_Dynamic_Array::Search_Dynamic_Array()
{
    //ctor
}

int Search_Dynamic_Array::getArrSize()
{
    return arr_size;
}

void Search_Dynamic_Array::getArray()
{
    cout << "The elements of the array are: ";
    for(int i=0; i<arr_size; i++){
        cout << arr[i] << " ";
    }
}

void Search_Dynamic_Array::setArrsize(int s)
{
    arr_size=s;
}

void Search_Dynamic_Array::setArray(int a[])
{
    for(int i=0; i<arr_size; i++){
        arr[i]=a[i];
    }
}

int Search_Dynamic_Array::getSum(int a[])
{
    int sum;
    for(int i=0; i<arr_size; i++){
        sum +=arr[i];
    }
    return sum;

}

int Search_Dynamic_Array::getMed(int a[])
{
    int sum;
    for(int i=0; i<arr_size; i++){
         sum = sum + arr[i];
    }
    return (int)sum/arr_size;
}


int main()
{

    int s=1, a[s];

    cout << "Enter the size of the array: ";
    cin >> s;
    cout << "Enter " << s << " numbers:\n";

    for(int i=0; i<s; i++){
        cin >> a[i];
    }

    Search_Dynamic_Array sda;
    sda.setArrsize(s);
    sda.setArray(a);

    cout << "The size of the array is " << sda.getArrSize();
    cout << endl;
    sda.getArray();
    cout << "\nThe sum of the array is: " << sda.getSum(a);
    cout << "\nThe median of the array entered is: " << sda.getMed(a);
    return 0;
}
(1) You appear to be trying to find the MEAN, not the MEDIAN. Please clarify. (The "median" is what you get if you sort the data, then extract the middle value.)

(2) You haven't initialised sum to zero.

(3) If working in integer arithmetic then you will invoke truncation errors due to integer division. One or other operands should be double, as should the "mean" itself.
A compiler says:
main.cpp:19:13: warning: flexible array members are a C99 feature [-Wc99-extensions]
        int arr[];
            ^
main.cpp:53:38: warning: unused parameter 'a' [-Wunused-parameter]
int Search_Dynamic_Array::getSum(int a[])
                                     ^
main.cpp:57:9: warning: variable 'sum' is uninitialized when used here [-Wuninitialized]
        sum +=arr[i];
        ^~~
main.cpp:55:12: note: initialize the variable 'sum' to silence this warning
    int sum;
           ^
            = 0
main.cpp:63:38: warning: unused parameter 'a' [-Wunused-parameter]
int Search_Dynamic_Array::getMed(int a[])
                                     ^
main.cpp:76:16: warning: variable length arrays are a C99 feature [-Wvla-extension]
    int s=1, a[s];
               ^
main.cpp:76:16: note: read of non-const variable 's' is not allowed in a constant expression
main.cpp:76:9: note: declared here
    int s=1, a[s];
        ^
5 warnings generated.

Your code is not valid C++. There is most likely undefined behaviour.
You don't say what you are being asked to do. However, the class name makes reference to dynamic array. So using dynamic memory, then possibly something like (with mean rather than median):

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
#include <iostream>

class Search_Dynamic_Array {
public:
	Search_Dynamic_Array() {}
	~Search_Dynamic_Array() { delete[] arr; };

	Search_Dynamic_Array(const Search_Dynamic_Array&) = delete;
	Search_Dynamic_Array& operator=(const Search_Dynamic_Array&) = delete;

	size_t getArrSize() const;
	void getArray() const;
	void setArrsize(size_t);
	void setArray();
	int getSum() const;
	double getMean() const;

private:
	size_t arr_size {};
	int* arr {};
};

size_t Search_Dynamic_Array::getArrSize() const {
	return arr_size;
}

void Search_Dynamic_Array::getArray() const {
	std::cout << "The elements of the array are: ";

	for (size_t i {}; i < arr_size; ++i)
		std::cout << arr[i] << ' ';

	std::cout << '\n';
}

void Search_Dynamic_Array::setArrsize(size_t s) {
	if (s > arr_size) {
		delete[] arr;
		arr = new int[s] {};
	}

	arr_size = s;
}

void Search_Dynamic_Array::setArray() {
	std::cout << "Enter " << arr_size << " numbers:\n";

	for (size_t i {}; i < arr_size; ++i)
		std::cin >> arr[i];
}

int Search_Dynamic_Array::getSum() const {
	int sum {};

	for (size_t i {}; i < arr_size; ++i)
		sum += arr[i];

	return sum;
}

double Search_Dynamic_Array::getMean() const {
	return (getSum() + 0.0) / arr_size;
}

int main() {
	size_t s {};
	Search_Dynamic_Array sda;

	std::cout << "Enter the size of the array: ";
	std::cin >> s;

	sda.setArrsize(s);
	sda.setArray();

	std::cout << "The size of the array is " << sda.getArrSize() << '\n';
	sda.getArray();
	std::cout << "The sum of the array is: " << sda.getSum() << '\n';
	std::cout << "The mean of the array entered is: " << sda.getMean() << '\n';
}


If you mean something else, then you'll need to be more specific re the requirements.
Topic archived. No new replies allowed.