complicated comparison

This is a discussion on complicated comparison within the C++ Programming forums, part of the General Programming Boards category; hello all, I'm trying to compare a user selected number of characters to an array which holds 500000 characters. Basically ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    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

    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    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:

    Streaming in a file with spaces/blanks

    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;}
    			}
    		}
    	}
    }

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. String Comparison
    By Cdrwolfe in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2006, 09:59 AM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21