nuber of occurence of each caractere using tree loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    nuber of occurence of each caractere using tree loops

    what is wrong with this code??

    Code:
    for(i = 0; i < taille; i++)
    	{
    		count = 0;
    		for(j = 0; j < i; j++)
    		{
    			if(*(T+j) == *(T+i))
    			{
    				i++;
    				break;
    			}
    			else if(j == i)
    			{
    				for(k = i; k < taille; k++)
    					if(*(T+k) == *(T+i))
    						count++;
    			}
    		}
    		printf("%c : %d\n", *(T+i), count);
    	}
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Hard to say since you don't say what it is meant to do, however you have a problem in your if/else if statement. The else if block will never be executed because

    if
    j == i is true
    then
    *(T+j) == *(T+i) is also true by definition

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      The invariant of the for-loop is that j is always less than i within the body of the loop. This means that 'else if (j == i)' can never be true.

      I notice that you conditionally increment i within the loop. That may be exactly what you want to do, but it makes me nervous to fool around with the loop variables.

      It would be easier for us to help you if we (a) knew what you expect this program to do and (b) knew precisely what it did to disappoint your expectations.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by donbock
        I notice that you conditionally increment i within the loop. That may be exactly what you want to do, but it makes me nervous to fool around with the loop variables.
        One of the problems of this issue Don has pointed out is that in the for loop statement where you increment i you then immediately test it to ensure it is with-in range, however in the body of the for-loop where you increment i there is no such check to ensure i is still in the correct range and therefore the next time round the inner loop, and there will be a next time round because if j < i then j + 1 < i + 1 you access *(T+i) without knowing that i is in the correct range for the object pointed to by T and if it is not in the correct range you cause undefined behaviour.

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          The purpose of this code is to count the occurrence of each character in a given string using three loops but am having irrelevant data

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            OK so the basic structure should be (modifying your current structure and using no additional data)

            Code:
            LOOP A DOWN ENTIRE STRING
                LOOP B DOWN STRING ALREADY PARSED IN FIRST LOOP
                    IF CHARACTER LOOP B == CHARACTER LOOP A
                         END LOOP EARLY
                    ENDIF
                ENDLOOP B
            
                IF LOOP DIDN'T END EARLY
                    COUNT = 0
                    LOOP C DOWN ENTIRE STRING
                        IF CHARACTER LOOP C == CHARACTER LOOP A
                            COUNT++
                        ENDIF
                    ENDLOOP C
            
                    PRINT CHARACTER LOOP A AND COUNT
                ENDIF
            ENDLOOP A
            Which differs somewhat from you code in you have the final if and for loop inside the second for loop.

            Comment

            Working...