class vector of dimension n

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Vector {
int ndim;
double* data;
public:
Vector( int n, double d[]);
~Vector();
Vector& operator+=(const Vector& q);
Vector& operator-=(const Vector& q);
double operator[](int i) const { return data[i]; };
friend Vector operator+(const Vector& p,const Vector& q);
friend Vector operator-(const Vector& p,const Vector& q);
friend double operator*(const Vector& p,const Vector& q);
friend ostream& operator<<(ostream& s, Vector& p);
};

Here ndim is the dimension of the vector and data is a pointer to the array elements.
Copy this declaration and write implementations for the eight undefined functions and operators. The addition and subtraction operators should be defined to carry out vector addition
and subtraction. The * operator should represent the scalar product.
The constructor takes as it arguments the vector dimension, n, and an existing pointer to an
array d. The vector elements in this array need to be copied to a new array on the freestore.
In order to allocate the space on the freestore to store the vector elements a special form of
new operator is required:
data = new double[n];
which will allocate space on the freestore for an array of n doubles. The corresponding delete
statement, which should appear in the destructor is:
delete[] data;
The overloaded operator [] is defined to return individual array elements.
good luck
closed account (z05DSL3A)
http://www.cplusplus.com/forum/beginner/1/#msg6680
http://www.cplusplus.com/forum/articles/1295/#msg4406
http://www.cplusplus.com/articles/how_to_ask/
this is hard
closed account (z05DSL3A)
It may well be hard but unless you ask specific questions and show that you are not just trying to get your code written for you, you are unlikely to get help.
I have actually written a few codes now but initialising the friend functions are pretty complex. especially when you are dealing with arrays. this is what i have so far. any suggestions?
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
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

class Vector {
int ndim;
double* data;

public:
Vector( int n, double d[]);
~Vector();
Vector& operator+=(const Vector& q);
Vector& operator-=(const Vector& q);
double operator[](int i) const { return data[i]; };
friend Vector operator+(const Vector& p,const Vector& q);
friend Vector operator-(const Vector& p,const Vector& q);
friend double operator*(const Vector& p,const Vector& q);
friend ostream& operator<<(ostream& s, Vector& p);
};

Vector::Vector( int n, double d[]){
                data =new double [n];
                for (int i =0; i<ndim; i++)
                double data = d[i];
                }

Vector::~Vector(){
       delete[] data;
       }
Vector::Vector& operator+=(const Vector& q){

       data+=q.data;
       return *this;
       }
Vector::Vector& operator-=(const Vector& q){
       data-=q.data;
       return *this;
       }
Vector operator+(const Vector& p,const Vector& q){
       Vector a = p;
       a+ = q;
       return a;
}
Vector operator-(const Vector& p,const Vector& q){
       
Vector a = p;
       a- = q;
       }
double operator*(const Vector& p,const Vector& q){
       return p.data*q.data + p.data*q.data;
       }
ostream& operator<<(ostream& s, Vector& p){
       return s <<"("<<p.data[i]<<" , "<<p.data[i]<<" , "<<p.[i]<<" , "<<p.[i]<<")";
       }

int main(int argc, char *argv[])
{
       data = new double[n];
      double a[]= {1.0, 2.0, 3.0, 4.0};
       
       Vector b(4, a);
       cout<<b<<endl;
      
       
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Defining the functions is this it?
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
Vector::Vector( int n, double d[]){
                data =new double [n];
                for (int i =0; i<ndim; i++)
                double data = d[i];
                }

Vector::~Vector(){
       delete[] data;
       }
Vector::Vector& operator+=(const Vector& q){
       data+=q.data;
       return *this;
       }
Vector::Vector& operator-=(const Vector& q){
       data-=q.data;
       return *this;
       }
Vector operator+(const Vector& p,const Vector& q){
       Vector a = p.data[i] + q.data[i];
       return Vector a;
}
Vector operator-(const Vector& p,const Vector& q){
       
       Vector b = p.data[i] - q.data[i];
       return Vector b;
       }
double operator*(const Vector& p,const Vector& q){
       double c = p.data[i]*q.data[i] + p.data[i]*q.data[i];
       }
ostream& operator<<(ostream& s, Vector& p){
       return s <<"("<<p.data[0]<<" , "<<p.data[1]<<" , "<<p.[2]<<" , "<<p.[3]<<")";
       }
line 26 is wrong. here you declare a bunch of locals that hide members and assign stuff to them. ths should just be data[i] = d[i];
operator += and -=, you can't add arrays like that. you'll just have to iterate and add every element.
operator - doesn't return anything.
[edit] operator + and -. actually the way you had it before was better.. [/edit]
operator *, again, you can't do that to arrays. you have to iterate through them and multiply each element. also, have a sum variable and add the result of each multiplication to it.
opeartor <<. i is undeclared.[edit] this is slightly better, but won't work if ndim != 4[/edit] print "(", then print each element in the array with a for loop, then print ")". and what is p.[i] supposed to be?
line 60, n and data aren't declared and you don't need them at all.
Last edited on
closed account (z05DSL3A)
I have had a quick look (i stress a quick look) Here is some code to help you along.
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
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

class Vector 
{
    int ndim;
    double* data;
    
public:
    Vector( int n, double d[]);
    ~Vector();
    Vector& operator+=(const Vector& q);
    Vector& operator-=(const Vector& q);
    double operator[](int i) const { return data[i]; };
    friend Vector operator+(const Vector& p,const Vector& q);
    friend Vector operator-(const Vector& p,const Vector& q);
    friend double operator*(const Vector& p,const Vector& q);
    friend ostream& operator<<(ostream& s, Vector& p);
};

Vector::Vector( int n, double d[])
{
    ndim = n;
    data = new double[n]
    for (int i =0; i < ndim; i++)
        data[i] = d[i];
}

Vector::~Vector()
{
    if(data != NULL)
    {
        delete[] data;
        data = NULL;
    }
}

Vector& Vector::operator+=(const Vector& q)
{
    for (int i =0; i<ndim; i++)
        data[i] += q.data[i];
    return *this;
}

Vector operator+(const Vector& p,const Vector& q)
{
    Vector a(p.ndim, p.data);
    a += q;
    return a;
}


int main(int argc, char *argv[])
{
    double a[4]= {1.0, 2.0, 3.0, 4.0};
    
    Vector b(4, a);

    
    return EXIT_SUCCESS;
}

Grey Wolf, what's with the ~Vector ? I mean, what purpose does that serve? In what case could (and should) something try to access a Vector object after it has been destroyed and not get errors if data is null? explicit destruction?
Is that a 'habit' or is there something I'm unaware of?
Also checking for NULL is redundant. deleting null pointers has no effect. Not to mention data can't be null anyway.

Just as good:

1
2
3
4
Vector::~Vector()
{
  delete[] data;
}



Also that class needs a copy ctor or it will explode.
closed account (z05DSL3A)
Sorry, it is habit. I tend to check pointers aren't NULL before I do anything with them.

Also that class needs a copy ctor or it will explode.

I hadn't noticed that the spec given didn't have one , I also said it was a quick look.
Last edited on
So Grey, in case of the operater* and ostream& operator<<(ostream& s, Vector& p); if i say
1
2
3
4
5
6
Vector operator*(const Vector& p,const Vector& q)
{
    double c = (p.ndim, p.data);
    c += q*p;
    return c;
}


and
1
2
3
ostream& operator<<(ostream& s, Vector& p){
       return s <<"("<<p.data[0]<<" , "<<p.data[1]<<" , "<<p.[2]<<" , "<<p.[3]<<")";
       }


would I expect the operator* to do the dot product and make use of this output using ostream& operator?
Topic archived. No new replies allowed.