I would like to kill two birds with one stone, as the questions are very similiar:
1:
I followed this code on github Smith Waterman Alignment to create the smith-waterman in C++. After some research I understood that implementing
double H[N_a+1][N_b+1]; is not possible (anymore) for the "newer" C++ versions. So to create a constant variable I changed this line to:
double **H = new double*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
H[i] = new double[nSynth + 1];
and also the same scheme for int I_i[N_a+1][N_b+1], I_j[N_a+1][N_b+1]; and so one (well, everywhere, where a two dimensional array exists). Now I'm getting the exception:
Unhandled exception at 0x00007FFF7B413C58 in Smith-Waterman.exe: Microsoft C
++ exception: std :: bad_alloc at location 0x0000008FF4F9FA50.
What is wrong here? Already debugged, and the program throws the exceptions above the for (int i = 0; i < nReal + 1; i++).
2: This code uses std::strings as parameters. Would it be also possible to create a smith waterman algortihm for cv::Mat?
For maybe more clarification, my full code looks like this:
#include "BinaryAlignment.h"
#include "WallMapping.h"
//using declarations
using namespace cv;
using namespace std;
//global variables
std::string bin;
cv::Mat temp;
std::stringstream sstrMat;
const int maxMismatch = 2;
const float mu = 0.33f;
const float delta = 1.33;
int ind;
BinaryAlignment::BinaryAlignment() { }
BinaryAlignment::~BinaryAlignment() { }
/**
*** Convert matrix to binary sequence
**/
std::string BinaryAlignment::matToBin(cv::Mat src, std::experimental::filesystem::path path) {
cv::Mat linesMat = WallMapping::wallMapping(src, path);
for (int i = 0; i < linesMat.size().height; i++) {
for (int j = 0; j < linesMat.size().width; j++) {
if (linesMat.at<Vec3b>(i, j)[0] == 0
&& linesMat.at<Vec3b>(i, j)[1] == 0
&& linesMat.at<Vec3b>(i, j)[2] == 255) {
src.at<int>(i, j) = 1;
}
else {
src.at<int>(i, j) = 0;
}
sstrMat << src.at<int>(i, j);
}
}
bin = sstrMat.str();
return bin;
}
double BinaryAlignment::similarityScore(char a, char b) {
double result;
if (a == b)
result = 1;
else
result = -mu;
return result;
}
double BinaryAlignment::findArrayMax(double array[], int length) {
double max = array[0];
ind = 0;
for (int i = 1; i < length; i++) {
if (array[i] > max) {
max = array[i];
ind = i;
}
}
return max;
}
/**
*** Smith-Waterman alignment for given sequences
**/
int BinaryAlignment::watermanAlign(std::string seqSynth, std::string seqReal, bool viableAlignment) {
const int nSynth = seqSynth.length(); //length of sequences
const int nReal = seqReal.length();
//H[nSynth + 1][nReal + 1]
double **H = new double*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
H[i] = new double[nSynth + 1];
cout << "passt";
for (int m = 0; m <= nSynth; m++)
for (int n = 0; n <= nReal; n++)
H[m][n] = 0;
double temp[4];
int **Ii = new int*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
Ii[i] = new int[nSynth + 1];
int **Ij = new int*[nReal + 1];
for (int i = 0; i < nReal + 1; i++)
Ij[i] = new int[nSynth + 1];
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
temp[0] = H[i - 1][j - 1] + similarityScore(seqSynth[i - 1], seqReal[j - 1]);
temp[1] = H[i - 1][j] - delta;
temp[2] = H[i][j - 1] - delta;
temp[3] = 0;
H[i][j] = findArrayMax(temp, 4);
switch (ind) {
case 0: // score in (i,j) stems from a match/mismatch
Ii[i][j] = i - 1;
Ij[i][j] = j - 1;
break;
case 1: // score in (i,j) stems from a deletion in sequence A
Ii[i][j] = i - 1;
Ij[i][j] = j;
break;
case 2: // score in (i,j) stems from a deletion in sequence B
Ii[i][j] = i;
Ij[i][j] = j - 1;
break;
case 3: // (i,j) is the beginning of a subsequence
Ii[i][j] = i;
Ij[i][j] = j;
break;
}
}
}
//Print matrix H to console
std::cout << "**********************************************" << std::endl;
std::cout << "The scoring matrix is given by " << std::endl << std::endl;
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
std::cout << H[i][j] << " ";
}
std::cout << std::endl;
}
//search H for the moaximal score
double Hmax = 0;
int imax = 0, jmax = 0;
for (int i = 1; i <= nSynth; i++) {
for (int j = 1; j <= nReal; j++) {
if (H[i][j] > Hmax) {
Hmax = H[i][j];
imax = i;
jmax = j;
}
}
}
std::cout << Hmax << endl;
std::cout << nSynth << ", " << nReal << ", " << imax << ", " << jmax << std::endl;
std::cout << "max score: " << Hmax << std::endl;
std::cout << "alignment index: " << (imax - jmax) << std::endl;
//Backtracing from Hmax
int icurrent = imax, jcurrent = jmax;
int inext = Ii[icurrent][jcurrent];
int jnext = Ij[icurrent][jcurrent];
int tick = 0;
char *consensusSynth = new char[nSynth + nReal + 2];
char *consensusReal = new char[nSynth + nReal + 2];
while (((icurrent != inext) || (jcurrent != jnext)) && (jnext >= 0) && (inext >= 0)) {
if (inext == icurrent)
consensusSynth[tick] = '-'; //deletion in A
else
consensusSynth[tick] = seqSynth[icurrent - 1]; //match / mismatch in A
if (jnext == jcurrent)
consensusReal[tick] = '-'; //deletion in B
else
consensusReal[tick] = seqReal[jcurrent - 1]; //match/mismatch in B
//fix for adding first character of the alignment.
if (inext == 0)
inext = -1;
else if (jnext == 0)
jnext = -1;
else
icurrent = inext;
jcurrent = jnext;
inext = Ii[icurrent][jcurrent];
jnext = Ij[icurrent][jcurrent];
tick++;
}
// Output of the consensus motif to the console
std::cout << std::endl << "***********************************************" << std::endl;
std::cout << "The alignment of the sequences" << std::endl << std::endl;
for (int i = 0; i < nSynth; i++) {
std::cout << seqSynth[i];
};
std::cout << " and" << std::endl;
for (int i = 0; i < nReal; i++) {
std::cout << seqReal[i];
};
std::cout << std::endl << std::endl;
std::cout << "is for the parameters mu = " << mu << " and delta = " << delta << " given by" << std::endl << std::endl;
for (int i = tick - 1; i >= 0; i--)
std::cout << consensusSynth[i];
std::cout << std::endl;
for (int j = tick - 1; j >= 0; j--)
std::cout << consensusReal[j];
std::cout << std::endl;
int numMismatches = 0;
for (int i = tick - 1; i >= 0; i--) {
if (consensusSynth[i] != consensusReal[i]) {
numMismatches++;
}
}
viableAlignment = numMismatches <= maxMismatch;
return imax - jmax;
}
Thanks!
Related
a scheduling problem that wants to minimize a weighted completion time
I would like to pass the image problem to the c++ language and solve it with cplex solver.
Problem:
Min ∑WiCi
xi ∈ 0,1
t= ∆ + ∑_{i} xi*pi1
Ci1 = ∑_{j=1}^{i} xj*pj1
Ci2 = t + ∑_{j=1}^{i} (1−xj)*pj2
Ci ≥ Ci1
Ci ≥ Ci2−Ωxi
Ω = ∆ + ∑_i max(pi1, pi2)
The goal is to minimize the weighted completion time of a problem with one machine with at most one machine reconfiguration. I have a boolean variable Xi that tells me if the job is done before or after the reconfiguration. The variable t is the instant after the reconfiguration(delta is the time to reconfigure the machine). pi1 is the processing time in configuration 1 and pi2 is the processing time in configuration 2. Besides, I have restrictions due to Ci (completion time of a job i).
The example of code I made is not working. I know I have a problem writing the C variable (but I don't know how to solve it).
#include <iostream>
#include <ilcplex/ilocplex.h>
#include <ilcp/cp.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int nbJob = 4;
int delta = 2;
int nbConf = 2;
vector<int> w_job;
w_job.push_back(1); w_job.push_back(1); w_job.push_back(1); w_job.push_back(1);
vector<vector<int>>p_job;
p_job.resize(nbJob);
p_job[0].push_back(6); p_job[0].push_back(3);
p_job[1].push_back(1); p_job[1].push_back(2);
p_job[2].push_back(10); p_job[2].push_back(2);
p_job[3].push_back(1); p_job[3].push_back(9);
int max_p = 0;
int aux;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(p_job[i][0], p_job[i][1]);
max_p = max(max_p, aux);
}
cout << max_p << endl;
int max_w = 0;
aux = 0;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(w_job[i], w_job[i + 1]);
max_w = max(aux, max_w);
}
cout << max_w << endl;
int omega = 0;
for (size_t i = 0; i < nbJob; i++) {
omega += max(p_job[i][0], p_job[i][1]);
}
omega += delta;
cout << omega << endl;
try {
IloEnv env;
IloModel model(env);
IloBoolVarArray x(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloBoolVar xi(env, 0, 1, "xi");
x[i] = xi;
}
IloArray<IloExprArray> C(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloExprArray Ci(env);
for (size_t j = 0; j < nbConf; j++) {
IloExpr Cij(env);
Ci.add(Cij);
}
C[i] = Ci;
}
IloNumVarArray C_final(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloNumVar C_finali(env, 0, IloInfinity, ILOINT); //
C_final[i] = C_finali;
}
IloExpr t(env);
for (int i = 0; i < nbJob; i++) {
t += x[i] * p_job[i][0];
}
t += delta;
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][0] += x[j] * p_job[j][0];
}
}
}
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][1] += ((1 - x[j]) * p_job[j][1]);
}
}
C[i][1] += t;
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][0]);
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][1] - (omega * x[i]));
}
IloExpr wiCi(env);
for (size_t i = 0; i < nbJob; i++) {
wiCi += w_job[i] * C_final[i];
}
model.add(IloMinimize(env, wiCi));
IloCplex solver(model);
solver.solve();
for (int i = 0; i < nbJob; i++) {
cout << "C " << i + 1 << " = " << solver.getValue(C_final[i]) << " x" << i+1 << " = " << solver.getValue(x[i]) << endl;
}
cout << endl;
cout << "t = " << solver.getValue(t) << endl;
cout << "wiCi = " << solver.getObjValue() << endl << endl;
}
catch (IloException e) {
cout << e.getMessage();
}
}
in my program there are two sorting functions shaker and shell sorting functions, also in each sorting there are two counters, a count of the number of permutations and count of the number of comparisons, but for some reason they do not work, I just can’t understand why, I will be grateful for any help =)
void ShakerSort(ZKR* M, int N) {
int l, r, i, k, buf;
int q = 0;
int g = 0;
k = l = 0;
r = N - 2;
while(l <= r) {
for(i = l; i <= r; i++)
if(M[i].ACADEMIC_DEGREE > M[i + 1].ACADEMIC_DEGREE) {
q++;
buf = M[i].ACADEMIC_DEGREE;
M[i].ACADEMIC_DEGREE = M[i + 1].ACADEMIC_DEGREE;
M[i + 1].ACADEMIC_DEGREE = buf;
k = i;
}
g++;
r = k - 1;
for(i = r; i >= l; i--)
if(M[i].ACADEMIC_DEGREE > M[i + 1].ACADEMIC_DEGREE) {
q++;
buf = M[i].ACADEMIC_DEGREE;
M[i].ACADEMIC_DEGREE = M[i + 1].ACADEMIC_DEGREE;
M[i + 1].ACADEMIC_DEGREE = buf;
k = i;
}
g++;
l = k + 1;
}
cout << "Number of comparisons = " << q << endl;
cout << "Number of permutations = " << g << endl;
}
// SHELL SORT
void sortShell(ZKR* M, int N) {
int g = 0;
int q = 0;
int t = clock();
int step = N / 2;
while(step > 0) {
for(int i = 0; i < (N - step); ++i) {
q++;
int j = i;
while((j >= 0) && (M[j].ACADEMIC_DEGREE > M[j + step].ACADEMIC_DEGREE)) {
int tmp = M[j].ACADEMIC_DEGREE;
M[j].ACADEMIC_DEGREE = M[j + step].ACADEMIC_DEGREE;
M[j + step].ACADEMIC_DEGREE = tmp;
j -= step;
g++;
}
}
step /= 2;
}
cout << "Number of comparisons = " << q << endl;
cout << "Number of permutations = " << g << endl;
cout << "Время сортировки = " << clock() - t << "мили cекунд" << endl;
}
I am trying to convert an array to a matrix, so I dynamically allocated memory to the matrix, but I'm getting an error:
CRT detected that the application wrote to memory after end of heap buffer
int main() {
float a[9] = { 1, 3, 5, 6,4,6,5,6,8};
int b = sizeof(a)/sizeof(a[0]);
int r = sqrt(b) - 1;
float **A_mat = new float*[r];
//A_mat = ;
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r];
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
cout << "a[0,0] is " << A_mat[0][0] << endl;
cout << "a[0,2] is " << A_mat[0][2] << endl;
cout << "a[1,0] is " << A_mat[1][0] << endl;
cout << "a[2,0] is " << A_mat[2][0] << endl;
for (int i = 0; i <= r; i++) {
delete[] A_mat[i];
}
delete[] A_mat;
system("pause");
}
Your code has two major issues:
Issue 1:
float **A_mat = new float*[r]; // allocating memory for 2 rows because r = sqrt(9) - 1 => r = 2
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r]; // Usage of A_mat[r] for r = 2 is out of bounds. Also, memory is allocated for 2 columns.
Issue 2:
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)]; // accessing indices (0, 2), (1, 2), (2, 0), (2, 1), (2, 2) are out of bounds
}
}
Instead, you should allocate (r + 1) size memory. Like this:
int main() {
float a[9] = { 1, 3, 5, 6,4,6,5,6,8};
int b = sizeof(a)/sizeof(a[0]);
int r = sqrt(b) - 1;
float **A_mat = new float*[r + 1];
//A_mat = ;
for (int i = 0; i <= r; i++)
A_mat[i] = new float[r + 1];
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
cout << "a[0,0] is " << A_mat[0][0] << endl;
cout << "a[0,2] is " << A_mat[0][2] << endl;
cout << "a[1,0] is " << A_mat[1][0] << endl;
cout << "a[2,0] is " << A_mat[2][0] << endl;
for (int i = 0; i <= r; i++) {
delete[] A_mat[i];
}
delete[] A_mat;
system("pause");
}
Suggestions:
1) Never use raw pointers. Always look for safer alternatives. In this case, you should go for std::vector.
2) Never use naked new. It is generally the major source of Memory leaks. Though currently, its not the case for you. But it might become an issue in the future.
3) Always remember that array starts from 0-indexing in C++.
You have problem with for loop. The index is from 0 to (r-1). But your for loop from 0 to r. It made out of bound of your array.
1> So please replace this for loop
for (int i = 0; i <= r; i++)
by
for (int i = 0; i < r; i++)
2> And replace this statements
for (int j = 0; j <= r; j++) {
for (int i = 0; i <= r; i++) {
A_mat[j][i] = a[i + j * (r+1)];
}
}
by
for (int j = 0; j < r; j++) {
for (int i = 0; i < r; i++) {
A_mat[j][i] = a[i + j * r];
}
}
I am new to c++. I wrote a program which gives a set of states, the problem is when a try to print the state in the main program because I have a pointer of Nodo of [5]; I print the states in the same function and these states are generated the problem is when I call the function in the main program and when I try to print some state this print a number like 912222558. Please help me.
#include <iostream>
using namespace std;
class State {
public:
int dsc[3];
State()
{
dsc[0] = 3;
dsc[1] = 3;
dsc[2] = 1;
}
void printstate()
{
for (int i = 0; i < 3; i++) {
cout << " " << dsc[i] << " ";
}
cout << endl;
}
bool checkObjetivo()
{
if (dsc[0] == 0 && dsc[1] == 0 && dsc[2] == 0) {
return true;
}
else {
return false;
}
}
bool validState()
{
if ((dsc[0] >= 0 && dsc[0] <= 3) && (dsc[1] >= 0 && dsc[1] <= 3) && (dsc[2] >= 0 && dsc[2] <= 1) && (dsc[1] <= dsc[0])) {
return true;
}
else {
return false;
}
}
};
class Nodo {
public:
State state;
Nodo* father;
Nodo* child[5];
int level;
bool solution;
Nodo()
{
father = NULL;
for (int i = 0; i < 5; i++) {
child[i] = NULL;
}
level = 0;
solution = false;
}
Nodo childGeneration()
{
Nodo nuevoNodo;
if (state.validState()) {
if (state.checkObjetivo()) {
for (int i = 0; i < 3; i++) {
nuevoNodo.state.dsc[i] = state.dsc[i];
}
nuevoNodo.solution = true;
return nuevoNodo;
}
else {
int h0[3], h1[3], h2[3], h3[3], h4[3];
Nodo nH0, nH1, nH2, nH3, nH4;
for (int i = 0; i < 3; i++) {
h0[i] = state.dsc[i];
h1[i] = state.dsc[i];
h2[i] = state.dsc[i];
h3[i] = state.dsc[i];
h4[i] = state.dsc[i];
}
if (state.dsc[2] == 1) {
cout << "Paso dsc es 1 " << endl;
//MC
h0[0] = h0[0] - 1;
h0[1] = h0[1] - 1;
h0[2] = h0[2] - 1;
//CC
h1[1] = h1[1] - 2;
h1[2] = h1[2] - 1;
//MM
h2[0] = h2[0] - 2;
h2[2] = h2[2] - 1;
//C_
h3[1] = h3[1] - 1;
h3[2] = h3[2] - 1;
//M_
h4[0] = h4[0] - 1;
h4[2] = h4[2] - 1;
}
if (state.dsc[2] == 0) {
//MC
h0[0] = h0[0] + 1;
h0[1] = h0[1] + 1;
h0[2] = h0[2] + 1;
//CC
h1[1] = h1[1] + 2;
h1[2] = h1[2] + 1;
//MM
h2[0] = h2[0] + 2;
h2[2] = h2[2] + 1;
//C_
h3[1] = h3[1] + 1;
h3[2] = h3[2] + 1;
//M_
h4[0] = h4[0] + 1;
h4[2] = h4[2] + 1;
}
for (int i = 0; i < 3; i++) {
nH0.state.dsc[i] = h0[i];
nH1.state.dsc[i] = h1[i];
nH2.state.dsc[i] = h2[i];
nH3.state.dsc[i] = h3[i];
nH4.state.dsc[i] = h4[i];
}
cout << "nH0 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH0.state.dsc[j];
}
cout << endl;
cout << "nH1 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH1.state.dsc[j];
}
cout << endl;
cout << "nH2 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH2.state.dsc[j];
}
cout << endl;
cout << "nH3 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH3.state.dsc[j];
}
cout << endl;
cout << "nH4 state: " << endl;
for (int j = 0; j < 3; j++) {
cout << nH4.state.dsc[j];
}
cout << endl;
nuevoNodo.child[0] = &nH0;
nuevoNodo.child[1] = &nH1;
nuevoNodo.child[2] = &nH2;
nuevoNodo.child[3] = &nH3;
nuevoNodo.child[4] = &nH4;
cout << "New nodo child[0] " << endl;
for (int j = 0; j < 3; j++) {
cout << nuevoNodo.child[0]->state.dsc[j];
}
cout << endl;
cout << "New nodo child[1] " << endl;
for (int j = 0; j < 3; j++) {
cout << nuevoNodo.child[1]->state.dsc[j];
}
cout << endl;
return nuevoNodo;
}
}
}
};
int main()
{
State est;
est.printstate();
Nodo nNodo;
nNodo.state = est;
Nodo nRes;
Nodo verN;
verN = nRes.childGeneration();
cout << "child[0] " << endl;
verN.child[0]->state.printstate();
return 0;
}
You have undefined behavior.
In the childGeneration function, you are storing pointers to stack variables. Those pointers are only valid from within that function.
You should consider using dynamic memory allocation instead, or rethink your program in some other way.
I have been given a "Sand box" of variable length and width. I've been given instructions to find a "shovel" of static size, which may be oriented either horizontally or vertically. I implement the following algorithm in order to search the least amount of times to find one valid location (one which corresponds to a "part of the object") in the grid:
found = false;
nShift = 0;
shovelSize = 4;
for(int i = 0; i < SandBoxRows; i++) {
for(int j = 0; j < SandBoxColumns; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
}
if(nShift >= shovelSize - 1 || nShift > SandBoxColumns) {
nShift = 0;
} else {
nShift++;
}
}
In this case, the "Sand box" will be tested by the function as described below.
I completely recreate this scenario with a "Sand box" whose size is fixed (though easily manipulated) whose shovel is still randomly placed and oriented within the following code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
const int ROW = 12;
const int COL = 16;
char sandbox[ROW][COL];
bool probeSandBoxTwo(char c, int i);
void displayResults(int sCount, bool found, int x, int y);
void displaySandbox();
void displaySearchPattern();
void fillSandbox();
void placeShovel();
int main() {
fillSandbox();
placeShovel();
displaySandbox();
//define your variables here
bool found;
int nShift,
sCount,
shovelSize,
x,
y;
found = false;
nShift = 0;
shovelSize = 4;
sCount = 0;
for(int i = 0; i < ROW && !found; i++) {
for(int j = 0; j < COL && !found; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
x = i;
y = j + nShift;
sCount++;
cout << "Search conducted at (" << i << ", " << (j + nShift) << ")" << endl;
}
if(nShift >= shovelSize - 1 || nShift > ROW) {
nShift = 0;
} else {
nShift++;
}
}
displayResults(sCount, found, x, y);
displaySearchPattern();
}
bool probeSandBoxTwo(char c, int i) {
if(sandbox[c-'A'][i-1] == 'X') {
return true;
} else {
return false;
}
}
void displayResults(int sCount, bool found, int x, int y) {
cout << endl;
cout << "Total searches: " << sCount << endl;
cout << endl;
if(found) {
cout << "Shovel found at coordinates: (" << x << ", " << y << ")" << endl;
}
}
void displaySandbox() {
cout << " ";
for(int i = 0; i < COL; i++) {
cout << (i % 10); //show index numbers [col]
}
cout << endl;
for(int i = 0; i < ROW; i++) {
cout << (i % 10) << " "; //show index numbers [row]
for(int j = 0; j < COL; j++) {
cout << sandbox[i][j];
}
cout << endl;
}
cout << endl;
}
void displaySearchPattern() {
int nShift = 0;
int shovelSize = 4;
cout << endl << " ";
for(int i = 0; i < COL; i++) {
cout << (i % 10); //show index numbers [col]
}
cout << endl;
for(int i = 0; i < ROW; i++) {
cout << (i % 10) << " "; //show index numbers [row]
for(int j = 0; j < COL; j++) {
if(!((j + nShift) % shovelSize)) {
cout << 'o';
} else {
cout << '.';
}
}
if(nShift >= shovelSize - 1 || nShift > COL) {
nShift = 0;
} else {
nShift++;
}
cout << endl;
}
}
void fillSandbox() {
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
sandbox[i][j] = '.';
}
}
}
void placeShovel() {
srand(time(NULL));
int shovelRow,
shovelCol,
shovelSize = 4;
if(rand() % 2) {
//horizontal
shovelRow = rand() % ROW + 1;
shovelCol = rand() % (COL - (shovelSize - 1)) + 1;
for(int i = shovelCol - 1; i < shovelSize + (shovelCol - 1); i++) {
sandbox[shovelRow - 1][i] = 'X';
}
} else {
//vertical
shovelRow = rand() % (ROW - (shovelSize - 1)) + 1;
shovelCol = rand() % COL + 1;
for(int i = shovelRow - 1; i < shovelSize + (shovelRow - 1); i++) {
sandbox[i][shovelCol - 1] = 'X';
}
}
}
In this code, I also graphically display the pattern (when run) with which my algorithm searches.
Is this truly the optimal search pattern for such a scenario, is my implementation correct, and if so, why might I be having incorrect results returned?
A given test driver reports the following results:
The source code for this result (and its test driver).
found = false;
nShift = 0;
shovelSize = 4;
for(int i = 0; i < SandBoxRows; i++) {
for(int j = 0; (j + nShift) < SandBoxColumns; j+=shovelSize) {
found = probeSandBoxTwo(('A' + i), (j + 1 + nShift));
}
if(nShift >= shovelSize - 1 || nShift > SandBoxColumns) {
nShift = 0;
} else {
nShift++;
}
}
This corrects an error in the conditional portion of the for loop header which did not account for the index-shifting variable nShift.