5 Replies - 11301 Views - Last Post: 22 April 2008 - 02:54 PM Rate Topic: -----

#1 doyle1414   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-April 08

Help on alphabetizing a simple string list in ascending order from an

Post icon  Posted 20 April 2008 - 08:50 PM

Hi all, sorry for the seemingly simple question, but I am just learning sorting and this has been bugging me. I need a program that that reads the lists of animals (19 strings) from the given text file into an array, then sorts them into ascending order. I need to output the sorted array to a new file. Then, as long as the user wishes, search the list for user-specified names; if a match is found, output this fact (to the screen) along with the index at which the match was found. If no match is found, output this fact (to the screen). For each search, you MUST output (to the screen) how many elements of the array had to be checked in carrying out the search.

I don't think searching will be a problem for me, but I can't get the program to read right (and I keep getting a redefintion error on my array declaration). Any tips how to fix this and make it read right? I think my logic with the sort is right, but someone can help me out on that. BTW, it was recommended we use a simple linear sort, and we didnt learn how to use structures for this sort of thing yet, so the simpler the better.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

#define ARRAY_SIZE 50


int _tmain(int argc, _TCHAR* argv[])
{
	string animals[ARRAY_SIZE];

	ifstream in("animalinput.txt");
	int N = 0;

	while (!in.eof()) {
		in >> animals[N];
		if (!in.fail() ) {
			N++;
		}
	}

	const int N = 19;
	string data[19] = {animals[N]};

	//selection sort algorithm
	for (int startIndex = 0; startIndex < N-1; startIndex++) {
		int indexOfMin = startIndex;
		//find the minimum element
		for (int i = startIndex + 1; i < N; i++) {
			if (data[i] < data[indexOfMin]) {
				indexOfMin = i;
			}
		}
		//swap min element into proper place
		string temp = data[startIndex];
		data[startIndex] = data[indexOfMin];
		data[indexOfMin] = temp;
	}

	ofstream out("animaloutput.txt");
	out << animals[N];


	return 0;
}





the input file to read is:

dog
cat
boy
zebra
fish
eagle
bird
girl
tiger
lion
brother
sister
antelope
elephant
bear
monkey
donkey
mule
horse

Is This A Good Question/Topic? 0
  • +

Replies To: Help on alphabetizing a simple string list in ascending order from an

#2 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help on alphabetizing a simple string list in ascending order from an

Posted 21 April 2008 - 06:13 PM

Well, N is being defined twice (int N = 0; and const int N = 19;), so that is one issue.

Secondly, the item used in the initilizer list (animals[N]) should be an empty string, as animals[19] should not have been assigned (if the list read from the file actually is 19 items long).

You will need to code a loop to assign each of the items from animals to data. The initializer list would require all items to be written out in order to make it do what you want.

(If we wanted to do this a bit quicker, std::copy is an easy way to do this in one line).


Oh, and you'll need a loop to print the output (each array element).
Was This Post Helpful? 0
  • +
  • -

#3 doyle1414   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-April 08

Re: Help on alphabetizing a simple string list in ascending order from an

Posted 21 April 2008 - 06:41 PM

View Postperfectly.insane, on 21 Apr, 2008 - 06:13 PM, said:

Well, N is being defined twice (int N = 0; and const int N = 19;), so that is one issue.

Secondly, the item used in the initilizer list (animals[N]) should be an empty string, as animals[19] should not have been assigned (if the list read from the file actually is 19 items long).

You will need to code a loop to assign each of the items from animals to data. The initializer list would require all items to be written out in order to make it do what you want.

(If we wanted to do this a bit quicker, std::copy is an easy way to do this in one line).


Oh, and you'll need a loop to print the output (each array element).




Any idea on how to code the assignment of each of the items from animals to data? And if i'm defining N twice, how should it be set so this doesn't happen?
Was This Post Helpful? 0
  • +
  • -

#4 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help on alphabetizing a simple string list in ascending order from an

Posted 21 April 2008 - 06:57 PM

View Postdoyle1414, on 21 Apr, 2008 - 06:41 PM, said:

View Postperfectly.insane, on 21 Apr, 2008 - 06:13 PM, said:

Well, N is being defined twice (int N = 0; and const int N = 19;), so that is one issue.

Secondly, the item used in the initilizer list (animals[N]) should be an empty string, as animals[19] should not have been assigned (if the list read from the file actually is 19 items long).

You will need to code a loop to assign each of the items from animals to data. The initializer list would require all items to be written out in order to make it do what you want.

(If we wanted to do this a bit quicker, std::copy is an easy way to do this in one line).


Oh, and you'll need a loop to print the output (each array element).




Any idea on how to code the assignment of each of the items from animals to data? And if i'm defining N twice, how should it be set so this doesn't happen?


As far as defining N twice, you can:

1.) Define it once, but reassign it later (instead of declaring const int N = 19, write N = 19)
2.) Use a different variable name altogether.

As far as the assignment, in pseudo code, it's:

For N = 0 To count - 1 Do
data[N] = animals[N]

This post has been edited by perfectly.insane: 21 April 2008 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#5 Sepanto   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Help on alphabetizing a simple string list in ascending order from an

Posted 21 April 2008 - 09:26 PM

You could use a bubble sort using strcmp for alphebatizing, but to do that you must insure all letters are either capatilized or non capitalized.
Here's a piece of code I wrote for sorting a sentence (an array of strings). you could do pretty much the same.
void  Sentence::Sort()
{
     for(int i=0;i<num_of_words;i++)
     {
          for(int j=0;j<num_of_words-1;j++)
          {
		//2.6.1comparing using the > algorithem in Word class
                if((*words[j])>(*words[j+1]))
                	swap(words[j], words[j+1]);
          }
     }
}
bool Word::operator>(Word const&w)
{
//1.11.1 like in operator == I define two temporary variables of lowercase
//10.1 only letters and compare them using strcmp.
Word w1=w;
char *temp1=no_upper(),*temp2=w1.no_upper();
	if (strcmp(temp1,temp2)>0)
		return true;
	else 
		return false;



Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Help on alphabetizing a simple string list in ascending order from an

Posted 22 April 2008 - 02:54 PM

If one is going to resort to plain C functions, then one could use strcasecmp (case insensitive comparison, though on some systems, it might be stricmp).

i.e. To see if one string is less than another:

if(strcasecmp(data[0].c_str(), data[1].c_str()) < 0) {
   // Do something
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1