Sunday, July 12, 2009

C++ - Integer Arrays - Check if Array B is in Array A?

Given two arrays, A and B, of unknown length, determine if the array B is contained inside array A. So, B contains a list of integers. We need to know if that same list of integers exists anywhere in A. How can this program be modified if both arrays have strings instead of integers?

C++ - Integer Arrays - Check if Array B is in Array A?
void CheckArrayBinArrayA(int a[], int sizeofa, int b[], int sizeofb)


{


int count = 0;


for(int i=0;i%26lt;sizeofa;i++)


{


for(int j=0;j%26lt;sizeofb;j++)


{


if(b[j] == a[i])


{


count++;


break;


}


}


}


if(count==sizeofb)


{


printf("All elements in Array B are in Array A\n\n");


}


else


{


printf("All elements in Array B are NOT in Array A\n\n");


}


}





int main()


{


int a[] = {1, 5, 3,4};


int b[] = {1, 5, 3};


int sizeofa = sizeof(a)/sizeof(int);


int sizeofb = sizeof(b)/sizeof(int);





CheckArrayBinArrayA(a, sizeofa, b, sizeofb);


}
Reply:To get the best answers to a homework question, you should show any work you have done. If you haven't done any work, there is no reason for anyone else to do it for you.





*edit*


Thanks for making a try at it.





You have the right idea, with the j loop nested in the i loop, just not quite right.





The i loop is the starting point, where you will start comparing B inside A. However, the limit of i should not be %26lt; sizeofA, but instead %26lt;= sizeofa - sizeofb.





Set match = true at the beginning of the j loop. As you go through the j loop comparing, you can set it false if the compare doesn't match.





When you compare, compare b[j] != a[i+j], otherwise you are comparing every b element with the same a[i] element.





// This inside the i loop. The .'s are for spacing in YA


match = false;


for(int j=0;j%26lt;sizeofb;j++)


{


....if(b[j] != a[i+j])


....{


........match = false;


........break;


....}


}


if (match)


....break;





The break tells the compiler to skip the rest of the j loop once we have a failed match. The second break skips the rest of the i loop if match is true.
Reply:The easiest way I can think of right now would be to #include %26lt;string.h%26gt; then change the data type of the arrays to strings(obviously) then you should be able to use the "==" operator with the values in the array.


Other than that I think theres a few pieces of your logic you might have to alter but as for syntax then you should be fine.
Reply:There's probably some library somewhere that does this, but it sounds like this is for school and you're probably supposed to create your own algorithm. I'd start out by creating a loop to loop over each element of array B and for each, I'd then loop over each element in array A testing if they're equal. If I found a match, I'd set some boolean flag to denote it and break out of the inner loop. The program would then continue on with the outer loop. Inside the outer loop, I'd probably also check if that match flag had been set cuz if it hadn't been, that would tell me the previous B element wasn't contained in A and that I could stop the whole thing and return that B is not in A. This also means, I guess, you'd initialize that boolean flag to be true before any looping took place. Here's some pseudo-code:





match=true


foreach a in arrayA {


if match != true { return false // B not in A }


match = false


foreach b in arrayB {


if b==a {


match=true


break


}


}


if match == true { return true // B IS in A }


}








For strings, it's pretty-much still the same thing except that in C/C++, if memory serves (I'm a Java dev), instead of saying something like "if a==b" you'd use the strcmp() method and test for 0 or something like that I believe. I don't know how much of a programmer you are, so forgive me if this is obvious to you, but if you're not familiar with pseudo-code, just know that it's not necessarily real code in any specific language. The code above definitely won't work unchanged in C/C++/Java/Perl or anything else. It's just the basic algorithm.

carnation

No comments:

Post a Comment