# Creating a Linear Search

Page 1 of 1

## 8 Replies - 15695 Views - Last Post: 18 September 2006 - 03:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18676&amp;s=4c5c04455f2bfc16597ba3106d7decdf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Chios

Reputation: 0
• Posts: 5
• Joined: 17-September 06

# Creating a Linear Search

Posted 17 September 2006 - 11:42 PM

```
#include <stdio.h>

#define SIZE 15

void binarySearch(int numbers[], int value);
void linearSearch(int numbers[], int value);

int main()
{
int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1\$

printf("Searching for -1 in numbers using linear search\n");
linearSearch(numbers, -1);
printf("Searching for -1 in numbers using binary search\n");
binarySearch(numbers, -1);

printf("\nSearching for 0 in numbers using linear search\n");
linearSearch(numbers, 0);
printf("Searching for 0 in numbers using binary search\n");
binarySearch(numbers, 0);

printf("\nSearching for 7 in numbers using linear search\n");
linearSearch(numbers, 7);
printf("Searching for 7 in numbers using binary search\n");
binarySearch(numbers, 7);

printf("\nSearching for 11 in numbers using linear search\n");
linearSearch(numbers, 11);
printf("Searching for 11 in numbers using binary search\n");
binarySearch(numbers, 11);

printf("\nSearching for 15 in numbers using linear search\n");
linearSearch(numbers, 15);
printf("Searching for 15 in numbers using binary search\n");
binarySearch(numbers, 15);

printf("\nSearching for 22 in numbers using linear search\n");
linearSearch(numbers, 22);
printf("Searching for 22 in numbers using binary search\n");
binarySearch(numbers, 22);

return 0;
}

void linearSearch(int numbers[], int value)
{
/* Use linear search to find value in numbers.
Print the number of steps it takes to find or not find value
*/

for (int i=0; i < SIZE; i++)
{
if (value == SIZE[i])
{
return i;
}
else return -1;
}
}

```

I am trying to create a linear search in C as a part of some homework for school but I can't seem to work it out. I have tried to do it (See code) but yeah it doesn't work. Any help would be great. Thanks

Is This A Good Question/Topic? 0

## Replies To: Creating a Linear Search

### #2 zyruz

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Creating a Linear Search

Posted 18 September 2006 - 01:08 AM

First off post wath errors you get so people dont need to compile the code.

without compiling it I see some errors:

```void binarySearch(int numbers[], int value);
void linearSearch(int numbers[], int value);
```

becuse they are void, they wont return annything, you need to change from void to int..

```  int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1\$
```

dont end, you lack "};"

```if (value == SIZE[i])
```

SIZE aint a array, you shuld be using numbers[i];

Zy

This post has been edited by zyruz: 18 September 2006 - 01:09 AM

### #3 Chios

Reputation: 0
• Posts: 5
• Joined: 17-September 06

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:17 AM

Thanks Zy, I have updated the code and am still getting errors when I compile.

```#include <stdio.h>

#define SIZE 15

void binarySearch(int numbers[], int value);
void linearSearch(int numbers[], int value);

int main()
{
int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };

printf("Searching for -1 in numbers using linear search\n");
linearSearch(numbers, -1);
printf("Searching for -1 in numbers using binary search\n");
binarySearch(numbers, -1);

printf("\nSearching for 0 in numbers using linear search\n");
linearSearch(numbers, 0);
printf("Searching for 0 in numbers using binary search\n");
binarySearch(numbers, 0);

printf("\nSearching for 7 in numbers using linear search\n");
linearSearch(numbers, 7);
printf("Searching for 7 in numbers using binary search\n");
binarySearch(numbers, 7);

printf("\nSearching for 11 in numbers using linear search\n");
linearSearch(numbers, 11);
printf("Searching for 11 in numbers using binary search\n");
binarySearch(numbers, 11);

printf("\nSearching for 15 in numbers using linear search\n");
linearSearch(numbers, 15);
printf("Searching for 15 in numbers using binary search\n");
binarySearch(numbers, 15);

printf("\nSearching for 22 in numbers using linear search\n");
linearSearch(numbers, 22);
printf("Searching for 22 in numbers using binary search\n");
binarySearch(numbers, 22);

return 0;
}

void binarySearch(int numbers[], int value)
{
int step = 1, left = 0, right = SIZE-1, mid;

while (left < right)
{
mid = (left + right) / 2;
if (numbers[mid] == value)
{
printf("Number found after %d steps\n", step);
return;
}
else if (numbers[mid] < value)
left = mid+1;
else
right = mid-1;
}
}

void linearSearch(int numbers[], int value)
{
/* Use linear search to find value in numbers.
Print the number of steps it takes to find or not find value
*/

for (int i=0; i < numbers; i++)
{
if (value == numbers[i])
{
return i;
}

return -1;
}
}

```

The errors I am getting are:

search.c: In function `linearSearch':
search.c:71: error: 'for' loop initial declaration used outside C99 mode
search.c:71: warning: comparison between pointer and integer
search.c:75: warning: `return' with a value, in function returning void
search.c:78: warning: `return' with a value, in function returning void

### #4 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:39 AM

1) Linear search function is returning value and you have declared it as void. Don't return anything. Just give a descriptive message in linear search for the presence/absence of value in numbers array.

2) Declare the variable i before for loop in linear search function. Variables declaration inside conditional expression is not allowed in C89.

3) Compare i with SIZE, not with numbers.

Let's see if you can figure out now.

This post has been edited by Xing: 18 September 2006 - 02:52 AM

### #5 zyruz

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:42 AM

(xing replyed just before me :\ )
the probleme is that you chaged numbers in 2 places.
the:
``` for (int i=0; i < numbers; i++)
```

is wrong you need to change "numbers" to SIZE here.
and you need to change the functions from "void" to "int"

Zy

This post has been edited by zyruz: 18 September 2006 - 02:43 AM

### #6 Chios

Reputation: 0
• Posts: 5
• Joined: 17-September 06

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:44 AM

I'll have a play with 1 and 2 thanks.

But I'm a little confused with 3. I had SIZE instead of numbers originally but the first reply told me that was wrong. So what that right?

### #7 Chios

Reputation: 0
• Posts: 5
• Joined: 17-September 06

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:49 AM

Sorry. Ok I understand now. Thanks.

### #8 zyruz

Reputation: 1
• Posts: 31
• Joined: 13-August 05

## Re: Creating a Linear Search

Posted 18 September 2006 - 02:51 AM

edited : I'm to slow to post, people understnd by themself before I get to post :\

Zy

This post has been edited by zyruz: 18 September 2006 - 02:53 AM

### #9 Chios

Reputation: 0
• Posts: 5
• Joined: 17-September 06

## Re: Creating a Linear Search

Posted 18 September 2006 - 03:01 AM

Thanks guys! It compiles Now I just have to fix up the logical errors.

Thank you.