• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

c++ sequences help

Pegun

Golden Member
Hey guys, New problem, a different day. I'm trying to make a problem that detects a sequence of 3 or more in the array defined. It keeps not detecting the lower rows for some reason (2nd to last row doesnt detect the triple d's) Any help is greatly appreciated.

// updateBoard.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char BoardRow[8][8] =
{'c','c','c','d','a','a','a','c',
'a','c','c','c','d','d','d','b',
'a','a','c','c','c','b','b','b',
'a','c','a','c','c','d','c','a',
'b','c','c','d','d','c','a','b',
'a','c','c','d','c','d','d','d',
'd','c','c','d','d','a','a','a',
'a','c','c','c','c','d','a','b'
};

int start, end;

for(int r=0; r<8; r++)
{
for (int c=0; c<8; c++)
cout << BoardRow[r][c];
cout << endl;
}

for(int r=0;r<8;r++)
{


start = 0; end = 0; // set start and end to zero

for(int c=1; c<8; c++)
{
if(BoardRow[r][c] == BoardRow[r][c-1])
{end++;
}
else
{
end++;
//change chars from start to end to up if
//end - start >=3
if ((end-start) >= 3)
for(int cc = start; cc < end; cc++)
BoardRow[r][cc] += 'A' - 'a';

start = end;
}
cout << " (Start, End) = ( " << start << "," << end << " )" << endl;
}
for(int r=0; r<8; r++)
{
for(int c=0; c<8; c++)
cout << BoardRow[r][c];
cout << endl;
}
//{char ch; cin>>ch;} //This phrase, makes
//the program not do the columns
//until a character is input
}
return 0;
}
 
Maybe I'm missing something, but I don't see how it could detect any triples. Aside from basic logic flaws (you increment end in both branches of your character equality test) the problem is that in any set of triples there are only two possible cases where character[ i ] is equal to character[i-1]. So end - start can never be 3 or greater unless there is a sequence of 4 or greater.

I would just tick a counter everytime character[ i ] == character[i-1] and reset it to 0 every time character[ i ] != character[i-1]. If it ever reaches 2 you have a sequence of three.

 
I tried using sequence_length as a type of ticker and use if(sequence_length >=3)....... but this doesn't seem to logically work. It capitalizes letters randomly, it seems.
 
It capitalizes letters randomly, it seems.

Haha, well, two things: first, if "it" is capitalizing letters randomly then your program is changing the memory values in those locations; second, you should be able to find out where that is happening.
 
I did figure it out. Basically the program got to the end of the row, but because it couldn't go anywhere else, it had to neglect the previous two characters, whether the final sequence length turned out to be 3 or not. To fix this i made it so that it updates the end variable and then capitalizes the letters at the end of the loop.
 
Back
Top