C++ 6. Search Benchmarks.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 2
Reputation: xexex is an unknown quantity at this point 
Solved Threads: 0
xexex xexex is offline Offline
Newbie Poster

C++ 6. Search Benchmarks.

 
1
  #1
Sep 16th, 2006
"Write a program that has an array of at least 20 integers. It should call a function that uses the linear search algoritm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen."

My teacher has requested to use a text file for the integers that has 20,000 of them.

How can I call the text file? It's been to long and I can't find it in the book? I also can't find where it shows how to get the function to cout the number of times it has to search until it finds the value. Haven't tried the binary search yet, can't get past this part.

Thanks!

  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Function prototype
  6. int searchList(int[], int, int);
  7.  
  8. const int SIZE = 20000; //???
  9.  
  10. int main()
  11.  
  12. {
  13.  
  14. int bench[SIZE] = {
  15.  
  16.  
  17. };
  18.  
  19.  
  20. int results;
  21.  
  22.  
  23. }
  24.  
  25. int searchList(int list[], int numElems, int value)
  26. {
  27. int index = 0; // Used as a subscript to search array
  28. int position = -1; // To record position of search value
  29. bool found = false; // Flag to indicate if the value is found
  30.  
  31. while (index < numElems && !found)
  32. {
  33.  
  34. if (list[index] == value) // If the value is found
  35. {
  36. found = true; // Set the flag
  37. position = index; / Record the value's subscript
  38. }
  39. index++; // Go to the next element
  40. }
  41. return position; // Return the position, or -1
  42. }
  43.  
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ 6. Search Benchmarks.

 
0
  #2
Sep 16th, 2006
Use ifstream to open and read the integers from a text file into an array. Your program should have a loop that will read a single integer and insert it into the next available index of the array. I'm sure your text book has examples of how to do something like that.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 2
Reputation: xexex is an unknown quantity at this point 
Solved Threads: 0
xexex xexex is offline Offline
Newbie Poster

Re: C++ 6. Search Benchmarks.

 
0
  #3
Sep 16th, 2006
Something like this?

I'm still confused

  1. add #include <fstream>
  2.  
  3.  
  4. ifstream in("C:/path/to/text/file.txt");
  5. int counter = 0;
  6. while (in.good() && counter <= SIZE)
  7. {
  8. bench[counter] = (int)in.getLine(); //assuming each line stores one number
  9. counter++;
  10. }
  11. in.close()
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ 6. Search Benchmarks.

 
0
  #4
Sep 16th, 2006
close. After this loop finished, counter will contain the number of elements read, so you should use that variable instead of SIZE when searching the array.

  1. add #include <fstream>
  2.  
  3.  
  4. ifstream in("C:/path/to/text/file.txt");
  5. int counter = 0;
  6. while (counter < SIZE && in >> bench[counter] )
  7. {
  8. counter++;
  9. }
  10. in.close()
Last edited by Ancient Dragon; Sep 16th, 2006 at 8:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: flexeblesteel is an unknown quantity at this point 
Solved Threads: 0
flexeblesteel flexeblesteel is offline Offline
Newbie Poster

Re: C++ 6. Search Benchmarks.

 
0
  #5
Apr 23rd, 2007
xexex,
please copy and paste whole program's source code so i can better assist you. also, i'm curious how you did it.

thanks,
flexeblesteel
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC