954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ 6. Search Benchmarks.

"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!

#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
}
xexex
Newbie Poster
2 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Something like this?

I'm still confused

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()
xexex
Newbie Poster
2 posts since Sep 2006
Reputation Points: 14
Solved Threads: 0
 

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.

add #include <fstream>


ifstream in("C:/path/to/text/file.txt");
int counter = 0;
while (counter < SIZE && in >> bench[counter] )
{
    counter++;
}
in.close()
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

flexeblesteel
Newbie Poster
1 post since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You