| | |
C++ 6. Search Benchmarks.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2006
Posts: 2
Reputation:
Solved Threads: 0
"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!
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!
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; // Function prototype int searchList(int[], int, int); const int SIZE = 20000; //??? int main() { int bench[SIZE] = { }; int results; } int searchList(int list[], int numElems, int value) { int index = 0; // Used as a subscript to search array int position = -1; // To record position of search value bool found = false; // Flag to indicate if the value is found while (index < numElems && !found) { if (list[index] == value) // If the value is found { found = true; // Set the flag position = index; / Record the value's subscript } index++; // Go to the next element } return position; // Return the position, or -1 }
•
•
Join Date: Sep 2006
Posts: 2
Reputation:
Solved Threads: 0
Something like this?
I'm still confused
I'm still confused
C++ Syntax (Toggle Plain Text)
add #include <fstream> ifstream in("C:/path/to/text/file.txt"); int counter = 0; while (in.good() && counter <= SIZE) { bench[counter] = (int)in.getLine(); //assuming each line stores one number counter++; } in.close()
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.
C++ Syntax (Toggle Plain Text)
add #include <fstream> ifstream in("C:/path/to/text/file.txt"); int counter = 0; while (counter < SIZE && in >> bench[counter] ) { counter++; } in.close()
Last edited by Ancient Dragon; Sep 16th, 2006 at 8:47 pm.
![]() |
Similar Threads
- Using Search Engine Friendly PHP URLs (PHP)
- help please cannot use search button on ie6 (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: i need help.. C++ unresolved external symbol?
- Next Thread: Repost of Bubblesort error, wont debug
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game generator getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






