Sorting Names

Sort names using linear and binary searches?

Page 1 of 1

4 Replies - 1189 Views - Last Post: 12 December 2010 - 12:38 PM Rate Topic: -----

#1 fuzmaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 10

Sorting Names

Posted 08 December 2010 - 07:03 PM

I'm trying to get used to using the search functions, as their main focus is to just search for something specific in the array. I'm used to looking through numbers, since they only use int. But for this certain problem I'm trying to get, which states:

Create 2 search functions that search through an array of strings (read this from a file)
-A linear search
-A binary search
Pass an integer by reference which keeps track of how many comparisons each function performs for 3 runs each
-5 items
-10 items
-20 items
Resetting the value in between each set of calls
Print these numbers to the screen

I'm not sure basically how to search a text file, for in this case is a list of names, and to copy them into a array so the search functions can do their job. For the rest of the problem, I don't get what they're saying and would like context on what the book I'm learning this from can help me? This is the program so far.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int searchList(string [], int, string);


int main()
{
  int SIZE1;
  char names[20][20];
  char temp[20];
  int x = 0;
  int results1;
  string line;


  ifstream myfile ("names.txt");
  if (myfile.is_open())
  {
	int x=0;
    while (myfile.good() )
    {
      for(int i = 0; i < 20; i++)
	  {
		myfile.getline(temp,50);
		strcpy_s(names[i],temp);
		cout << line << endl;
	  }
    }
    myfile.close();
	
	results1 = searchList(names, SIZE1, "Conrad");
  }

	system("pause");
	return 0;
}

int searchList(string list[], int numElems, string value)
{
	int index = 0;
	int position = -1;
	bool found = false;

	while (index < numElems && !found)
	{
		if (list[index] == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position;
}

int binarySearch(int array[], int numElems, int value)
{
	int first = 0,
		last = numElems - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last)/2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return position;
}


This post has been edited by fuzmaster: 08 December 2010 - 07:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Names

#2 jimblumberg   User is offline

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Sorting Names

Posted 08 December 2010 - 08:39 PM

In the code below is there any reason you are not using std::string instead of C-strings? And std::vector instead of an array?


  if (myfile.is_open())
  {
	int x=0;
    while (myfile.good() )
    {
      for(int i = 0; i < 20; i++)
	  {
		myfile.getline(temp,50);
		strcpy_s(names[i],temp);
		cout << line << endl;
	  }
    }




Jim
Was This Post Helpful? 0
  • +
  • -

#3 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1862
  • View blog
  • Posts: 6,713
  • Joined: 19-February 09

Re: Sorting Names

Posted 08 December 2010 - 08:39 PM

There is a disparity between temp declaration and use.

  char temp[20]; // size = 20
  ...
  myfile.getline(temp,50); // size = 50



You should be able to get data directly into array.

  char names[20][50];
  ...
  myfile.getline(names[i], 50);
  cout << names[i] << endl; // display line of text



You could use C++ strings.

  string names[20][50];
  ...
  getline(myfile, names[i]);
  cout << names[i] << endl;




The while statement doesn't check the state of the file stream, while the file is being read within the for loop.

    while (myfile.good() )
    {
      for(int i = 0; i < 20; i++)
	  {


Was This Post Helpful? 0
  • +
  • -

#4 fuzmaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-December 10

Re: Sorting Names

Posted 12 December 2010 - 12:24 PM

i keep getting this error C2664 for the line "results1 = searchList(names, SIZE1, "Conrad");" and i keep changing alot in the program and idk what's up with that. Thanks for the reply though
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5812
  • View blog
  • Posts: 17,743
  • Joined: 25-December 09

Re: Sorting Names

Posted 12 December 2010 - 12:38 PM

Lets see, You define your searchList function as:

int searchList(string [], int, string); 



You define your variable names as:

 char names[20][20];


Then you call your function like:

    results1 = searchList(names, SIZE1, "Conrad");


And if you were to Google your error number "C2664" you would find this Error C2664 as the first link.

Now what would you say is wrong with the line you asked about?

You need to start to learn how to read your error messages. One of the things the error message should tell you is the line number where the compiler thinks the error is located.

As for the changing of a lot of lines in the program. Could it be you have multiple mistakes?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1