Checking values of adjacent elements in a 2D array
Mar 30, 2013 at 10:09pm UTC
Hey,
So I currently have a program that can create an array of two dimensions.
ie.
111
111
111
This program then removes random elements from the array using a removal vector.
ie. {2}
101
111
111
What I am now trying to do is check if you have the fifth element (in this case the center element) if the horizontal and adjacent elements are 0.
ie. would return true
101
010
101
but
101
110
101
would return false, because not all four adjacent elements are zero.
I feel like an if statement would likely work but I keep getting parse issues.
My code is below.
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
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
/*------Define Variables-----*/
long int cols;
long int rows;
long int rem;
/*long int verify;*/
long int testver=5;
long int myrandom (int i) { return rand()%i;}
/*--------------------------------*/
/*----------MAIN PROGRAM----------*/
/*--------------------------------*/
int main()
{
/*----------User Interface-----------*/
cout<<"Caution: Values larger than 50 might crash program" <<endl;
cout<<"Input Rows and press enter:" <<endl;
cin >> cols;
cout<<"Input Columns and press enter:" <<endl;
cin >>rows;
cout<<"What element would you like to verify?" <<endl;
/*cin>>verify;*/
long int elements=cols*rows;
long int perc[rows][cols];
cout<<"rows=" <<rows<<endl;
cout<<"columns=" <<cols<<endl;
cout<<"elements=" <<elements<<endl;
/* cout<<"verify="<<verify<<endl;*/
/*-----------Creates Random Vector of rows*cols Elements-----------*/
srand ( unsigned ( time(0) ) );
vector<int > myvector;
for (int i=1; i<=elements; ++i) myvector.push_back(i);
random_shuffle ( myvector.begin(), myvector.end() );
random_shuffle ( myvector.begin(), myvector.end(), myrandom);
cout << "myvector contains: {" ;
for (vector<int >::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it <<"," ;
cout <<"}" << '\n' ;
/*----------Builds Array in Memory-----------*/
if (!rem)
{
for (int x=0;x<rows;x++)
for (int y=0;y<cols;y++)
perc[x][y]=1;
}
/*------------Removal Loop and Pointer------------*/
for (long int seq=0;seq<elements;seq++)
{
rem=myvector[seq];
long * p = &perc[0][0];
*(p+=rem-1) = 0;
cout<<"rem=" <<rem<<endl;
/*------------Verification Step------------*/
/*------------Final Printout------------*/
for (int x=0;x<rows;x++)
{
for (int y=0;y<cols;y++)
cout<<perc[x][y]<<" " ;
cout<<endl;
}
}
cout<<"The Array Has Been Emptied" ;
return EXIT_SUCCESS;
}
/*----------END PROGRAM-----------*/
Let me know if you have any ideas or suggestions!
Thanks!
Topic archived. No new replies allowed.