Sorting values (const double)

Hi everyone, I am new in c++. I got a task to sort startNode[i] in ascending order of mapping fitness. I had review on topic for sorting values. I had try to do it, but error occur. I cant figure it out where is the mistake i have made. Hoping for anyone to help me. Thanks.

input file in (.txt) as follow:
3 272
4 265
5 279
6 295
7 331
8 281
9 303

Here is the code:

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

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
    int i;
    const int Initial_Population = 7;
    int startNode [Initial_Population];
    double objFunction [Initial_Population];

    // store data into array.
    {
        // open the file for input
        ifstream inputFile("myFile.txt");

        // the input file was opened successfully and contains valid data
        for( int i = 0; i < Initial_Population; i++ )
        inputFile >> startNode[i] >> objFunction[i];

        // the file is automatically closed
    }

    double max = objFunction [0];
    double min = objFunction [0];

    for( int i = 0; i < Initial_Population; i++ )
    {
		//finding maximum value of objFunction
        if( objFunction[i] > max ) 
		{
			max = objFunction[i];
		}
		//finding minimum value of objFunction
        else if( objFunction[i] < min ) 
		{
			min = objFunction[i];
		}
    }

    // calculate the fitness and mapping fitness for each startNode[i].
    const double diff = max - min ;
    if( diff > 0 )
    {
        for( int i = 0; i < Initial_Population; i++ )
        {
            const double fitness = ( max - objFunction[i] ) / diff ;
			const double mappingFitness = ( 0.5 * ( tanh ( 4 * fitness - 2 ) + 1 ));
			//cout << "startNode" << startNode [i] << "," << endl;
			//cout << "fitness value:	" << fitness << endl;
			//cout << "mapping value:	" << mappingFitness << endl;
		}
        
    }

	// sort startNode[i] in ascending order of mapping fitness.

	const double mappingFitness;
	sort ( mappingFitness.begin(), mappingFitness.end(), [] (const double a, const double b)
	{
		return a < b;
	}
	for ( const double mappingFitnes : mappingFitnesses )
	cout << startNode[i] << mappingFitness << endl;

	system("pause");

	return 0;
}
This only make sense if mappingFitness is a container, but it isn't. It's a single value.
1
2
3
4
5
	const double mappingFitness;
	sort ( mappingFitness.begin(), mappingFitness.end(), [] (const double a, const double b)
	{
		return a < b;
	}


The default comparison (std::less) is sufficient and is defined for double, so you shouldn't need to specify the sort predicate.

Perhaps, mappingFitness should be an array too? I dunno, but if it were, you code might look like:
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
    const int Initial_Population = 7;
    int startNode [Initial_Population];
    double objFunction [Initial_Population];

    // store data into array.
    {
        ifstream inputFile("myFile.txt");

        // the input file was opened successfully and contains valid data
        for( int i = 0; i < Initial_Population; i++ )
            inputFile >> startNode[i] >> objFunction[i];
    }

    double max = objFunction [0];
    double min = objFunction [0];

    for( int i = 0; i < Initial_Population; i++ )
    {
        //finding maximum value of objFunction
        if( objFunction[i] > max )
        {
            max = objFunction[i];
        }
        //finding minimum value of objFunction
        else if( objFunction[i] < min )
        {
            min = objFunction[i];
        }
    }

    // calculate the fitness and mapping fitness for each startNode[i].
    double mappingFitness [Initial_Population] = { 0.0 };
    const double diff = max - min ;
    if( diff > 0 )
    {
        for( int i = 0; i < Initial_Population; i++ )
        {
            const double fitness = ( max - objFunction[i] ) / diff ;
            mappingFitness[i] = ( 0.5 * ( tanh ( 4 * fitness - 2 ) + 1 ));
            //cout << "startNode" << startNode [i] << "," << endl;
            //cout << "fitness value:   " << fitness << endl;
            //cout << "mapping value:   " << mappingFitness << endl;
        }
    }

    // sort startNode[i] in ascending order of mapping fitness.
    sort ( startNode, startNode + Initial_Population );
    for( int i = 0; i < Initial_Population; i++ )
        cout << startNode[i] << "\t" << mappingFitness[i] << endl;

    system("pause");

    return 0;
}
Last edited on
Thanks for your help..
Topic archived. No new replies allowed.