Min and max values?

Hello guys, I have a little problem with assigning the min and max values.The following code, should input several points and after that should find the min and max distance between two points, and also to calculate the max distance in a triangle.I am almost sure that the problem is in the assigning of the min and max values, because it is compiling but gives errors in the calculations.

So here is the code.I will appreciate any help.
Thanks :).
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
96
97
98
99
100
101

#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#define  PI  3.14159265358979323846        
using namespace std;


// Inputs data from a given file into a Two-dimensional array
void read_data(ifstream& in, double point[][2], int table_rows)
{
        int i,j;
        for(i = 0; i < table_rows; i++)
        {
                for(j = 0; j < 2; j++)
                {
                        in >> point[i][j];
                }
        }
}

// Finds minimal and maximal distance between 2 points
void min_max(const double point[][2], int table_rows)
{
    int max=std::numeric_limits<int>::min();
     int min=std::numeric_limits<int>::max();
     
       //double smal = 10000;
        //double large = 0;
        int i,j;
        double dist;
        for(i = 0; i < table_rows-1; i++)
        {
                for(j = i+1; j < table_rows; j++)
                {       
                        dist = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
                        if (dist < min) dist=min;
                        if (dist > max) dist=max;
/If I change it to min=dist, max=dist
gives and error-[warning] assignment
                [warning] argument  
                }
        }
        cout << "The minimum distance between two points is: " << min << endl;
        cout << "The maximum distance between two points is: " << max << endl;
}

// Finds the biggest triagle area
void T_area(const double point[][2], int table_rows)
{int max=std::numeric_limits<int>::min();
        double largest = 0;
        int i,j,k;
        double area,distAB,distBC,distCA,s;
        for(i = 0; i < table_rows-2; i++)
        {
                for(j = i+1; j < table_rows-1; j++)
                {       
                        for(k = i+2; k < table_rows; k++)
                        {
                                distAB = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
                                distBC = sqrt(pow((point[k][0] - point[j][0]), 2) + pow((point[k][1] - point[j][1]), 2));
                                distCA = sqrt(pow((point[i][0] - point[k][0]), 2) + pow((point[i][1] - point[k][1]), 2));
                                //@param s is the semiparameter of the triangle
                                s = (distAB + distBC + distCA)/2;
                                area = sqrt(s * (s - distAB) * (s - distBC) * (s - distCA));
                                if(area > max) max=area;
                        }
                }
        }
        cout << "The largest area of a triangle is: " << max << endl;
}


int main()
{      ifstream infile; 
       
        cout << "Enter the input file name: ";
        char filename[1024];
         cin.getline(filename, 1024, '\n');
         
        
        if (infile.fail())
        {
                cout << "Error opening " << filename << endl;
                return 1;            
        }
        const int POINT_ROWS = 50;
        const int POINT_COLS = 2;
        double point[POINT_ROWS][POINT_COLS];
        const int x = 50;
        const int y = 2;        
        double h_point[x][y];
        int count = 0;
        read_data(infile, point, POINT_ROWS);
        min_max(point, POINT_ROWS);
        T_area(point, POINT_ROWS);
        infile.close();
       system ("pause");
         return 0;
}
Last edited on
if (dist < min) dist=min;

So you're calculating the dist, and if the dist is less than your current min value, you've found a new minimum. So far so good. You then take the old minimum value, and store that in dist. Shouldn't you be storing the new dist value in min?

i.e. if (dist < min) min=dist;

Likewise for max.

The comment in your code makes it clear that you actually know this already, but you changed it to something incorrect because you couldn't solve the warning. Make it right and solve the warning.

I note that dist is a double value, and min and max are int type. If you need to find the min and max distance, min and max will have to be double as well (I presume you don't want to lose data by rounding the double value to an int).
Last edited on
Yes, I know that it should be if (dist < min) min=dist; , but the problem is that it gives errors (assignment, argument..), which I dont know how to fix..
If you tell us what the error is, we can help you fix it.

It is somewhat frowned upon in programming circles to deliberately write code that you know gives the wrong answer. The fact that it compiles does not make up for it :)
The errors are:
37-[Warning] assignment
37-[Warning] argument
38-[Warning] assignment
38-[Warning] argument
67-[Warning] assignment
67-[Warning] argument

37,38,63 are the lines where I use min=dist and max=dist.
I am using Dev-C++ 4.9.8.0.
Last edited on
The following code compiles without error. I made four changes; fixed the setting of min and max, added the missing header <limits>, and removed the call to system("pause") at the end.

You are almost certainly using a very old compiler that is known to have bugs and non-standard behaviour. Please read this: http://www.cplusplus.com/forum/articles/36896/

You will help yourself a great deal by getting a new compiler (and indeed, abandoning Dev-C++ completely and moving on to something better).

In the code below, there is still the issue that you are calculating dist as a double, but it will be rounded when you store the value to min or max.

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
96
97
98
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <limits>
#define  PI  3.14159265358979323846        
using namespace std;


// Inputs data from a given file into a Two-dimensional array
void read_data(ifstream& in, double point[][2], int table_rows)
{
        int i,j;
        for(i = 0; i < table_rows; i++)
        {
                for(j = 0; j < 2; j++)
                {
                        in >> point[i][j];
                }
        }
}

// Finds minimal and maximal distance between 2 points
void min_max(const double point[][2], int table_rows)
{
    int max=std::numeric_limits<int>::min();
     int min=std::numeric_limits<int>::max();
     
       //double smal = 10000;
        //double large = 0;
        int i,j;
        double dist;
        for(i = 0; i < table_rows-1; i++)
        {
                for(j = i+1; j < table_rows; j++)
                {       
                        dist = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
                  if (dist < min) min=dist;
                        if (dist > max) max=dist;

                }
        }
        cout << "The minimum distance between two points is: " << min << endl;
        cout << "The maximum distance between two points is: " << max << endl;
}

// Finds the biggest triagle area
void T_area(const double point[][2], int table_rows)
{int max=std::numeric_limits<int>::min();
        double largest = 0;
        int i,j,k;
        double area,distAB,distBC,distCA,s;
        for(i = 0; i < table_rows-2; i++)
        {
                for(j = i+1; j < table_rows-1; j++)
                {       
                        for(k = i+2; k < table_rows; k++)
                        {
                                distAB = sqrt(pow((point[j][0] - point[i][0]), 2) + pow((point[j][1] - point[i][1]), 2));
                                distBC = sqrt(pow((point[k][0] - point[j][0]), 2) + pow((point[k][1] - point[j][1]), 2));
                                distCA = sqrt(pow((point[i][0] - point[k][0]), 2) + pow((point[i][1] - point[k][1]), 2));
                                //@param s is the semiparameter of the triangle
                                s = (distAB + distBC + distCA)/2;
                                area = sqrt(s * (s - distAB) * (s - distBC) * (s - distCA));
                                if(area > max) max=area;
                        }
                }
        }
        cout << "The largest area of a triangle is: " << max << endl;
}


int main()
{      ifstream infile; 
       
        cout << "Enter the input file name: ";
        char filename[1024];
         cin.getline(filename, 1024, '\n');
         
        
        if (infile.fail())
        {
                cout << "Error opening " << filename << endl;
                return 1;            
        }
        const int POINT_ROWS = 50;
        const int POINT_COLS = 2;
        double point[POINT_ROWS][POINT_COLS];
        const int x = 50;
        const int y = 2;        
        double h_point[x][y];
        int count = 0;
        read_data(infile, point, POINT_ROWS);
        min_max(point, POINT_ROWS);
        T_area(point, POINT_ROWS);
        infile.close();
        return 0;
}


I do still get warnings. Note that a warning is not the same as an error. What you seem to be getting are not errors. They are warnings. A warning is something that does not stop compiling - the code may run absolutely fine. An error stops compilation.

These are the warnings I get:

1
2
3
4
5
6
7
8
9
38.cpp: In function ‘void min_max(const double (*)[2], int)’:
38.cpp:38: warning: converting to ‘int’ from ‘double’
38.cpp:39: warning: converting to ‘int’ from ‘double’
38.cpp: In function ‘void T_area(const double (*)[2], int)’:
38.cpp:65: warning: converting to ‘int’ from ‘double’
38.cpp:50: warning: unused variable ‘largest’
38.cpp: In function ‘int main()’:
38.cpp:91: warning: unused variable ‘h_point’
38.cpp:92: warning: unused variable ‘count’


The first three warnings are converting from double to int; we've already discussed that.
The other three warnings are informing you that you're making variables and not using them. There's no harm done, but maybe you meant to use them for something and forgot, or you don't need them anymore and should remove them.
Last edited on
Thanks a lot.But I dont understand you-"In the code below, there is still the issue that you are calculating dist as a double, but it will be rounded when you store the value to min or max."
I know that the warnings shouldnt stop the compilation, but in fact it doesnt compile.
What should I do now?
Firstly, stop using Dev-C++ and get something else. That page gives a list of possibilities.

But I dont understand you-"In the code below, there is still the issue that you are calculating dist as a double, but it will be rounded when you store the value to min or max.

You are calculating dist, which is a double value. Double values can hold numbers that are not integers. Numbers such as 1.76, or 3.14159, or 864768.56486

min and max are int type. They can only hold integers, like 6, or 7654, or -3521.

What happens when you take a double, such as 3.14159, and try to store that value in an int? It can't be stored. The int could hold the value 4, or the value 3, but not 3.14159; you will lose some of the data. The warning is telling you about this; that you are trying to store some numerical data in a variable that simply cannot store it.


If you insist on using int values for min and max, do the conversion from double to int explicitly with a proper C++ cast:

1
2
if (dist < min) min = static_cast<int>(dist);
if (dist > max) max = static_cast<int>(dist);


Remove those unused variables (largest, h_point, count).

Last edited on
Mmm nah, it still generates wrong outputs..I dont need min and max to be int, so it can be double.
I changed it to
1
2
double max=std::numeric_limits<int>::min();
     double min=std::numeric_limits<int>::max(); 

and if (dist < min) min = dist; whithout casting because they are now floats, and they should be floats.
P.s. The error in this code still persists.May be its not only the assignments for min and max :? :S
Last edited on
Topic archived. No new replies allowed.