#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <vector>
usingnamespace std;
constint cols=3;
constint rows=3;
//---------------------------------------VARIABLES-------------
longint perc[rows+1][cols+1];
longint ArrayDEF;
longint elements=cols*rows;
longint n=1;
longint V=n;
longint rem=5;
//---------------------------------------BODY------------------
int main()
{
//------------------------------------
srand (time(0));
/*
the random function needs to create the string of random numbers here, then set this value equal to rem at each instance
*/
//------------------------------------
if (!rem)
{
longint V=0;
for(int y=1;y<rows+1;y++) //this function defines the number of row in the array
for(int x=1;x<cols+1;x++) //this function defines number of columns in the array
perc[x][y]=V++;
}
else {
longint V=n;
for(int y=1;y<rows+1;y++) //this function defines the number of row in the array
for(int x=1;x<cols+1;x++) //this function defines number of columns in the array
perc[x][y]=V++;
}
//------------------------------------
cout<<"rows="<<rows<<endl;
cout<<"columns="<<cols<<endl; //displays rows, columns, and elements on screen
cout<<"elements="<<elements<<endl;
//------------------------------------
/*for(int y=1;y<rows+1;y++) //this function defines the number of row in the array
for(int x=1;x<cols+1;x++) //this function defines number of columns in the array
perc[x][y]=V++;*/ //builds the array...IN MEMORY
//------------------------------------
for(int y=1;y<rows+1;y++) //prints the rows
{
for(int x=1;x<cols+1;x++) //prints the columns
cout<<perc[x][y]<<" "; //displays the final product
cout<<endl; //creates a new line for each row
}
//------------------------------------
}
So, basically it's replace a cell value with a zero. Althought it's a 2d array, you can think of the memory it occupies like this:
[1][2][3][4][5][6][7][8][9]
To get a pointer to the first element of your grid you would need to get the address thus:
long* p = &perc[0][0];
Then to change the cell currently occupied by the value '5' you would just increment your pointer by 4 (remember we are dealing with a zero based index) and replace the value '5' with '0':
*(p+=4) = 0;
The
p+=4
merely increments our pointer from the address for cell[0][0] to the address occupied by cell[1][1]
The preceding asterrisk is the de-reference operator so that we can update the address value, which is the same as:
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <vector>
usingnamespace std;
constint cols=3;
constint rows=3;
//---------------------------------------VARIABLES-------------
longint perc[rows][cols];
longint ArrayDEF;
longint elements=cols*rows;
longint n=1;
longint V=n;
longint rem=5;
//---------------------------------------BODY------------------
int main()
{
//------------------------------------
srand (time(0));
/*
the random function needs to create the string of random numbers here, then set this value equal to rem at each instance
*/
//------------------------------------
if (!rem)
{
longint V=0;
for(int y=0;y<rows;y++) //this function defines the number of row in the array
for(int x=0;x<cols;x++) //this function defines number of columns in the array
perc[x][y]=V++;
}
else {
longint V=n;
for(int y=0;y<rows;y++) //this function defines the number of row in the array
for(int x=0;x<cols;x++) //this function defines number of columns in the array
perc[x][y]=V++;
}
//------------------------------------
cout<<"rows="<<rows<<endl;
cout<<"columns="<<cols<<endl; //displays rows, columns, and elements on screen
cout<<"elements="<<elements<<endl;
//------------------------------------
/*for(int y=1;y<rows+1;y++) //this function defines the number of row in the array
for(int x=1;x<cols+1;x++) //this function defines number of columns in the array
perc[x][y]=V++;*/ //builds the array...IN MEMORY
//------------------------------------
for(int y=0;y<rows;y++) //prints the rows
{
for(int x=0;x<cols;x++) //prints the columns
cout<<perc[x][y]<<" "; //displays the final product
cout<<endl; //creates a new line for each row
}
//------------------------------------
// remove elemet at index 5, i.e. replace value with '0':
long* p = &perc[0][0];
*(p+=4) = 0;
}
So, basically it's replace a cell value with a zero. Althought it's a 2d array, you can think of the memory it occupies like this:
[1][2][3][4][5][6][7][8][9]
To get a pointer to the first element of your grid you would need to get the address thus:
long* p = &perc[0][0];
Then to change the cell currently occupied by the value '5' you would just increment your pointer by 4 (remember we are dealing with a zero based index) and replace the value '5' with '0':
*(p+=4) = 0;
The
p+=4
merely increments our pointer from the address for cell[0][0] to the address occupied by cell[1][1]
The preceding asterrisk is the de-reference operator so that we can update the address value, which is the same as:
1
2
p+=4;
*p = 0;
Okay! I hadn't thought of using pointers! That solved my problem. Thanks everybody!