RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 315 | Replies: 2 | Thread Tools  Display Modes
Reply
Join Date: Jun 2008
Posts: 16
Reputation: zourlas is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
zourlas zourlas is offline Offline
Newbie Poster

compare 2D arrays

  #1  
Jul 25th, 2008
Hello mates


I have a new question...

I have 2 2-d arrays with the same dimensions and contents.
The first one, say a [15][4] and the second on, say b[15][4] and are both char type.

They both have the same elements(same 4character Lines) but in different order(the second one's Lines are mixed compared with the first one)....

My question is,

if i declare an array, say int c [15] wht should i do to use it as an index for the array b compared to array a.
for example

i array A i array B i index array C
0 abcd 0 jklm 0 2
1 efgh 1 abcd 1 0
2 ijkl 2 efgh 2 1
. . . .
. . . .
. . . .

i don't want to change the variable type of the arrays becouse this routine is one part of big function i am currently programing.I am tring to think of a way of comparing these 2d arrays line by line and fill in the index array.

Thanks in advance...
Last edited by zourlas : Jul 25th, 2008 at 7:34 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,861
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 1012
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: compare 2D arrays

  #2  
Jul 25th, 2008
1) are those null-terminated strings? If yes, then the first row in A would be abc, not abcd.

2) I guess C will contain the row number in B that matches the row in A?

probably something like this
const int maxrows = 15;
for(int i = 0; i < maxrows; i++)
{
    for(int j = 0; j < maxrows; j++)
    {
        if( strcmp(A[i], B[i]) == 0)
        {
            C[i] = j;
            break;
        }
    }
}
<<Hire Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jun 2008
Posts: 16
Reputation: zourlas is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
zourlas zourlas is offline Offline
Newbie Poster

Re: compare 2D arrays

  #3  
Jul 25th, 2008
Originally Posted by Ancient Dragon View Post
1) are those null-terminated strings? If yes, then the first row in A would be abc, not abcd.

2) I guess C will contain the row number in B that matches the row in A?

probably something like this
const int maxrows = 15;
for(int i = 0; i < maxrows; i++)
{
    for(int j = 0; j < maxrows; j++)
    {
        if( strcmp(A[i], B[i]) == 0)
        {
            C[i] = j;
            break;
        }
    }
}


that worked fine but i also tested this which stores the wanted line to a a string s.
Thanks mate +1 from me

for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
s[j]=b[i][j];

}
for (l = 0; l < 4; l++) {
for (m = 0; m < 4; m++) {
if(a[l][m]==s[m]){c[i]=l;}
else
break;
}

}}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:29 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC