doubleArray question

Pages: 12
Ah, I didn't see that. This problem is getting a bit ridiculous. Why don't we just make it a circular linked list of pointers to point objects while we're at it? :/
This is still quite standard "traffic rules" homework. Understanding pointers is critical; you can do a lot without them, but when you have to use them you better know them.

Linked list is an another "traffic rule" test.

(C++14 tries hard to deprecate new but that is "operating a car".)


Someone else had a much more elaborate "pointless" pointer excercise recently. Not a thing you would use in production code, but a pretty thought-provoker nevertheless.
This question is more complex than I thought.
So what I have to do is create a Points object, then take array of Points as parameter.

So how am I going to take array of this object as parameter?Because taking regular ints was not hard enough.

I just woke up and did this>
Would this be fine enough or I have to add more stuff?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#ifndef POINT_H_
#define POINT_H_
class Point {
double x, y;
public:
Point(); // default ctor
Point(double x, double y); // overloaded ctor
Point(const Point& p); // copy ctor
Point& operator= (const Point& p); // assignment operator
double getX() const;
double getY() const;
void setX(double x);
void setY(double y);
friend void printPoint(const Point& p);
~Point();
};
#endif /* POINT_H_ */



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

#include "Point.h"
#include <iostream>
using namespace std;
Point::Point() {
x = y = 0;
//cout<<"Default cTor called"<<endl;
}
Point::Point(double x, double y) {
this->x = x;
this->y = y;
//cout<<"cTor called with "<<x<<" and "<<y<<endl;
}
Point::Point(const Point& p) {
this->x = p.x;
this->y = p.y;
//cout<<"Copy cTor called with "<<x<<" and "<<y<<endl;
}


Point& Point::operator =(const Point& p) {
this->x = p.x;
this->y = p.y;
//cout<<"Assignment operator called with "<<x<<" and 
"<<y<<endl;
return *this;
}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
void Point::setX(double x) {
this->x = x;
}
void Point::setY(double y) {
this->y = y;
}
Point::~Point() {
//cout<<"dTor called for "<<x<<" and "<<y<<endl;
}
 
Last edited on
You should use better variables for assignment. x and y are fine in the class since it's a point, but you shouldn't assign them to a variable of the same name. Use newX and newY or X and Y, or something else so they're not exactly the same.

In your program you just have to replace int* with Point*.
Ther is a problem tho, when I change the int* to Point* I can't make
Point arr[] = {1, 1, 2 ,2 ,5 ,5};
Since Point is a object now, I can't put ints inside?
What do I have to do?
Last edited on
Put Point objects inside.
Point arr[] = {Point(1,1), Point(2,2),Point(5,5)};
should work.
Thank you.

I also changed some other parts.
Now I am unable to print anything since cout doesn't let me print the Point object.
Is there any way to print it with cout or I have to do something else?

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

Point* doubleArray (Point arg[], int size) {
  
  int rsize= size*2;
  static Point* array = new Point[rsize];//New array

	  for(int i=0; i<size; i++){
		  array[i]= arg[i];
		}//Copy the passed array to new array

	  for(int i=size; i<rsize; i++){
		  array[i]=Point(0,0);
		}//Add default zeros

	  return array;//return the new array with 
					//double the size and defualt zeros

}//EndofdoubleArray

int main (){
  //Point arr[6];
	Point arr[] = {Point(1,1), Point(2,2), Point(5,5)};
	Point *newArr = doubleArray(arr,6);

  for(int i=0; i<6; i++){
	cout<<newArr[i]; //print the doubleArray
  
  }
  delete [] newArr;

  system ("PAUSE");
  return 0;
}//Endofmain


cout << arr[i].x << ' ' << arr[i].y << '\n';
You could add commas and parentheses too if you want.
Yes thank you this worked.
cout<<arr[i].getX()<< ' ' <<arr[i].getY()<< '\n';

I do not get the zeroes at the end, I get garbage value.

I change this

1
2
3
4
5

  for(int i=size; i<rsize; i++){
		  array[i]=Point(0,0);
		}//Add default zeros


into this thinking that it would do the job but no luck
1
2
3
4
5
6
7

for(int i=size; i<rsize; i++){
		  array[i].setX(0);
		  array[i].setY(0);
		}//Add default zeros


If you didn't change the variable names, it may be reading this this->x = x; as this this->x = this->x; which would be garbage.
I do not think that is the problem since this is the output with the print statements.



cTor called with 1 and 1
cTor called with 2 and 2
cTor called with 5 and 5
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Default cTor called
Assignment operator called with 1 and 1
Assignment operator called with 2 and 2
Assignment operator called with 5 and 5
Assignment operator called with 1.00392e+035 and 4.73116e-306
Assignment operator called with 1.89078e-307 and 2.12648e-314
Assignment operator called with 3.68939e-308 and 5.90184e-315
1 1
2 2
5 5
1.00392e+035 4.73116e-306
1.89078e-307 2.12648e-314
3.68939e-308 5.90184e-315
dTor called for 0 and 0
dTor called for 0 and 0
dTor called for 0 and 0
dTor called for 0 and 0
dTor called for 0 and 0
dTor called for 0 and 0
dTor called for 3.68939e-308 and 5.90184e-315
dTor called for 1.89078e-307 and 2.12648e-314
dTor called for 1.00392e+035 and 4.73116e-306
dTor called for 5 and 5
dTor called for 2 and 2
dTor called for 1 and 1
Press any key to continue . . .

Can you post all of your current code?
This is the whole code.


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

Point::Point() {
x = y = 0;

//cout<<"Default cTor called"<<endl;
}
Point::Point(double x, double y) {
this->Newx = x;
this->Newy = y;
//cout<<"cTor called with "<<x<<" and "<<y<<endl;
}
Point::Point(const Point& p) {
this->Newx = p.x;
this->Newy = p.y;
//cout<<"Copy cTor called with "<<x<<" and "<<y<<endl;
}


Point& Point::operator =(const Point& p) {
this->Newx = p.x;
this->Newy = p.y;
//cout<<"Assignment operator called with "<<x<<" and "<<y<<endl;
return *this;
}
double Point::getX() const {
return Newx;
}
double Point::getY() const {
return Newy;
}
void Point::setX(double x) {
this->Newx = x;
}
void Point::setY(double y) {
this->Newy = y;
}
Point::~Point() {
//cout<<"dTor called for "<<x<<" and "<<y<<endl;
}



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

#ifndef POINT_H_
#define POINT_H_


class Point {
		double x, y;
		double Newy;
		double Newx;
	public:
		Point(); // default ctor
		Point(double x, double y); // overloaded ctor
		Point(const Point& p); // copy ctor
		Point& operator= (const Point& p); // assignment operator
		
		double getX() const;
		double getY() const;
		void setX(double x);
		void setY(double y);
		friend void printPoint(const Point& p);
		~Point();
};

#endif /* POINT_H_ */



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

Point* doubleArray (Point arg[], int size) {
  
  int rsize= size*2;
  static Point* array = new Point[rsize];//New array

	  for(int i=0; i<size; i++){
		  array[i]= arg[i];
		}//Copy the passed array to new array

	  for(int i=size; i<rsize; i++){
		  array[i].setX(0);
		  array[i].setY(0);
		}//Add default zeros

	  return array;//return the new array with 
					//double the size and defualt zeros

}//EndofdoubleArray

int main (){
  
	Point arr[] = {Point(1,1), Point(2,2), Point(5,5)};
	Point *newArr = doubleArray(arr,6);

  for(int i=0; i<6; i++){
	cout<<arr[i].getX()<< ' ' <<arr[i].getY()<< '\n'; //print the doubleArray
  
  }
  delete [] newArr;

  system ("PAUSE");
  return 0;
}//Endofmain



Point *newArr = doubleArray(arr,6);
arr only has 3 items in it, not 6.

Also, your Point class has x, y, Newx, and Newy variables. You should only use two of them, not all four.
How come, does each Point(x,y) counts as 1 in the array?

I changed it, but still garbage value instead of 0s.

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

#include "Point.h"
#include <iostream>

using namespace std;


Point::Point() {
Newx = Newy = 0;

//cout<<"Default cTor called"<<endl;
}
Point::Point(double x, double y) {
this->Newx = x;
this->Newy = y;
//cout<<"cTor called with "<<x<<" and "<<y<<endl;
}
Point::Point(const Point& p) {
this->Newx = p.Newx;
this->Newy = p.Newy;
//cout<<"Copy cTor called with "<<x<<" and "<<y<<endl;
}


Point& Point::operator =(const Point& p) {
this->Newx = p.Newx;
this->Newy = p.Newy;
//cout<<"Assignment operator called with "<<x<<" and "<<y<<endl;
return *this;
}
double Point::getX() const {
return Newx;
}
double Point::getY() const {
return Newy;
}
void Point::setX(double x) {
this->Newx = x;
}
void Point::setY(double y) {
this->Newy = y;
}
Point::~Point() {
//cout<<"dTor called for "<<x<<" and "<<y<<endl;
}
Ok found the mistake I was trying to prin arr rather then newArr.

Thank you so much for your help, you have been really helpful.
Lets touch some points:

* Member initialization lists http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=172

* Friends are not nice http://herbsutter.com/2014/01/06/gotw-7c-solution-minimizing-compile-time-dependencies-part-3/

* Overloading I/O operators http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

* Magic numbers are not nice either
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
std::ostream& operator<< ( std::ostream & out, const Point & rhs )
{
  out << '(' << rhs.getX() << ',' << rhs.getY() << ')';
}

template <typename T, size_t N>
inline
size_t SizeOfArray( const T(&)[ N ] )
{
  return N;
}

int main ()
{
  Point arr[] = { Point(1,1), Point(2,2), Point(5,5) };
  const size_t arrSize = SizeOfArray( arr );  // const size_t arrSize = 3;

  Point *newArr = doubleArray( arr, arrSize );
  const size_t newSize = 2 * arrSize; // doubleArray promises to double, and so should we

  for( size_t i=0; i < newSize; ++i ) {
    if ( i ) std::cout << ' ';

    std::cout << newArr[ i ];
  }
  std::cout << '\n';

  delete [] newArr;
  return 0;
}

* Array size http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/
Topic archived. No new replies allowed.
Pages: 12