Operator <<c++

Pages: 12
The next task is:
Implement the add method. The rectangle transferred as a parameter should be written to the next free position of the array. If there is no free space left in the array, the array should be doubled in size if it contains less than 100 elements. If it contains 100 or more elements, it will only be increased by 100 additional elements (if additional space is needed).


Is my code ok?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void CDrawing::add(const CRectangle& rectangle){
	m_rectangles[m_nextFree] = rectangle;
	
	if(m_arraySize < 100){
		m_rectangles[2*m_arraySize] = rectangle;
		
		
		
	}
	
	if(m_arraySize >=100 && m_arraySize ==0){
		m_rectangles[m_arraySize+100] = rectangle;
		m_arraySize++;
		
	}
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

void CDrawing::add(const CRectangle& rectangle){
	
	
	if(m_nextFree < m_arraySize ){
	m_rectangles[m_nextFree] = rectangle;
	
	}
	
	if(m_nextFree < 100){
		CRectangle* newRectangle = new CRectangle [m_arraySize -1];
		delete[] m_rectangles;
		newRectangle[2*m_nextFree] = rectangle;
		
		
		
	}
	
	if(m_nextFree >=100 && m_nextFreee == m_arraySize){
		newRectangle[m_nextFree+100] = rectangle;
		m_nextFree++;
		
	}


}



I think the idea is not 100% right?
Can somebody help?
Replying to your latest post:

If you tried to test your code, you'll probably very quickly see that it isn't correct.

First, you need to check whether or not you need to resize the array in the first place. Your if-statement on line 5 is doing this. But everything else you do is bizarre.

Let's say the initialize size of your m_rectangles array is 10, right?
And the initial value of m_nextFree should be 0, because start with 0 actual logical elements in your array. (I am assuming this, not 100% if that's actually how you want it to work.)

m_nextFree = 0 --> insert 1st item --> m_nextFree = 1
m_nextFree = 1 --> insert 2nd item --> m_nextFree = 2
m_nextFree = 2 --> insert 3rd item --> m_nextFree = 3
...
m_nextFree = 9 --> insert 10th item --> m_nextFree = 10
So, what happens when you try to insert the 11th item in the array? 10 is no longer a valid array index.
When this happens, you need to resize your dynamic array. When you resize an array, what you actually need to do is
(1) allocate a new array with a larger size (in this case 2x as many elements)
(2) copy over the existing elements of the old array into the new array
(3) delete the old array
(4) assign the old array's pointer to point to the new data.

I'll show an example of this with ints. You should be able to apply it to CRectangles as well.
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
// Example program
#include <iostream>
#include <string>

class Array {
  public:
    Array()
    : capacity(5), current_size(0)
    {
        arr = new int[capacity];
    }
    ~Array()
    {
        delete [] arr;
    }

    void add(int value)
    {
        if (current_size == capacity)
        {
            // need to re-allocate array with a bigger capacity, twice as big
            // I did not do this, but once you go above 100, you only add 100 each time you need to resize.
            // You need to implement that part yourself (it's a very simple if check)
            int new_capacity = capacity * 2;
            int* new_arr = new int[new_capacity];
            for (int i = 0; i < current_size; i++)
            {
                // copy over each existing element
                new_arr[i] = arr[i];
            }
            
            // delete old array, and make it point to new array
            delete [] arr;
            arr = new_arr;
            
            // update capacity
            capacity = new_capacity;
        }

        // add to next free index
        arr[current_size] = value;
        current_size++;
    }
    
  public: // making public just for demonstration purposes
    int capacity;
    int current_size;
    int* arr;
};

std::ostream& operator<<(std::ostream& os, const Array& array)
{
    for (int i = 0; i < array.current_size; i++)
    {
        os << array.arr[i] << " ";
    }
    return os;
}

int main()
{
    Array arr;
    
    for (int i = 0; i < 21; i++)
    {
        int value = 2 + i*i; // just some arbitrary value
        
        arr.add(value);
        std::cout << "added " << value << " to arr... ";
        std::cout << "  new size == " << arr.current_size << ", new capacity == " << arr.capacity << "\n";
        std::cout << "  new arr = " << arr << "\n\n";
    }
}

added 2 to arr...   new size == 1, new capacity == 5
  new arr = 2 

added 3 to arr...   new size == 2, new capacity == 5
  new arr = 2 3 

added 6 to arr...   new size == 3, new capacity == 5
  new arr = 2 3 6 

added 11 to arr...   new size == 4, new capacity == 5
  new arr = 2 3 6 11 

added 18 to arr...   new size == 5, new capacity == 5
  new arr = 2 3 6 11 18 

added 27 to arr...   new size == 6, new capacity == 10
  new arr = 2 3 6 11 18 27 

added 38 to arr...   new size == 7, new capacity == 10
  new arr = 2 3 6 11 18 27 38 

added 51 to arr...   new size == 8, new capacity == 10
  new arr = 2 3 6 11 18 27 38 51 

added 66 to arr...   new size == 9, new capacity == 10
  new arr = 2 3 6 11 18 27 38 51 66 

added 83 to arr...   new size == 10, new capacity == 10
  new arr = 2 3 6 11 18 27 38 51 66 83 

added 102 to arr...   new size == 11, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 

added 123 to arr...   new size == 12, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 

added 146 to arr...   new size == 13, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 

added 171 to arr...   new size == 14, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 

added 198 to arr...   new size == 15, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 

added 227 to arr...   new size == 16, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 

added 258 to arr...   new size == 17, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 

added 291 to arr...   new size == 18, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 

added 326 to arr...   new size == 19, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326 

added 363 to arr...   new size == 20, new capacity == 20
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326 363 

added 402 to arr...   new size == 21, new capacity == 40
  new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326 363 402 

Last edited on
I did it so how you told :

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
void CDrawing::add(const CRectangle& rectangle){




if(m_nextFree == m_arraySize ){


	CRectangle* newRectangle = new CRectangle [ m_arraySize*2];

	for(int i = 0; i<(m_arraySize*2);i++){

		newRectangle[i] = m_rectangles[i];

	}
	delete[] m_rectangles;
	m_rectangles = newRectangle;
	m_arraySize = (m_arraySize*2);

}
m_rectangles[m_nextFree] = rectangle;
m_nextFree++;


}



But they said in the task that I should do things when there are bigger than 100 Elements and less than 100 Elements?
I think I am the solution very close

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


void CDrawing::add(const CRectangle& rectangle){




if(m_nextFree == m_arraySize ){

    int m_newSize = m_arraySize*2;
	CRectangle* newRectangle = new CRectangle [ m_newSize];

	for(int i = 0; i<(m_newSize);i++){

		newRectangle[i] = m_rectangles[i];

	}
	delete[] m_rectangles;
	m_rectangles = newRectangle;
	
	m_arraySize = m_newSize;



}
m_rectangles[m_nextFree] = rectangle;
m_nextFree++;
if(m_arraySize < 100){
	m_newSize = m_arraySize*2;
}
else{
	m_newSize = m_arraySize+100;
}
}




Where should I add this

if(m_arraySize < 100){
m_newSize = m_arraySize*2;
}
else{
m_newSize = m_arraySize+100;
}
}


in the code?
Because it shows error?
Add it after line 10, that's when you need to know what the new size to create is...
Would it work so?

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
void CDrawing::add(const CRectangle& rectangle){


if(m_nextFree == m_arraySize ){
	int newSize = m_arraySize*2;
	CRectangle* newRectangle = new CRectangle [ newSize];
    for(int i = 0; i<(m_arraySize);i++){

		newRectangle[i] = m_rectangles[i];

    }
    delete[] m_rectangles;
    m_rectangles = newRectangle;

    m_arraySize = newSize;

    if(m_arraySize < 100){

    	newSize = m_arraySize*2;
    }

    else{
    	    newSize = m_arraySize+100;
         }
    }

     m_rectangles[m_nextFree] = rectangle;
     m_nextFree++;

     }






Use correct indentation. It's annoying to read your code when it's all over the place.
http://mrbool.com/importance-of-code-indentation/29079

No, that's not correct, and isn't what I said. You're still always calling new CRectangle[newSize] with newSize being m_arraySize * 2.

You need to determine what the new array size will be before you call line 6. (Whether to double the size, or add 100).

Move lines 17-26 to before line 6.
Last edited on
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
void CDrawing::add(const CRectangle& rectangle){
	
	  if(m_arraySize < 100){

	    	m_rectangles[ m_arraySize*2] = rectangle;
	    }

	    else{
	    	    m_rectangles[m_arraySize+100] = rectangle;
	         }
	    
if(m_nextFree == m_arraySize ){
	int newSize = m_arraySize*2;
	CRectangle* newRectangle = new CRectangle [ newSize];
    for(int i = 0; i<(m_arraySize);i++){

		newRectangle[i] = m_rectangles[i];

    }
    delete[] m_rectangles;
    m_rectangles = newRectangle;

    m_arraySize = newSize;

}

     m_rectangles[m_nextFree] = rectangle;
     m_nextFree++;

     }




I think that s 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

void CDrawing::add(const CRectangle& rectangle){

	std::size_t newSize;
	if( m_arraySize < 100 )
	    newSize = m_arraySize * 2;
	else
	    newSize = m_arraySize + 100;

if(m_nextFree == m_arraySize ){
	int newSize = m_arraySize*2;
	CRectangle* newRectangle = new CRectangle [ newSize];
    for(int i = 0; i<(m_arraySize);i++){

		newRectangle[i] = m_rectangles[i];

    }
    delete[] m_rectangles;
    m_rectangles = newRectangle;

    m_arraySize = newSize;

}

     m_rectangles[m_nextFree] = rectangle;
     m_nextFree++;

     }


Ok?
Now I have solved it .
next task:

Implement the drawFiltered method. The method "draws" all rectangles on the passed "screen object" that use the fill character specified as the parameter filter. If the value of the filter parameter is 0, all rectangles are drawn.

I have at the moment no idea how to solve?

My 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
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
101
102

#include "CRectangle.h"
#include "CPoint.h"

CRectangle::CRectangle(char fillChar)
{
	m_fillChar = fillChar;
}

CRectangle::CRectangle(CPoint bottomLeft, CPoint topRight, char fillChar)
{

	m_bottomLeft = bottomLeft;
	m_topRight = topRight;
	m_fillChar = fillChar;
	if(topRight.getX() < bottomLeft.getX()){

		topRight.setX(bottomLeft.getX());




	}

	if(topRight.getY() < bottomLeft.getY()){

			topRight.setY(bottomLeft.getY());




		}

}

void CRectangle::setCorners(CPoint bottomLeft, CPoint topRight)
{
	if (bottomLeft.getX()<= topRight.getX() && bottomLeft.getY() <= topRight.getY()) {
			m_bottomLeft = bottomLeft;
			m_topRight = topRight;
		}
}

CPoint CRectangle::getBottomLeftCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_bottomLeft;
}

CPoint CRectangle::getTopRightCorner() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_topRight;
}

void CRectangle::setFillChar(char fillChar)
{
	m_fillChar = fillChar;
}

char CRectangle::getFillChar() const
{
	// Bitte implementieren und dabei das return-Statement ersetzen.
	return m_fillChar;
}

bool CRectangle::operator ==(const CRectangle& other) const
{
	if(m_bottomLeft == other.m_bottomLeft&& m_topRight== other.m_topRight){
		return true;


	}
	else{

		return false;
	}

}

void CRectangle::draw(CScreen& screen) const
{


	 for(int j = m_bottomLeft.getX(); m_topRight.getX()>=j;j++){

		for(int i = m_bottomLeft.getY();  m_topRight.getY()>=i; i++) {
		    screen.setPoint(CPoint(j , i),m_fillChar);




		}
	    }


}
ostream& operator<< (ostream& lhs, const CRectangle& rhs){

	lhs << "CRectangle(" << rhs.getBottomLeftCorner().getX() << "," << rhs.getBottomLeftCorner().getY() << "),(" << rhs.getTopRightCorner().getX() <<","<< rhs.getTopRightCorner().getY()<< "," <<  rhs.getFillChar() << ")" <<  endl;
	return lhs;
}



draw cpp


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

#include<iostream>
using namespace std;
#include"CDrawing.h"

CDrawing::CDrawing(int initialSize){

	m_arraySize = initialSize;


if(m_arraySize < 1){

    m_arraySize = 10;


}
   m_rectangles = new CRectangle [m_arraySize];

}

CDrawing::~CDrawing(){

	delete [] m_rectangles;
}

void CDrawing::add(const CRectangle& rectangle){

	

if(m_nextFree == m_arraySize ){
	    std::size_t newSize;
		if( m_arraySize < 100 ){
		    newSize = m_arraySize * 2;
		}
		else{
		    newSize = m_arraySize + 100;
		}
	CRectangle* newRectangle = new CRectangle [ newSize];
    for(int i = 0; i<(m_arraySize);i++){

		newRectangle[i] = m_rectangles[i];

    }
    delete[] m_rectangles;
    m_rectangles = newRectangle;

    m_arraySize = newSize;

}
     m_rectangles[m_nextFree] = rectangle;
     m_nextFree++;

     }








https://www.pic-upload.de/view-35672492/Bildschirmfoto2018-07-22um21.28.44.png.html

Can somebody help?
Somebody there ?
Topic archived. No new replies allowed.
Pages: 12