Having trouble calculating + printing max/min values

Hi all,

I'm trying to write a program that reads the data below from an input file, planetsinput.txt, computes the density for each planet and prints the answer to an output file, planetsoutput.txt, along with the highest and lowest values.

Currently I have everything working except for the fact that I'm getting an incorrect output for max and min.

Any help would be much appreciated.

Thanks!

INPUT FILE DATA
1
2
3
4
5
6
7
8
9
Mercury		2440		3.30e+23
Venus		6052		4.87e+24
Earth		6371		5.97e+24
Mars		3390		6.39e+23
Jupiter		69911		1.90e+27
Saturn		58232		5.68e+26
Uranus		25362		8.68e+25
Neptune		24622		1.02e+26
Pluto		1186		1.31e+22


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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void Input();

int main()	{
    Input();
    return(0);
}


void Input()	{
    
    string Planet[9];
    double Radius[9];
    float Mass[9];
    const double pi = 3.14159;
    int i=0;
    
    double volume;
    double density[i];
    
    double min_x;
    min_x = density[0];
    
    double max_x;
    max_x = density[0];
    
    ifstream fin;
    ofstream fout;
    
    fout.open("planetsoutput.txt");
    
    fin.open("planetsinput.txt");
    if(fin.fail())	{
        cerr << "couldn’t open the file!" << endl;
        exit(1);
    }
    
    for (i=0; i<9; ++i)	{
        fin >> Planet[i] >> Radius[i] >> Mass[i];
        
        volume = ((4)*(pi)*(Radius[i]*Radius[i]*Radius[i]))/(3);
        density[i] = Mass[i]/volume;
        
        fout << Planet[i] << endl;
        fout << "--------" << endl;
        fout << "Radius: " << Radius[i] << endl;
        fout << "Mass: " << Mass[i] << endl;
        fout << "Volume: " << volume << endl;
        fout << "Density: " << density[i] << "\n\n" << endl;
    }
    
    for (int k=0; k<=8; ++k)	{
        if (density[k] > density[!k])
            max_x = density[k];
    }
    fout << "The max is " << max_x << endl;
    
    for (int k=0; k<=8; ++k)	{
        if (density[k] < density[!k])
            min_x = density[k];
    }
    
    fout << "The min is " << min_x << endl;
    
    
    fin.close();
    fout.close();
}


OUTPUT FILE DATA
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
Mercury
Radius: 2440
Mass: 3.3e+23
Volume: 6.08496e+10
Density: 5.42321e+12


Venus
--------
Radius: 6052
Mass: 4.87e+24
Volume: 9.28507e+11
Density: 5.24498e+12


Earth
--------
Radius: 6371
Mass: 5.97e+24
Volume: 1.08321e+12
Density: 5.51142e+12


Mars
--------
Radius: 3390
Mass: 6.39e+23
Volume: 1.63188e+11
Density: 3.91574e+12


Jupiter
--------
Radius: 69911
Mass: 1.9e+27
Volume: 1.43128e+15
Density: 1.32748e+12


Saturn
--------
Radius: 58232
Mass: 5.68e+26
Volume: 8.27129e+14
Density: 6.86713e+11


Uranus
--------
Radius: 25362
Mass: 8.68e+25
Volume: 6.83343e+13
Density: 1.27023e+12


Neptune
--------
Radius: 24622
Mass: 1.02e+26
Volume: 6.25257e+13
Density: 1.63133e+12


Pluto
--------
Radius: 1186
Mass: 1.31e+22
Volume: 6.98783e+09
Density: 1.87469e+12


The max is 5.51142e+12
The min is 1.87469e+12
Can you explain the density[!k]?
Well I was trying to do something like

1
2
3
        if (density[k] > density[not k])
            max_x = density[k];
    }


i suppose i might not be able to do that operation.. but even then I'd wonder why it's still giving me some output.

Thoughts?
The ! takes a bool operand. Therefore, the k is implicitly converted first to bool. The ! negates the boolean value. Then the bool is converted back to integral value in order to do the array indexing.


However, wouldn't it be more logical to compare to max_x, if the intent is to determine whether max_x has to be updated?


There is also the other way:
max_x = *std::max_element( density, density + ???? );

The ???? above is, because your line 25 is bad. Are you really creating a 0 element long array?
Topic archived. No new replies allowed.