2D array access violation

Hi, first time poster old time reader :) ...
ok, i have a problem with this homework that is due tonight, i have finished it but can't seem to figure out why i get an Access violation error when i run it.
The program is supposed read the number of Rows and Columns and allocates the array, and initializes its elements with a random number between -4 to 4. Then it calls a function (i will work on this later, its easy) that for each array[i][j] = 0 the entire row i = 0, and column j = 0; (they form a + mark where they meet at the original zero).
The Problem: when i start to read M[i][j]=5<--- (u will see why in the code) turn the rows and cols to zero. it Doesn't read m i j except if the number of rows & columns are larger than 5.. weird. there i get the access violation

PS: i have a header file, that is where the mysterious Random number function comes from, it results in ints from -4 to 4

here is 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
										//Generating the Array
		//===================================================================================
		int **m = new int* [row]; //Array of Pointers 
		for (int i=0; i<row;i++) {
			m[i] = new int [col]; //Each Element of the array points to another POINTER

			for (int j=0; j<col; j++) {
				m[i][j] = random(-4,4,100); //Adds a floating random number to each element 
											//in the array
				
			}
		}

										//Printing the Array
		//===================================================================================
		cout<<"Print The Array?? ";cin.get(); //just a brief holder ... so we don't jump fast
		cout<<"\n\n\n"; //easier on the eyes XD... 

		for (int i=0 ; i<row ; i++){
			for (int j = 0 ; j < col; j++) {
				cout<<"M["<<i<<"]["<<j<<"] = "<<m[i][j]<<"\t";
			}
			cout<<endl;
		}
		

									//THE ZERO FUNCTION
		//===================================================================================
		//Locate all the zeros

		for (int i=0 ; i<row ; i++){
			for (int j = 0 ; j < col; j++) {
				if (m[i][j] == 0) {

					m[i][j]=5;
				}
			}
		}
										//Turn 5into zeros
		//==================================================================================

		for (int i=0 ; i<row ; i++){
			for (int j = 0 ; j < col; j++) {
				if (m[i][j] == 5) {
					//Turn entire Row to 0
					for (int x=0;x<col;x++) {
						m[i][x] = 0;
					}


					//Turn entire Col to 0
					for (int x=0;x<row;x++) {
						m[x,j] = 0;
					}
				}
			}
		}
												//Printing the Array
		//===================================================================================
		cout<<"Print The Array?? ";cin.get(); //just a brief holder ... so we don't jump fast
		cout<<"\n\n\n"; //easier on the eyes XD... 

		for (int i=0 ; i<row ; i++){
			for (int j = 0 ; j < col; j++) {
				cout<<"M["<<i<<"]["<<j<<"] = "<<m[i][j]<<"\t";
			}
			cout<<endl;
		}


								//Deallocating the Memory of the Array
		//===================================================================================
		/*
		here there is something important happening... 
		we don't delete the pointer its self, we delete the parent
		pointer, eg, m[i][j]. we delete m[i], which will delete the Js 
		etc... so for m[x1][x2][x3]...[xi]
		we delete to [xi-1] and keep on deleting ontil we reach delete m[]
		which will delete m[x1]; 
		*/
		for (int i=0 ; i<row ; i++){
				delete [] m[i];
		}
		delete [] m;


m[x,j] = 0;
This line is incorrect.
Moschops .... :) .. it worked :P ...

I love you

i cannot believe it was that simple... damn XD
Topic archived. No new replies allowed.