# Thread: Help with Linear Search

1. The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas? Something to do with the array again?

2. Well, I hope you read my last post. Post the updated code

3. I am pretty sure that I have got the code right and made the amendments that you suggested.

Code:
``` #include<stdio.h>
void main(void)
{

int search, c, n, count = 0, number, count1[100]={0}, i;
char array[100];

printf("Enter the character of elements in array\n");
n = 5;

printf("Enter %d numbers\n", n);

for ( c = 0 ; c < n ; c++ )
scanf(" %c ",&array[c]);

for(i=0;i<n;i++)
{
count1[array[i]]++;                                                              //if array[i] is N, then count1[N]++//
}

for(i=0;i<100;i++)
{
if(count1[i])
printf("%c is present %d times in array.\n",i,count1[i]);
else
printf("");
}

return 0;
}```

4. Partially yes

As said, main has to return int.

Now, I asked before, what this line does?
Code:
`count1[array[i]]++;`
and I say again! array is of char type, so you are indexing an array (the count1 array) with a letter. In C, this is not good.

5. But you know what? I actually don't like this approach.

Think about the problem. You need for every piece of data a corresponding counter. It's a one to one assignment (I don't know if that makes sense as I said, but in math we have some set that every element corresponds to another. Here we have sets of equal size with elements that match to only one. Well, enough with math :P)

Use a struct, which has as members the piece of data ( a char ) and an integer counter. Then create one array only and then... well I' ll leave that to you. You found your way here, which means that you have understood how things go

6. Originally Posted by std10093
Partially yes

As said, main has to return int.

Now, I asked before, what this line does?
Code:
`count1[array[i]]++;`
This line means if array[i] is N, then count1[N]++ I believe.

7. But array is of char type!!!! In C, you can not index an array with a letter!

I said my opinion about the approach.

8. Ok, that was what another user suggested. I am new to programming.

9. Oh, ok then

10. Your initial post said this was for numbers. Do you need just letters, now? Or letters and numbers both?

11. Originally Posted by std10093
But array is of char type!!!! In C, you can not index an array with a letter!
Actually you can because char is just an integer type.

Originally Posted by SDH
The program is working now but for reason will only recognise a,b,c. Anything after c it will not work out. Any ideas?
And here you see how it works (assuming ASCII encoding for the characters):
The character 'a' has the ASCII value 97, 'b' is 98, 'c' is 99, 'd' is 100, ...

Now when you do
Code:
`count1[array[i]]++`
where array[i] holds a character, you get back the ASCII value for it. But since "count1" is declared having 100 elements (with indices 0 to 99) you are writing outside the legal memory for any lowercase character except 'a', 'b' and 'c'.

If you want to be able to count all characters you have to either increase "count1" (e.g. declaring it as count1[128] for all ASCII characters) or map the ASCII values to a smaller range.

Bye, Andreas

12. Thank you very much AndiPersti. I have now got it work properly. Hopefully this will be my last post.

13. Oh, that's why it didn't feel right when I was typing it.. Thanks for the correction! And SHD thanks too I guess

14. Nearly finished on this program! Cant wait to say its actually done! Been my life for the last week ( and yours too ).

15. Don't forget to inform us