# Thread: Help with Linear Search

1. ## Help with Linear Search

Thanks for looking at my thread. I have successfully made a code that with search for mutliplies.

But I am now trying to modify it so that I dont have to tell it which numbers to search for. I would like it to just output how many of each number there is.

Would changing the code so it says that it tries out every single number away, so would using a nested loop with a counter to add one to the value of the number searched for work?

Here is my code for the linear search for number to help anyone out. Really appreciate anyone who has even looked at my thread let alone replied. Thanks.

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

int array[100], search, c, n, count = 0;

printf("Enter the number of elements in array\n");
scanf("%d",&n);

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

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

printf("Enter the number to search\n");
scanf("%d",&search);

for ( c = 0 ; c < n ; c++ )
{
if ( array[c] == search )
{
printf("%d is present at location %d.\n", search, c+1);
count++;
}
}
if ( count == 0 )
printf("%d is not present in array.\n", search);
else
printf("%d is present %d times in array.\n", search, count);

return 0;
}```

2. Originally Posted by SDH
Would changing the code so it says that it tries out every single number away, so would using a nested loop with a counter to add one to the value of the number searched for work?
Yes. Make your count an array count[0]...count[100], each member starting at 0. Each time you find the element with value i in array, increment count[i]. Then you have a tally of each value 0...100 that occured in array.

3. I have done that using
Code:
` for ( nu = 0; nu <10 ; nu++)`
because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement
Code:
` printf("%d is present %d times in array.\n", nu, count);`
however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
thanks

4. Make `count' an array rather than a scalar. And post more of the context because it's not clear by the for loop with `nu' that you posted.

5. Originally Posted by SDH
I have done that using
Code:
` for ( nu = 0; nu <10 ; nu++)`
because I am only inputting values of one to ten into the program. I have go it succesfully find which number are in the input but when I would like it say how many times each one occurs. I am using the statement
Code:
` printf("%d is present %d times in array.\n", nu, count);`
however this doesnt work. It just counts from 1 to 5. I think it has got something to do with the %d assigned to count but I am not totally sure.
thanks
actually the i have done that part, isnt even in the code you posted!!! so it isnt like we could tell you WHY it dont work, IF it isnt in your code!

6. Here is the code that I am trying to work on.
Code:
``` #include<stdio.h>
int main(void)
{

int array[100], search, c, n, count = 0, number, count1[100];

printf("Enter the number of elements in array\n");
scanf("%d",&n);

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

for ( c = 0 ; c < n ; c++ )

scanf("%d",&array[c]);

for ( number = 0; number <10 ; number++)

for ( c = 0 ; c < n ; c++ )
{
if ( array[c] == number )
{
printf("%d is present at location %d.\n", number, c+1);
count++;

}

}

if ( count == 0 )
printf("%d is not present in array.\n", number);
else
printf("%d is present %d times in array.\n", number, count);

return 0;
}```
I have changed nu to number just so it is easier to read.
The input is -
5
1 1 1 7 2

The output is -
Enter the number of elements in array
Enter 5 numbers
1 is present at location 1.
1 is present at location 2.
1 is present at location 3.
2 is present at location 5.
7 is present at location 4.
10 is present 5 times in array.

But I would like the output to read, "1 is present 3 times in array"
"2 is present 1 times in array"
7 is present 1 times in array"

7. Originally Posted by SDH
Here is the code that I am trying to work on.

I have changed nu to number just so it is easier to read.
The input is -
5
1 1 1 7 2

The output is -
Enter the number of elements in array
Enter 5 numbers
1 is present at location 1.
1 is present at location 2.
1 is present at location 3.
2 is present at location 5.
7 is present at location 4.
10 is present 5 times in array.

But I would like the output to read, "1 is present 3 times in array"
"2 is present 1 times in array"
7 is present 1 times in array"

C99Tutorial already stated how to do this. You glossed over that post, without really understanding it!

Code:
```#include<stdio.h>

int main(void)
{

//added i to the list of int variables, and initialize count1 to all zero's
int array[100], i, search, c, n, count = 0, number, count1[100]={0};

printf("Enter the number of elements in array\n");
scanf("%d",&n);

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

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

//new: tricky counting
for(i=0;i<n;i++)   { //for every number
count1[array[i]]++; //if array[i] is N, then count1[N]++
}

//new: reporting
for(i=0;i<100;i++) { //scan the whole range of numbers
if(count1[i])      //there was at least one with this value
printf("%d is present %d times in array.\n",i,count[i]);
else
printf("%d was not in the array\n",i);
}

return 0;
}```
Don't feel badly though - it's tricky!

8. Adak - I have tried to compile that but for some reason it wont work :s.

Says there is an error on line 25. : In function 'main': Line 25 ERROR: subscripted value is neither array nor pointer
compilation terminated due to -Wfatal-errors.

I have tried to solve this issue myself but with no luck, any ideas?

9. Code:
`printf("%d is present %d times in array.\n",i,count[i]);`
@SDH: It appears to be a minor typo - "count[i]" should be "count1[i]". Notice that, at the top of "main()", "count" is declared as an int, and "count1" is declared as an array of int.

10. Originally Posted by c99tutorial
Yes. Make your count an array count[0]...count[100],..
while the code is
Originally Posted by SDH
Code:
``` ...
int array[100];
...```
shouldn't we say count[0] to count[99]?
Or maybe I am mistaken

//SHD didn't do fortunately the same mistake

11. Matticus nailed it - yes, count is not an array, it should be count1[i] in that printf() statement.

12. Thank you very much, I knew it was something simply just could not spot it myself. Thanks

13. Originally Posted by SDH
Thank you very much, I knew it was something simply just could not spot it myself. Thanks
As you < ahem! > noticed, that code was just "off the cuff", and has not been run (let alone tested). So be sure to test it thoroughly, yourself.

And you're welcome.

14. I have no tried to get it to work but with characters. I am once again having problems.
Here is my code,
Code:
``` #include<stdio.h>
void main(void)
{

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

printf("Enter the number of elements in array\n");
scanf("%d",&n);

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;
}```
The input I am testing it with is
5
a s a b d

With the output being -
Enter the number of elements in array
Enter 5 numbers

is present 1 times in array.
a is present 2 times in array.
b is present 1 times in array.

So I am assuming it is something to do with the loops again.

15. Change main to return int as you nicely had before

i is a counter, thus it has to be an integer

this
Code:
`printf("Enter %d numbers\n", n);`
should be this
Code:
`printf("Enter %d characters\n", n);`
:P

%c will not eat the newline character (..because it is a character!). %d does this.
So, leave a space before %c, like this
Code:
`scanf(" %c ",&array[c]);`
Now tell me what does this code do
Code:
``` for(i=0;i<n;i++)
{
count1[array[i]]++;
}```
Hint : array is of type char!