Search an Array with Linear Search in C – Dead Easy, Innit

search an array with linear search in c – dead easy, innit

intro

Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna sling together a lil program to sniff out a number in an array using linear search—ya know, just checking each spot one by one til we nab it. It’s dead simple but proper handy—like when I was tryna find my mate’s lost fiver in a pile of receipts (didn’t, gutted). Great for getting your head round arrays 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. Shoved it in a box so it don’t look like a total shambles on your blog. take a peek:

#include

int main() { // nearly did void, ha!
int arr[10],n,i,key,found=0; // 10’s plenty, innit

printf("how many numbers ya want: ");
scanf("%d",&n);

if(n<=0||n>10) { // keep it sensible, ya muppet
printf("oi, gimme a number between 1 and 10!\n");
}
else {
printf("gimme %d numbers: ",n);
for(i=0;i scanf("%d",&arr[i]);
}

printf("gimme the number to find: ");
scanf("%d",&key);

for(i=0;i if(arr[i]==key) { // gotcha!
printf("%d’s at spot %d\n",key,i);
found=1; // flag it up
break; // leg it outta here
}
}
if(found==0) { // no luck, eh
printf("%d ain’t in there, mate!\n",key); // forgot ; once, ugh!
}
}

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I lobbed in some numbers—worked bang on, mate!

how many numbers ya want: 5
gimme 5 numbers: 23 45 12 67 8
gimme the number to find: 12
12’s at spot 2

how many numbers ya want: 3
gimme 3 numbers: 1 2 3
gimme the number to find: 5
5 ain’t in there, mate!

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the chippy with a greasy bag—or a pint, 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 piece of piss once ya clock it, swear down!

1. that `#include ` gubbins
This is me nicking my toolkit before we start. ``—fancy pants “standard input-output”—lets me use `printf` and `scanf`. 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 arr[10],n,i,key,found=0;`
These are my lil buckets. `arr[10]` is a big array pot—10 numbers tops, plenty for a quick go. `n` is how many ya want, `i` for looping, `key` is what we’re hunting, `found=0` is my flag—stays 0 til we nab it.

4. chatting at ya: `printf` and `scanf`
So yeah, `printf("how many numbers ya want: ")` is me barking, “Oi, how many, sharpish!” Then `scanf("%d",&n)` grabs it. If `n<=0||n>10`, it’s “oi, 1 to 10 only!” Else, `printf("gimme %d numbers: ")` and a `for` loop nabs em into `arr`. Then `printf("gimme the number to find: ")` and `scanf("%d",&key)` gets what we’re after. `%d` for integers, `&` like a post-it saying “bung it here, ya twit!”

5. hunting it: `for(i=0;i
Here’s the guts! `for(i=0;i

6. no luck check: `if(found==0)`
If `found`’s still 0 after the loop, `printf("%d ain’t in there, mate!\n",key)` tells ya it’s not there. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

7. `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 hunt a number in an array. Faff with it—chuck in daft numbers, tweak it to say “found the bugger” if ya fancy. First time I ran this, I lost 7 in a mess of 10s—took me ages to spot it! You’re a legend already, mate—keep smashing it! 😊