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

New Topic/Question
Reply



MultiQuote




|