Hard Matrix question

I'm doing a practice problem, the question is long so it's better if I just paste it below.

I wrote a code for it below but I can't seem to get it working. I don't know how to create the src=dest line to make it crop the correct location from the dest array matrix. In my example I put a 4x4 matrix and I'm trying to crop a 2x3 region, starting from position 2x2. It should print 2 1 4 3 5 2. Is there any good references on C++ matrices?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
We want a function to make a copy of a rectangular region of an image (i.e., it makes a 

cropped copy). The parameters are src[], containing the input image; width and height, 

giving the original image size; startR and startC, giving the row and column in src for the 

top left corner of the portion of the image to be copied; subW and subH, giving the width 

and height of the region to copy; and dest[], which is the array for the destination image. 

Note that, when the function returns the top left corner of the copied portion of the image 

must be in dest[0] and the other pixels must follow contiguously after that in the usual 

manner. You may assume that the region to be copied does not exceed the bounds of src[] 

and that the size of the destination array is sufficient.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Example program
#include <iostream>
#include <string>
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]);
int main()
{
int src[]={5,4,2,6,4,2,1,4,2,3,5,2,3,1,3,6};//16
int dest[]={0};
pictureCrop(src,4,4,2,2,2,3, dest);
std::cout<<dest[0]<<" "<<dest[1]<<" "<<dest[2]<<" "<<std::endl<<dest[3]<<" "<<dest[4]<<" "<<dest[5];
return 0;
}
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]){
    for(int i=0;i<width;i++){
    int newWidth=i+startR;
    for(int j=0;j<height;j++){
    int newHeight=j+startC;
    dest[i*subW+j]=src[newWidth*subW+newHeight];
    
    }}
}
Last edited on
Changed my code a bit, still not working, I can get it to print what I want now but I think it's still wrong. Fixed some errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]);
int main()
{
int src[]={5,4,2,6,4,2,1,4,2,3,5,2,3,1,3,6};//16
int dest[]={0};
pictureCrop(src,4,4,2,2,3,2, dest);
std::cout<<dest[0]<<" "<<dest[1]<<" "<<dest[2]<<" "<<std::endl<<dest[3]<<" "<<dest[4]<<" "<<dest[5];
return 0;
}
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]){
    for(int i=0;i<subH;i++){
    for(int j=0;j<subW;j++){
   
    dest[i*subW+j]=src[(i+startR-1)*width+j+startC-1];
    
    }}
}
Last edited on
JSYK what you're doing is more of a C style approach as was instructed of you. The image is a resource and I am of a mind to think that resources ought to be handled by classes due to the whole Resource Allocation Is Initialization. BUT we arn't talking about that rn.
You don't need
#include <string>

here's some example code that works. Duoas save me:

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
//	Example program
//	<written by> <date>

//STD includes
#include <iostream> //#include <string> not needed

//Function Declaration(s)
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]);

//Main function //start to program
int main()
{
	int src[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};//16, 4x4. test.
	int dest[]={};
	pictureCrop(src, 4, 4, 2, 2, 2, 2, dest); //4x4 img; start @2,2; make 2x2 copy.
	std::cout << src[0] << src[1] << src[2] << src[3] << '\n';
	std::cout << src[4] << src[5] << src[6] << src[7] << '\n';
	std::cout << src[8] << src[9] << src[10] << src[11] << '\n';
	std::cout << src[12] << src[13] << src[14] << src[15] << '\n';
	std::cout << "\n\n"; //spacer
	std::cout << dest[0] << dest[1] << '\n';
	std::cout << dest[2] << dest[3] << '\n';
	return 0;
}


void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[])
{
	int count = 0; //counts through the dest array
	for (int i=startC; i<startC+subH; i++)
	{
		for (int j=startR; j<startR+subW; j++)
		{
			dest[count] = src[(width*i)+j]; //EOL math is (width*i)+j
			count++;
		}
	}
}


and the output...


1234
5678
9101112
13141516


1112
1516
Shouldn't the out put be 6 7 10 11 since it starts at 2x2? Also, how is dest[count] (count going 0-n) equal to what I had, which was dest[i*subW+j]? It seems to be functioning the same, yet my value does not go up by 1.

When I remove the minus 1's from line 17 in my code and use your main, I get the same output, but I don't understand why since our functions are so much different.
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
// Example program
#include <iostream>
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]);
int main()
{
	int src[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};//16, 4x4. test.
	int dest[]={};
	pictureCrop(src, 4, 4, 2, 2, 2, 2, dest); //4x4 img; start @2,2; make 2x2 copy.
	std::cout << src[0] << src[1] << src[2] << src[3] << '\n';
	std::cout << src[4] << src[5] << src[6] << src[7] << '\n';
	std::cout << src[8] << src[9] << src[10] << src[11] << '\n';
	std::cout << src[12] << src[13] << src[14] << src[15] << '\n';
	std::cout << "\n\n"; //spacer
	std::cout << dest[0] << dest[1] << '\n';
	std::cout << dest[2] << dest[3] << '\n';
	return 0;
}
void pictureCrop(int src[], int width, int height, int startR, int startC, int subW, int subH, int dest[]){
    int count=0;
    for(int i=0;i<subH;i++){
    for(int j=0;j<subW;j++){
   
    dest[count]=src[(i+startR)*width+j+startC];
    count ++;
    
    }}
}
Last edited on
Firstly, no. Since computers start counting at the number 0, I want the array element 2 not the 2nd element. This is something that you'll get used to eventually.For example:
1
2
3
src[0] == 1
src[1] == 2
src[2] == 3


You can experiment from there!
Secondly, that's because it's evaluating to the same numbers (0,1,2,3). You're logic isn't wrong, it's just unnecessary. The way I did it is not safe though as you could mess up somewhere or the picture and the copy be too big for your computer's stack, but that is not really relevant in beginner example problems. Where are you getting these examples from?

Third, there are always more than one way to do something in c++. The difference is and something you always have to keep in mind is what about the person who reads my code? can they deduce the logic of
 
dest[i*subW+j]=src[(i+startR-1)*width+j+startC-1];
?
or is is easier for someone to understand
 
dest[count] = src[(width*i)+j]

where count is always increasing by 1 each time the program loops.
dest has only one element, from initialiation.

Aceix.
Topic archived. No new replies allowed.