473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem in comparing two arrays

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.
Expand|Select|Wrap|Line Numbers
  1. string str1[10];
  2. string str2[10];
  3. int i=0,j=0,cmp;
  4.  
  5. for (i=0;i<10;++i)
  6. {
  7.      for(j=0;j<10;++j)
  8.         cmp=str1[i].compare(str2[j]);
  9.  if(cmp!=0)
  10. cout<<str[i]<<" doesnt exist in the second array";
  11. }
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.
Mar 8 '08 #1
8 1804
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. string str1[10];
  2. string str2[10];
  3. int i=0,j=0,cmp;
  4.  
  5. for (i=0;i<10;++i)
  6. {
  7.     for(j=0;j<10;++j)
  8.         cmp=str1[i].compare(str2[j]);
  9.             if(cmp!=0)
  10.                 cout<<str[i]<<" doesnt exist in the second array";
  11. }
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

Expand|Select|Wrap|Line Numbers
  1. foreach entry in array1
  2.     foreach entry in array2
  3.         if the entries are not the same print array1 entry "does not exist in the second array"
  4.  
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.
Mar 8 '08 #2
ashitpro
542 Expert 512MB
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.
Expand|Select|Wrap|Line Numbers
  1. string str1[10];
  2. string str2[10];
  3. int i=0,j=0,cmp;
  4.  
  5. for (i=0;i<10;++i)
  6. {
  7.      for(j=0;j<10;++j)
  8.         cmp=str1[i].compare(str2[j]);
  9.  if(cmp!=0)
  10. cout<<str[i]<<" doesnt exist in the second array";
  11. }
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.com/reference/algorithm/set_difference.html
Mar 8 '08 #3
Thank u sooooo much Banfa and ashitpro for ur help.
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 ! )

Expand|Select|Wrap|Line Numbers
  1. bool flag=false;
  2. for (i=0;i<10;++i)
  3.  {
  4.      for(j=0;j<10;++j)
  5.      {
  6.          cmp=str1[i].compare(str2[j]);
  7.          if ( cmp==0 )
  8.          {
  9.            flag = true;
  10.            break;
  11.          }
  12.      }
  13.  
  14.     if (flag=false)
  15.     cout<<text[i];
  16.  
  17.    flag=false;
  18.  }
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.
Mar 8 '08 #4
Anybody knows what's wrong in my code ?
Mar 9 '08 #5
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.     if (flag=false)
  2.     cout<<text[i];
  3.  
This is an assignment not a comparison.
Mar 9 '08 #6
Lookup assingment operators.....
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. if(varOne == varTwo) // Compares varOne and varTwo for equality
  4. if(varOne = varTwo)  // Says var1 = var2
  5.  
  6.  
This was probably a simple typo. I have done it myself. Hope this helps.
Mar 9 '08 #7
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.
Mar 9 '08 #8
Your welcome. Actually Banfa gave the answer I just made it easier to understand.
Mar 9 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
3
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is...
18
by: Mike Bartels | last post by:
Hi Everyone! I have two Arrays A and B. Both arrays are byte arrays with 7 bytes each. The contents of array A and B are the same A = {1, 2, 3, 4, 5, 6, 7}; B = {1, 2, 3, 4, 5, 6, 7}; When...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
4
by: eoghan.kenny | last post by:
Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
11
by: Sheldon | last post by:
Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
1
by: chiefychf | last post by:
I'm working on a school project and I am having a few issues... The program calls for three arrays a,b,c that have to be sorted, then compared to even or odd and stored in arrays d & e, then merge...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.