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
//Generating the Array
//===================================================================================
int **m = newint* [row]; //Array of Pointers
for (int i=0; i<row;i++) {
m[i] = newint [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;