8 Replies - 15096 Views - Last Post: 18 September 2006 - 03:01 AM Rate Topic: -----

#1 Chios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Chios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
   }
   printf("Number not found after %d steps\n", step);
}

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
Was This Post Helpful? 0
  • +
  • -

#4 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Chios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 Chios  Icon User is offline

  • New D.I.C Head

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

Re: Creating a Linear Search

Posted 18 September 2006 - 02:49 AM

Sorry. Ok I understand now. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 zyruz  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 Chios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1