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 24th, 2005, 02:21 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: 4
Algorithm Efficiency Help!!!!

Hello ppl.
Ok have to investigate and write a report on the efficiency of the following Linear Search algorithm.
But im having problems starting off.... can anyone here help me kick start this 3 page report... would apreciate any information & help you guys here can give.

Code:
//   This program randomly generates a series of test data of 

// differing sizes 

//   (5 - 100 positive integers) reading them into array a. 

//   It then randomly generates a search key 

//   and searches the elements of the array using linear search. 

//   It returns a table giving the amount of data the test was 

//   run on and the number of comparisons made. 

  

  

#include <stdlib.h>     

// Make standard library available eg for random number 

// generation functions 

#include <stdio.h>     // Make input/output  library available 

#include <time.h> 

  

// define a new type called SearchResult  

//(like int is already a type) 

//There are two values (like 1, 2,3 etc are values for ints) 

// of type searchResult: FOUND and NOT_FOUND 

enum SearchResult {FOUND, NOT_FOUND}; 

  

  

// List the functions in this program 

SearchResult linearSearch 

   (int array[], int key, int sizeOfData, 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("lsrch.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"); 

  

       // 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; 

  

       fill_array(a, datasize); 

  

       result = linearSearch(a, searchKey, datasize, &count);  

           // Note address of count passed - so by reference 

        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); 

     } 

     } 

   else 

     fprintf(resultsFile, "File not opened\n"); 

  

   fclose(resultsFile); return 0; 

  } 

  

  

// generate random data in range 0-20 and fill the 

// array with it up to the given size 

  

void fill_array(int array[], int sizeOfData) 

 { 

  

     for (int counter = 0; counter < sizeOfData; counter++) 

      { 

     array[counter] = rand () % RANGE; 

      } 

 } 

  

  

// A linear search function that returns the number of 

// comparisons 

// call by value as well as a direct indication of found or not. 

  

SearchResult linearSearch 

      (int array[], int key, int sizeOfData, int * comparisons) 

 { 

  

   *comparisons = 0;  // count comparisons made 

   //Note comparisons passed by reference so use * to refer 

   //to value at that address 

  

   for (int counter = 0; counter < sizeOfData; counter++) 

    { 

      *comparisons = *comparisons + 1;  

                  //about to do a comparison 

  

      if (array[counter] == key) 

         return FOUND;     // Key found! 

    } 

  

   return NOT_FOUND;           // Key not found 

 } 



Again i would apreciate all help.
Thanks
Peace!!

Reply With Quote
  #2  
Old October 24th, 2005, 06:20 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
Do you mean efficiency in terms of code length, or run time, cpu usage, etc...?

Reply With Quote
  #3  
Old October 25th, 2005, 07:23 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: 4
i need to produce a report and detailed analysis of the results for that algorithm. This should include comparison graphs, explanations of efficiency, insights of suitability to the task and how the algorithms may be improved or replaced.

This involves investigating the efficiency of a program by generating data on its efficiency, plotting graphs and analysing the results, comparing them with that predicted by theory and explaining any anomalies.
Questions i thought of answering in the process are:
What is the equation of any line generated?
What order does this line imply the algorithm is?
What does theory say the order of the algorithm is?
What order would one expect from studying the program itself?
If there are differences in the above can I explain them?
Have I considered best worst and average cases?
Are there any anomalies in the graph (such as points not on a smooth line)?
If so can you explain them?
How does this implementation of an algorithm compare with variations on the algorithm e.g. that are intended to make it more efficient?
How does this algorithm compare with other algorithms to do the same thing?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Algorithm Efficiency Help!!!!


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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT