GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Apr-2005, 04:57
eccoflame eccoflame is offline
New Member
 
Join Date: Apr 2005
Posts: 13
eccoflame is on a distinguished road

NEwbie new help


CPP / C++ / C Code:
#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 1;
      }
      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
   */
}


this up is the original code and contains some errors so need help and in linearSearch need add code


the result should be like this
Searching for -1 in numbers using linear search
Number not found after 16 steps
Searching for -1 in numbers using binary search
Number not found after 5 steps

Searching for 0 in numbers using linear search
Number found after 1 steps
Searching for 0 in numbers using binary search
Number found after 4 steps

Searching for 7 in numbers using linear search
Number found after 8 steps
Searching for 7 in numbers using binary search
Number found after 1 steps

Searching for 11 in numbers using linear search
Number found after 12 steps
Searching for 11 in numbers using binary search
Number found after 2 steps

Searching for 15 in numbers using linear search
Number found after 16 steps
Searching for 15 in numbers using binary search
Number found after 5 steps

Searching for 22 in numbers using linear search
Number not found after 16 steps
Searching for 22 in numbers using binary search
Number not found after 6 steps

thx
Last edited by LuciWiz : 18-Apr-2005 at 04:58. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 18-Apr-2005, 08:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by eccoflame

this up is the original code and contains some errors


So: post the exact errors that you got, and tell what you did to try to fix them.

Tell what compiler you are using (sometimes it makes a difference as we try to point out what the messages are really trying to tell you).

Regards,

Dave
  #3  
Old 18-Apr-2005, 17:44
eccoflame eccoflame is offline
New Member
 
Join Date: Apr 2005
Posts: 13
eccoflame is on a distinguished road
the error occurs in the void binarysearch function in the return
they saying warning: `return' with a value, in function returning void
search.c:57: parse error before `else'
like that i using linux to compile it..from school
CPP / C++ / C Code:
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 ;---> this one
      }
      else if (numbers[mid] < value)
   left = mid+1;
      else
         right = mid-1;
   }
   printf("Number not found after %d steps\n", step);
}
  #4  
Old 19-Apr-2005, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by eccoflame
the error occurs in the void binarysearch function in the return
they saying warning: `return' with a value, in function returning void
search.c:57: parse error before `else'
like that i using linux to compile it..from school

1. The "return with a value" message was from your original code (you used to have "return 1" here).


2. For the "parse error" message: get rid of the spurious semicolon on the following

CPP / C++ / C Code:
     if(numbers[mid] == value);

Here's the context:

CPP / C++ / C Code:
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)/* ; <=== This shouldn't be here */
      {
         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);
}


Regards,

Dave
Last edited by davekw7x : 19-Apr-2005 at 09:10.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 11:32
newbie using Sam's series Skampy C++ Forum 3 06-Mar-2004 18:51
Grouping data from MySQL with PHP - Newbie question. giobbi MySQL / PHP Forum 12 27-Feb-2004 00:34
Newbie Problem with cin trs2988 C++ Forum 4 08-Feb-2004 12:05
Problem with my Geforce video card Shivs Computer Hardware Forum 2 30-Jan-2004 20:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:51.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.