Resize canvas using pointer

Ok so First i have to create a Canvas using this fucntion
1
2
3
4
5
6
7
8
char ** allocate_canvas(int width, int height)
{
	char** arr = new char*[width];
	for (int i = 0; i < width; ++i) {
		arr[i] = new char[height];
	}
	return arr;
}


then i have to resize.... but i don't know how to do it so far this is what i have.

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
void resize_canvas(char *** Cp, int old_width, int old_height, int new_width, int new_height, char wc)
{
	if (old_width < new_width) {
		Cp = new char**[new_width];
		for (int i = new_width - old_width; i < new_width; ++i) {
			*Cp[i][new_height] = wc;
		}
	}

	////////
	else if (old_width > new_width) {
		for (int i = old_width; i > new_width; --i) {
			delete Cp[i];
		}
	}

	////////
	if (old_height < new_height) {
		for (int i = 0; i < new_width; ++i) {
			Cp[new_width][i] = new char[new_height];
		}
		for (int i = new_height - old_height; i < new_height; ++i) {
			*Cp[new_width][i] = wc;
		}
	}

	/////////
	else if (old_height > new_height) {
		for (int i = old_height; i > new_width; --i) {
			Cp[i] = new char*[new_height];
		}
	}
	
}


this is where its called in the main function
1
2
3
resize_canvas(&C, 5, 6, 5, 10, 'e');
resize_canvas(&C, 5, 10, 3, 15, 'f');
resize_canvas(&C, 3, 15, 4, 7, 'g'); 


could anyone help me fix my resize code T_T i'm struggling so much :<
Last edited on
main.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

#include <cassert>
#include <iostream>
#include "canvas.h"

using namespace std;

int main()
{
	char** C = allocate_canvas(4, 5);
	cout << "5% earned." << endl;

	wash_canvas(C, 4, 5, 'a');
	// C:
	// aaaaa
	// aaaaa
	// aaaaa
	// aaaaa
	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < 5; ++j)
			assert(C[i][j] == 'a');
	cout << "15% earned." << endl;
	
	wash_canvas(C, 2, 2, 'A');
	// C:
	// AAaaa
	// AAaaa
	// aaaaa
	// aaaaa
	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < 5; ++j)
			if (i < 2 && j < 2)
				assert(C[i][j] == 'A');
			else
				assert(C[i][j] == 'a');
	cout << "20% earned." << endl;

	C[3][4] = 'A';
	// C:
	// AAaaa
	// AAaaa
	// aaaaa
	// aaaaA
	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < 5; ++j)
			if ((i < 2 && j < 2) || (i == 3 && j == 4))
				assert(C[i][j] == 'A');
			else
				assert(C[i][j] == 'a');
	deallocate_canvas(C, 4);
	cout << "25% earned." << endl;

	C = allocate_canvas(1, 8);
	wash_canvas(C, 1, 8, 'c');
	// C:
	// cccccccc
	for (int j = 0; j < 8; ++j)
		assert(C[0][j] == 'c');
	deallocate_canvas(C, 1);
	cout << "30% earned." << endl;
	
	C = allocate_canvas(6, 7);
	wash_canvas(C, 6, 7, 'd');	
	// C:
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	C[0][0] = 'D';
	C[5][0] = 'E';
	C[0][6] = 'F';
	C[5][6] = 'G';
	// C:
	// DdddddF
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// EdddddG
	
	char** C2 = duplicate_canvas(C, 6, 7);
	// C2:
	// DdddddF
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// EdddddG
	assert(C2[0][0] == 'D');	
	assert(C2[5][0] == 'E');
	assert(C2[0][6] == 'F');	
	assert(C2[5][6] == 'G');	
	
	C[0][0] = 'd';
	C[5][0] = 'd';
	C[0][6] = 'd';
	C[5][6] = 'd';
	// C:
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// ddddddd
	// C2 is unchanged
	assert(C2[0][0] == 'D');	
	assert(C2[5][0] == 'E');
	assert(C2[0][6] == 'F');	
	assert(C2[5][6] == 'G');	
	
	deallocate_canvas(C2, 6);	
	cout << "50% earned." << endl;

	resize_canvas(&C, 5, 6, 5, 10, 'e');
	// C:
	// ddddddeeee 
	// ddddddeeee 
	// ddddddeeee 
	// ddddddeeee 
	// ddddddeeee 
	for (int i = 0; i < 5; ++i)
		for (int j = 0; j < 10; ++j)
			if (j < 6)
				assert(C[i][j] == 'd');
			else		
				assert(C[i][j] == 'e');
	cout << "60% earned." << endl;

	resize_canvas(&C, 5, 10, 3, 15, 'f');
	// C:
	// ddddddeeeefffff 
	// ddddddeeeefffff 
	// ddddddeeeefffff 
	for (int i = 0; i < 3; ++i)	
		for (int j = 0; j < 15; ++j)
			if (j < 6)
				assert(C[i][j] == 'd');
			else if (j < 10)	
				assert(C[i][j] == 'e');
			else 
				assert(C[i][j] == 'f');
	cout << "70% earned." << endl;

	resize_canvas(&C, 3, 15, 4, 7, 'g'); 
	// C:
	// dddddde
	// dddddde
	// dddddde
	// ggggggg
	for (int i = 0; i < 4; ++i)	
		for (int j = 0; j < 7; ++j)
		{
			if (i < 3 && j < 6)
				assert(C[i][j] == 'd');
			else if (i == 3)
				assert(C[i][j] == 'g');
 			else
				assert(C[i][j] == 'e');
		}
	cout << "80% earned." << endl;

	resize_canvas(&C, 4, 7, 5, 8, 'h'); 
	// C:
	// ddddddeh
	// ddddddeh
	// ddddddeh
	// gggggggh
	// hhhhhhhh
	for (int i = 0; i < 5; ++i)	
		for (int j = 0; j < 8; ++j)
		{
			if (i < 3 && j < 6)
				assert(C[i][j] == 'd');
			else if (i == 3 && j < 7)
				assert(C[i][j] == 'g');
 			else if (i < 3 && j == 6)
				assert(C[i][j] == 'e');
			else
				assert(C[i][j] == 'h');
		}
	cout << "90% earned." << endl;

	resize_canvas(&C, 5, 8, 0, 0, 'z');
	cout << "95% earned." << endl;
	resize_canvas(&C, 0, 0, 1000, 1000, 'q');
	deallocate_canvas(C, 1000);		
	cout << "100% earned." << endl;
}






canvas.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "canvas.h"

char ** allocate_canvas(int width, int height)
{
	char** arr = new char*[width];
	for (int i = 0; i < width; ++i) {
		arr[i] = new char[height];
	}
	return arr;
}

void deallocate_canvas(char ** C, int width)
{
	for (int i = 0; i < width; ++i) {
		delete C[i];
	}
	delete[]C;
}

void wash_canvas(char ** C, int width, int height, char wc)
{
	for (int colum = 0; colum < height; ++colum) {
		
		for (int row = 0; row < width; ++row) {
			C[row][colum] = wc;
		}
	}
}

char ** duplicate_canvas(char ** C, int width, int height)
{
	char **arr = new char*[width];
	for (int i = 0; i < width; ++i) {
		arr[i] = new char[height];
	}

	for (int colum = 0; colum < height; ++colum) {

		for (int row = 0; row < width; ++row) {
			arr[row][colum] = C[row][colum];
		}
	}

	return arr;
}

void resize_canvas(char *** Cp, int old_width, int old_height, int new_width, int new_height, char wc)
{
	if (old_width < new_width) {
		Cp = new char**[new_width];
		for (int i = new_width - old_width; i < new_width; ++i) {
			*Cp[i][new_height] = wc;
		}
	}

	////////
	else if (old_width > new_width) {
		for (int i = old_width; i > new_width; --i) {
			delete Cp[i];
		}
	}

	////////
	if (old_height < new_height) {
		for (int i = 0; i < new_width; ++i) {
			Cp[new_width][i] = new char[new_height];
		}
		for (int i = new_height - old_height; i < new_height; ++i) {
			*Cp[new_width][i] = wc;
		}
	}

	/////////
	else if (old_height > new_height) {
		for (int i = old_height; i > new_width; --i) {
			Cp[i] = new char*[new_height];
		}
	}
	
}

canvas.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

#ifndef CANVAS_H
#define CANVAS_H

#include <iostream>

using namespace std;

// In this homework, a canvas is an ASCII art image
// consisting of a rectangular grid of chararacter pixels. 

// In C++, this is represented as an array of char arrays,
// i.e., a char** that points to an array of char*s.
// Each char* (char array) is a column of pixels in the image.

// Returns a canvas of the given width and height.
// I.e. a char* array of length width, where each element is a 
// char array of length height.
char** allocate_canvas(int width, int height);

// Deletes all the parts of a canvas.
// First delete each array in the char* array,
// then delete the char* array.
void deallocate_canvas(char** C, int width);

// Replaces all of the pixels with the character wc.
void wash_canvas(char** C, int width, int height, char wc);

// Takes a canvas C of specified width and height, 
// and returns a duplicate canvas with the same size and pixels. 
char** duplicate_canvas(char** C, int width, int height);

// Changes the width and height of a given canvas.
// If the new version of the canvas has pixels not found in the old version,
// (e.g., the new version is wider) set the new pixels equal to wc.
void resize_canvas(char*** Cp, int old_width, int old_height, int new_width, int new_height, char wc); 

#endif

Line 15 of canvas.cpp should be delete[] C[i]; since C[i] is itself an array.

Use the functions that you write. For example, duplicate_canvas() should call allocate_canvas() to allocate the new one.

You're trying to optimize the resize_canvas() function by allocating only when you have to. I wouldn't do that now. Just create a new canvas using allocate_canvas() and copy / fill as appropriate.
Topic archived. No new replies allowed.