Problem in comparing two arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • just a feeling
    New Member
    • Aug 2007
    • 86

    Problem in comparing two arrays

    Hi,

    I have 2 arrays of strings. I wanna display only the words that exists in the first array but doesn't in the second one.
    For example :-
    Str1 = Apple,Banana.
    Str2=Apple, Orange.
    Banna should be displayed

    this is what I have done so far.
    Code:
    string str1[10];
    string str2[10];
    int i=0,j=0,cmp;
    
    for (i=0;i<10;++i)
    {
    	 for(j=0;j<10;++j)
    	    cmp=str1[i].compare(str2[j]);
     if(cmp!=0)
    cout<<str[i]<<" doesnt exist in the second array";
    }
    I dunno why it's not working correctly ? I know that is simple, but it seems that I have to refresh my memory ( It has been a while since I used C++ !!! ).

    Any help would be greatly appreciated.
    Thank you.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by just a feeling
    Code:
    string str1[10];
    string str2[10];
    int i=0,j=0,cmp;
    
    for (i=0;i<10;++i)
    {
        for(j=0;j<10;++j)
            cmp=str1[i].compare(str2[j]);
                if(cmp!=0)
                    cout<<str[i]<<" doesnt exist in the second array";
    }
    I dunno why it's not working correctly?
    In a simple case like this you should step through the logic of your code: The logic of you code is something like this

    Code:
    foreach entry in array1
        foreach entry in array2
            if the entries are not the same print array1 entry "does not exist in the second array"
    For the data

    array1 = "Apple","Banana "
    array2 = "Apple","Orange "

    This does the following comparisons and outputs

    "Apple" <=> "Apple" -> No Output
    "Apple" <=> "Orange" -> "Apple does not exist in the second array"
    "Banana" <=> "Apple" -> "Banana does not exist in the second array"
    "Banana" <=> "Orange" -> "Banana does not exist in the second array"

    Clearly the logic is wrong since

    a. Apple is in the 2nd list but the code says it isn't
    b. Although banana is not in the second list you get told this twice


    OK you real mistake is that you have jumped into coding before working out an algorithm that actually solves the problem of detecting if entries in list 1 exist in list 2. You need to go back and create a working algorithm before you try to code it.

    HINT: You will only need to output anything once in each iteration of the outside loop, not in each iteration of the inside loop, the inside loop should solely be used for logic of checking if the first list entry exists in the second list.

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      Originally posted by just a feeling
      Hi,

      I have 2 arrays of strings. I wanna display only the words that exists in the first array but doesn't in the second one.
      For example :-
      Str1 = Apple,Banana.
      Str2=Apple, Orange.
      Banna should be displayed

      this is what I have done so far.
      Code:
      string str1[10];
      string str2[10];
      int i=0,j=0,cmp;
      
      for (i=0;i<10;++i)
      {
      	 for(j=0;j<10;++j)
      	    cmp=str1[i].compare(str2[j]);
       if(cmp!=0)
      cout<<str[i]<<" doesnt exist in the second array";
      }
      I dunno why it's not working correctly ? I know that is simple, but it seems that I have to refresh my memory ( It has been a while since I used C++ !!! ).

      Any help would be greatly appreciated.
      Thank you.
      you need to work around with your logic.
      Debug the code and you will find it.

      I haven't gone through your code really.
      Instead I suggest to use inbuilt SET operations in C++
      All you need to take the difference of two vectors.
      check this link:
      http://www.cplusplus.c om/reference/algorithm/set_difference. html

      Comment

      • just a feeling
        New Member
        • Aug 2007
        • 86

        #4
        Thank u sooooo much Banfa and ashitpro for ur help.
        Originally posted by Banfa
        OK you real mistake is that you have jumped into coding before working out an algorithm that actually solves the problem of detecting if entries in list 1 exist in list 2. You need to go back and create a working algorithm before you try to code it.
        U are right. U know, It seems that my mind stopped thinkin ( around 7 hours at university and 7 hours on computer ) !!!
        Anyway, I just wrote this code before moments and it should work ( but it doesnt ! )

        Code:
        bool flag=false;
        for (i=0;i<10;++i)
         {
        	 for(j=0;j<10;++j)
        	 {
        		 cmp=str1[i].compare(str2[j]);
        	     if ( cmp==0 )
        		 {
        		   flag = true;
        		   break;
        		 }
        	 }
        
        	if (flag=false)
        	cout<<text[i];
        
           flag=false;
         }
        Let's trace it.
        Assuming str1: Apple,Banana
        str2: Apple,Orange

        i=0,,j=0;
        Apple = Apple
        cmp=0
        flag=true and break from the inner loop.
        wil not display any thing.
        setting flag to false.

        i=1, j=0
        Banna !=Apple
        cmp!=0

        i=1, j=1
        Banana != Orange
        cmp!=0
        finish the list of j ( assuming that the second array contains only two elements, not 10 )
        if (flag=false) yes it is.
        cout<<text[i]; ( but its not displayed !!!! )

        ok, my eyes cant take any more of the computer screen glare. going sleep !
        I'll try to sleep 4 or 5 hours and coming back.

        Thanks again.

        Comment

        • just a feeling
          New Member
          • Aug 2007
          • 86

          #5
          Anybody knows what's wrong in my code ?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Code:
            	if (flag=false)
            	cout<<text[i];
            This is an assignment not a comparison.

            Comment

            • DaemonCoder
              New Member
              • Mar 2008
              • 43

              #7
              Lookup assingment operators.....
              Code:
              
              if(varOne == varTwo) // Compares varOne and varTwo for equality
              if(varOne = varTwo)  // Says var1 = var2
              This was probably a simple typo. I have done it myself. Hope this helps.

              Comment

              • just a feeling
                New Member
                • Aug 2007
                • 86

                #8
                Oops !! didn't notice that. It's WORKING now.
                Thank u DaemonCoder.
                Banfa, thank you so much for the great review! I deeply appreciate your help.

                Comment

                • DaemonCoder
                  New Member
                  • Mar 2008
                  • 43

                  #9
                  Your welcome. Actually Banfa gave the answer I just made it easier to understand.

                  Comment

                  Working...