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