Intersection of Two Arrays in C – Dead Simple, Innit

intersection of two arrays in c – dead simple, innit

intro

Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna sling together a lil program to nab the intersection of two arrays—ya know, the numbers that show up in both, like finding mates who’ve been to the same pubs as ya. It’s dead easy but proper handy—like when I was tryna match my pint list with my mate’s (too smashed to count proper). Great for getting yer head round arrays and loops in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together. It’s a basic stab, assumes no duplicates in each array, and hard-codes the inputs. Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:

#include

void findIntersection(int arr1[], int n1, int arr2[], int n2) {
printf("intersection: ");
int found=0; // track if we got any
for(int i=0;i for(int j=0;j if(arr1[i]==arr2[j]) { // match!
printf("%d ",arr1[i]);
found=1;
break; // no dupes in arr2, move on
}
}
}
if(!found) {
printf("nowt in common, mate!");
}
printf("\n");
}

int main() { // nearly did void, ha!
int arr1[]={1,3,5,7}; // first lot
int arr2[]={2,3,5,6}; // second lot
int n1=4,n2=4; // sizes, innit

findIntersection(arr1,n1,arr2,n2); // chuck it out

return 0; // forgot ; once, ugh!
}

what ya get

Here’s what it chucks out when ya run it. I hard-coded two lil arrays—worked bang on, mate!

intersection: 3 5

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!

1. that `#include ` gubbins
This is me nicking my toolkit. ``—fancy pants “standard input-output”—lets me use `printf`. Forgot it once, and my code just sat there like a plonker. Total mug move!

2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!

3. boxes: `int arr1[]` and pals
These are my lil buckets—`arr1[]={1,3,5,7}` and `arr2[]={2,3,5,6}` are the two arrays, hard-coded with some numbers. `n1=4` and `n2=4` are their sizes—kept it small for a quick go!

4. `findIntersection` funtion
Here’s the guts! `findIntersection(int arr1[], int n1, int arr2[], int n2)` does the job. `found=0` tracks if we nab any matches. Nested `for` loops—`for(i=0;i

5. chucking it out: `printf` and `findIntersection`
`findIntersection(arr1,n1,arr2,n2)` runs it and chucks the matches—or lack of ‘em—on screen. `%d ` prints numbers with a space, `\n` tidies it. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

6. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to find the intersection of two arrays. Faff with it—chuck in daft arrays, tweak it to say “pub mates” if ya fancy. First time I ran this, I got 3 and 5—bang on, eh! You’re a star already, mate—keep smashing it! 😊 (Note: No dupes in each array assumed—fancier version’d need sorting or a hash, but this is pub-level simple!)