Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




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

 
Reply to this topicStart new topic

Help on alphabetizing a simple string list in ascending order from an, Alphabetizing using a selection sort

doyle1414
20 Apr, 2008 - 07:50 PM
Post #1

New D.I.C Head
*

Joined: 20 Apr, 2008
Posts: 2

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.

CODE


#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


User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Help On Alphabetizing A Simple String List In Ascending Order From An
21 Apr, 2008 - 05:13 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
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).
User is offlineProfile CardPM
+Quote Post

doyle1414
RE: Help On Alphabetizing A Simple String List In Ascending Order From An
21 Apr, 2008 - 05:41 PM
Post #3

New D.I.C Head
*

Joined: 20 Apr, 2008
Posts: 2

QUOTE(perfectly.insane @ 21 Apr, 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).




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?
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Help On Alphabetizing A Simple String List In Ascending Order From An
21 Apr, 2008 - 05:57 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(doyle1414 @ 21 Apr, 2008 - 06:41 PM) *

QUOTE(perfectly.insane @ 21 Apr, 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).




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 Apr, 2008 - 05:57 PM
User is offlineProfile CardPM
+Quote Post

Sepanto
RE: Help On Alphabetizing A Simple String List In Ascending Order From An
21 Apr, 2008 - 08:26 PM
Post #5

D.I.C Head
Group Icon

Joined: 20 Mar, 2008
Posts: 97


Dream Kudos: 50
My Contributions
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.
cpp

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;


User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Help On Alphabetizing A Simple String List In Ascending Order From An
22 Apr, 2008 - 01:54 PM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
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:

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



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:33PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month