-
complicated comparison
hello all, I'm trying to compare a user selected number of characters to an array which holds 500000 characters. Basically the user selects a window size, anywhere from 1 to whatever, now I have to look into the array that holds the 500000 characters, and pick a random starting position so lets say that:
window=4 and my random starting number is 48, so I take the characters in my textArray from textArray[48] to textArray[51], and put these 4 characters into a seperate array. The next thing I have to do is to again, step through the array of 500000 characters looking for the exact same combinations of 4 characters just taken out, and getting plus counting the character that appears right after it.
this is what I have so far:
Code:
void generateOutput(char text[], int window, int length, int sum)
{
//declaring arrays
//keep in mind that window size was inputed correctly
char *windowArr = new char[window]; //window array
//declaring variables
int randomPosition = rand(); //there is a function that I do not include that replaces this
//and limits the random position to be [0, 500000]
int i, j;
int count = 0;
for( i=0; i<window; i++){
windowArr[i] = text[randomPosition + i];
}
//compare the window
/*THE PROBLEM IS IN THE NEXT PART, ANYTHING I DO IS LIKE HARD CODING*/
int count = 1;
for( i=0; i<500000; i++){
if( windowArr[0] == text[i]){ //text is array with 500000 characters
for( j=1; j<window; j++){
if( windowArr[j] == text[i+1] ){
count++;
if( (count != window) && (windowArr[j+1] = text[i+2]) ){
cout++;
}else{break;}
}else{break;}
}
}else{break;}
}
//AND so on, this is nothing else but hardcoding...I've tried many
different ways and comout with similar result....
any help?
thanks,
axon
}
-
I got something better, but it still has flaws, and I'm not quite sure where, here goes
Code:
void generateOutput(char text[], int window, int length, int sum)
{
//declaring arrays
char *windowArr = new char[window]; //window array
char allChars[127];
//declaring variables
int randomPosition = rand(); //get random number
int i, j;
int count = 1;
for( i=0; i<window; i++){
windowArr[i] = text[randomPosition + i];
}
for( i=0; i<127; i++){ //set every element to 0
allChars[i] = 0;
}
for( i=0; i<500000; i++)
{
if( windowArr[0] == text[i] ){
for( j=1; j<window; j++ ){
if( windowArr[j] == text[i+j] ){
count++;
}else{ break;}
if( count == window ){
allChars[ text[j+i+1]]++; //to get next char and add 1 to it
break;
}
}
}else{continue;}
}
cout << endl << endl;
for(i=0; i<127; i++) //CHECK WHICH DOESN'T WORK
{
if(allChars[i] > 0){
cout << endl << (char)i << " : " << (int)allChars[i] << endl;
}
}
}
the problem is that it doesn't count ALL the instances that the characters in the window appear,
axon
-
I got it to work now...I had to place another continue statement in the inner for loop, and it works!!! the only problem is with another earlier part of the program...I made a post about it here:
http://cboard.cprogramming.com/showt...threadid=37884
if you guys still have some suggestions to improve this, I'll be glad to read them...
axon
and here is the final version for completeness:
Code:
void generateOutput(char text[], int window, int length, int sum)
{
//declaring arrays
char *windowArr = new char[window]; //window array
char allChars[127];
//declaring variables
int randomPosition = rand(); //get random number
int i, j;
int count;
for( i=0; i<window; i++){
windowArr[i] = text[randomPosition + i];
cout << windowArr[i];
}
cout<< endl << "=================" << endl;
for( i=0; i<127; i++){ //set every element to 0
allChars[i] = 0;
}
for( i=0; i<500000; i++){ //COMPARISON
count = 1;
if( windowArr[0] == text[i] ){
for( j=1; j<window; j++ ){
if( windowArr[j] == text[i+j] ){
count++;
}else{ break;}
if( count == window ){
allChars[ text[j+i+1]]++; //to get next char and add 1 to it
break;
}else{continue;}
}
}
}
}