Why does my code crash my program when I change one value? [C++]

closed account (oN3h0pDG)
I wrote a program for an assignment that takes pixel values and negates them with a 2D array. The 2D array parameters are controlled by a const height variable that determines the amount of columns or "height". All works fine when height is 5 but as soon as I change heights value (in this case 6)(in the code) to test a different output (also adding sixth array elements) it completely shuts down and doesn't work, it doesn't give me any errors but it says it has stopped working and I have no clue why.

If you can help me figure out why it crashes when i change the value of height and how I can fix it that would be greatly appreciated, thanks! :)

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
#include <iostream>
#include <iomanip>
using namespace std;

const int height = 5; // image height variable || Change to 6 or any other value it crashes??? 

// Procedure that takes the original grayscale image and turns it into a negated image, then displays it to the console.
void negatedImage(double imageI[height][5], int height) {

	double imageJ[5][height] = {imageI[height][5]};

	cout << "Negated Image: " << endl;

	for(int i=0; i < height; i++)  {  // This loops through the rows.

			for(int j=0; j < height; j++) { // This loops through the columns

					imageJ[i][j] = 1 - imageI[i][j];


					cout << setprecision(2) << fixed;
					cout << imageJ[i][j] << " ";


					}
			}

}

// Procedure that takes the grayscale image and displays it to the console.
void displayImageData(double imageI[height][5], int height) {

	cout << "Grayscale Image: " << endl;
	for(int i=0; i < height; i++)  {  // This loops through the rows.

			for(int j=0; j < height; j++) { // This loops through the columns


					cout << setprecision(2) << fixed;
					cout << imageI[i][j] << " ";


					}
			}

	cout << endl;

	negatedImage(imageI, height); // call procedure to negate the image
}

int main() {

// image pixels
double imageI[height][5] = {{ 0.00 , 0.25 , 0.50 , 0.25, 0.00 }
						,{ 0.20 , 0.40 , 0.60 , 0.80, 1.00 }
						,{ 0.25 , 0.50, 1.00, 0.50, 0.25 }
						,{ 0.20, 0.40, 0.60, 0.80, 1.00 }
						,{ 0.20, 0.40, 0.60, 0.80, 1.00 }};


displayImageData(imageI, height); // call procedure to display original image pixel numbers


	return 0;

}
Last edited on
Are you sure your using height in the correct position of your arrays? It appears to me that you transpose this value in several different places.
1
2
3
4
5
6
7
8
9
10
11
void negatedImage(double imageI[height][5], int height) {
double imageJ[5][height]

...

	for(int i=0; i < height; i++)  {  // This loops through the rows.

			for(int j=0; j < height; j++) { // This loops through the columns
...
					cout << imageI[i][j] << " ";


closed account (oN3h0pDG)
Looks like I must have overlooked that for the ImageJ array, it works now! Thank you! :)
Topic archived. No new replies allowed.