What is wrong with my selection sort?

Hello,

could someone look over my selection sort function and let me know if my logic is messed up? I also get teh following error " cannot convert creativetype to int"

Thank you very much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
const int MAX_HORSE = 25;

struct creative_type
{
  string horseName;
  int horseIQ;
  int horseGPA;
};

void selectionSort (creative_type creativeArray[], int length);
void printHorseData (creative_type creativeArray[],int numHorse);

int main()
{
creative_type creativeArray[MAX_HORSE];
int numElements = 0;

ifstream fin;

fin.open("sort.txt");

if (!fin)
        cout << "Infile did not open.";

selectionSort (creativeArray, 25);

fin >> creativeArray[numElements].horseName;

while (fin && numElements < MAX_HORSE )
{

 fin >> creativeArray[numElements].horseIQ;
 fin >> creativeArray[numElements].horseGPA;

    if (numElements < MAX_HORSE)
        fin >> creativeArray[numElements].horseName;

}

printHorseData (creativeArray, numElements);


fin.close();
fin.clear();

    return 0;
}
void selectionSort(creative_type creativeArray[], int length)
{
    int i;
    int smallest;
    int location;
    int temp;

    for (i = 0; i < length-1; i++)
    {
        smallest = i;

        for (location = i +1; location < length; location++)
        {
            if (creativeArray[location] < creativeArray[smallest])
                smallest = location;

            temp = creativeArray[smallest];
            creativeArray[smallest] = creativeArray[i]
            creativeArray[i] = temp;
        }
    }


}
void printHorse (creative_type creativeArray[], int numHorse)
{
    cout << setw(15) << "Name" << setw(15)<< "IQ" << setw(15) << "GPA" << endl;

    for (int i = 0; i < numHorse; i++)
    {
        cout << creativeArray[i].horseName;
        cout << creativeArray[i].horseIQ;
        cout << creativeArray[i].horseGPA;
    }



}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void selectionSort(creative_type creativeArray[], int length)
{
    int i;
    int smallest;
    int location;
    int temp; //if  creativeArray is an array of creative_type  which is a struct - then temp cannot be an integer. See below

    for (i = 0; i < length-1; i++)
    {
        smallest = i;

        for (location = i +1; location < length; location++)
        {
            if (creativeArray[location] < creativeArray[smallest])
                smallest = location;

            temp = creativeArray[smallest]; //see note above
            creativeArray[smallest] = creativeArray[i] //There is a semicolon missing here...
            creativeArray[i] = temp; //see note above.
        }
    }


}
Last edited on
Topic archived. No new replies allowed.