C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 31st, 2005, 09:14 AM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 5
Dont kNow Where this is going wrong??

Hello can neone help me on this.. Dontk now wats wrong with this Binary Search Algorithm..
Im trying to get it to work for my report,, im new to this c stuff....Would apreciate ne help..

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

enum SearchResult {FOUND, NOT_FOUND};

SearchResult binarySearch(int array[], int key, int low, int high,  int *comparisons);
void fill_array(int array[], int sizeOfData);

const int RANGE = 200; // random data used in range 0-199

int main()
  {
     const int arraySize = 100;
     int a[arraySize];
     FILE *resultsFile;
     resultsFile = fopen("bsrch.dat", "w");  // open the file lsrrch.dat for writing ("w")

     if (resultsFile != NULL)      // file successfully opened
      {
         // seed the random number generator so it gives different
       // random numbers each time
       srand ( time (0) );


       // Table headings
       printf ("Found\t Data Size\t Comparisons Needed\n"  );
                        //\t puts in a tab character, \n a new line
       fprintf (resultsFile, "Data Size\t Comparisons Needed\n"  );

       fill_array(a, arraySize);
       // Generate table for data of different sizes
       // giving number of comparisons each time

       for (int datasize = 1; datasize <= arraySize; datasize = datasize + 1)
	{
	  int count = 0;  // number of comparisons this time
	  SearchResult result; // result of this search
	  int searchKey = rand() % RANGE;


	  result = binarySearch(a, searchKey, 0, datasize -1, &count);
        if (result == FOUND)
             printf("KEY FOUND \t");
        else
             printf("NOT FOUND \t");

	  // print results in a table
          printf("%d  \t\t\t  %d\n", datasize, count);
          fprintf(resultsFile, "%d  \t\t\t  %d\n", datasize, count);
 	}

Reply With Quote
  #2  
Old October 31st, 2005, 10:32 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 956 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 23 m 51 sec
Reputation Power: 5
What is going wrong? Cannot compile, or the application does not want you want it to do? The code you pasted is incomplete (probably just missing some closing brackets, and the two forward declared functions) and does not compile directly on my machine either. Also, you want C or C++?

Reply With Quote
  #3  
Old October 31st, 2005, 02:34 PM
nabil1983 nabil1983 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 36 nabil1983 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 7 m 44 sec
Reputation Power: 5
When i try to compile it it dont work at all.
I can not find where the missing brackets are, so that i can atleast compile it in C. Can you help me with this please.

Reply With Quote
  #4  
Old October 31st, 2005, 05:07 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 14 m 46 sec
Reputation Power: 5
You're missing a closing brace ( } ) at the very end.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #5  
Old October 31st, 2005, 09:24 PM
jaf1211 jaf1211 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 21 jaf1211 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 28 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by B-Con
You're missing a closing brace ( } ) at the very end.


I think he's missing 2. I see three open ( { ) and only 1 closed( } )

Reply With Quote
  #6  
Old November 1st, 2005, 01:21 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 956 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 23 m 51 sec
Reputation Power: 5
You are also missing the code for the two forward declared functions. Can you post all the code + the full error message your compiler is giving?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Dont kNow Where this is going wrong??


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek