Dynamic Array Class and Functions

Hello! I'm doing a Dynamic Array class in which I have to do some statistical function to do some calculations. I'm trying to do one to calculate the Average of the array, but I get this error "argument of type Dynamic Array is incompatible with parameter of type *" The error is underlined when Im passing the x and y arrays to the 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
 #ifndef DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_H

class Dynamic_Array {

public:
	Dynamic_Array();
	Dynamic_Array(const Dynamic_Array&);
	~Dynamic_Array();
	const Dynamic_Array& operator = (const Dynamic_Array&);

	double& operator [](size_t); 
	const double& operator [] (size_t) const; 
	
	void push_back(double);
	void pop_back();
	size_t size() const;
	bool empty() const;

	double average(double arr[], int size); //Here is my function to calculate average


private:
	static const size_t DEFAULT_CAPACITY;
	size_t capacity;
	size_t number_of_items;
	double* data;
	void resize();

};

#endif 

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
#include "Dynamic_Array.h"
#include <stddef.h>
#include <cmath>
using namespace std;

double Dynamic_Array::average(double arr[], int size) {

	int sum = 0;
	for (size_t i = 0; i < size; i++) { 

		sum += arr[i];
	}
	return sum/ size;
}

bool Dynamic_Array::empty() const { return size() == 0; }


size_t Dynamic_Array::size() const { return number_of_items; }


void Dynamic_Array::pop_back() { number_of_items--; }


void Dynamic_Array::push_back(double value) {

	if (number_of_items == capacity) { resize(); }

	data[number_of_items++] = value; 
}

void Dynamic_Array::resize() {

	capacity *= 2;

	double* newArr = new double[capacity];

	for (size_t i = 0; i < number_of_items; i++) { newArr[i] = data[i]; }

	delete[]data;

	data = newArr;
}


double& Dynamic_Array:: operator [] (size_t index) { return data[index]; } 

const double& Dynamic_Array::operator [] (size_t index)const { return data[index]; } 


Dynamic_Array::~Dynamic_Array() {  //Delete all the dynamic allocated memory
	if (data != NULL) { delete data; }
}

Dynamic_Array::Dynamic_Array(const Dynamic_Array& other) {
	data = NULL;
	*this = other;

}

const Dynamic_Array& Dynamic_Array::operator= (const Dynamic_Array& rhs) {

	if (this != &rhs) { 

		if (data != NULL) {
			delete[] data; //Deleting dynamically allocated memory
			data = NULL;
		}
		
		number_of_items = rhs.number_of_items;
		capacity = rhs.capacity;

		if (capacity > 0) {
			
			data = new double[capacity];

			for (size_t i = 0; i < number_of_items; i++) {
				if (i < capacity) {
					data[i] = rhs.data[i];
				}
			}
		}
	}
	return *this; 
}


Dynamic_Array::Dynamic_Array() : capacity(DEFAULT_CAPACITY), number_of_items(0) { 

	data = new double[capacity]; 

}

const size_t Dynamic_Array::DEFAULT_CAPACITY = 10;

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
#include <iostream>
#include <fstream>
#include"Dynamic_Array.h"

using namespace std;

int main() {

    ifstream fin("input.txt");
    
    if (!fin) {
        cout << "<Error> Nonexistent Input File" << endl;
        system("pause");
        return -1;
    }
    ofstream fout("output.txt");

    Dynamic_Array x, y;

    double xVals, yVals;
    

    while (fin >> xVals >> yVals) {
        x.push_back(xVals);
        y.push_back(yVals);

    }
  
    int indexSize = x.size();
    fout << "Number of pairs read: " << indexSize << endl;
    fout << endl;
    double xpairAvg = x.average(x, indexSize), ypairAvg = y.average(y, indexSize);
    fout << "Algebraic average of x: " << xpairAvg << endl;
    fout << "Algebraic average of y: " << ypairAvg << endl;
    

    fin.close();
    fout.close();

	system("pause");
	return 0;
}
Last edited on
You've defined average() to take a regular C-style array as the first argument:

double average(double arr[], int size); //Here is my function to calculate average

When you call it at line 32 of main(), you're trying to pass a Dynamic_Array object as the first argument:

1
2
3
4
5
    Dynamic_Array x, y;

    // ..

    double xpairAvg = x.average(&x, indexSize), ypairAvg = y.average(&y, indexSize);


Not the same type at all.

EDIT:

Why are you passing in an array as an argument at all? Shouldn't Dynamic_Array::average() be operating on the object's own data member?
Last edited on
Ah I understand that, my bad I just started to learn C++ some months ago.
Do you mean the values the push_back() function is passing into the Dynamic Array object?
I don't understand your question. Can you clarify which part of my post you don't understand?
When you asked why I was passing in an array as an argument, you meant that I should pass the values that I'm storing here to the Dynamic Class function.
Since x and y are objects, then the object's data should be pass.

1
2
3
4
5
6
7
8
9
10
 Dynamic_Array x, y;

    double xVals, yVals;
    

    while (fin >> xVals >> yVals) {
        x.push_back(xVals);
        y.push_back(yVals);

    }

No, I meant:

MikeyBoy wrote:
Shouldn't Dynamic_Array::average() be operating on the object's own data member?
Can you explain it to me?
Let me ask you a question:

What is the data member of your class for?
that would be the double* data; ?

that would be the double* data; ?


How many other members called data do you see in your class?

Yes, it's the one defined as a pointer to a double.
Not any other member are called data.
So what value should I pass to the function in the main cpp so it can calculate the average of the numbers stored in x?
I think I have solved the issue! Thank you for making me think some things!
No problem. Hope it all worked out!
Registered users can post here. Sign in or register to post.