nan problem

hello beautiful people
first thanks for your time
second i know this code is a huge mess but the idea behind it is:
the program generate a random x and y location for sara then generate 3 other random locations, after that it take them to calDistance function and get the distance between each supermarket and sara then to evaluate function to compare and get the least distance, the problem is i get nan as a distance quit often, not all the time though, i looked it up and initialized everything but i still get nan as a distance.
btw i have to keep separate functions.

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

double sarax = rand()% (RAND_MAX - 0+1), saray = rand()% (RAND_MAX - 0+1);
double calDistance (double , double , double, double);
double evaluate(double, double, double);
int main()
{
	srand(time(0));
	double x1 = rand()% (RAND_MAX - 0+1), y1 = rand()% (RAND_MAX - 0+1), x2=rand()% (RAND_MAX - 0+1), y2 = rand()% (RAND_MAX - 0+1), x3 = rand()% (RAND_MAX - 0+1), y3 = rand()% (RAND_MAX - 0+1);
	double  distance1=0, distance2=0, distance3=0;
	distance1 = calDistance(sarax, saray, x1, y1);
	distance2 = calDistance(sarax, saray, x2, y2);
	distance3 = calDistance(sarax, saray, x3, y3);
double	close = evaluate(distance1, distance2, distance3);
	cout<<distance1<<endl;
	cout<<distance2<<endl;
	cout<<distance3<<endl;
	cout<<"the closest supermarket is "<<close<<"m away"<<endl;

	return 0;
}


double calDistance(double x1,double y1,double x2,double y2)
{
  double  x = pow(x2,2)-pow(x1,2);
  double  y = pow(y2,2)-pow(y1,2); 
  double  answer = sqrt(x) + sqrt(y);
  
  return answer;
}


double evaluate(double dis1, double dis2, double dis3)
{

	double close = dis1;
	if (dis2 < dis1)
	{
		close = dis2;
    }
	if (dis3 < dis1)
	{
		    close = dis3;
	}
	
return close;
}
> second i know this code is a huge mess but the idea behind it is:
So clean it up.
It's a 5-second job with indent -kr -nut -ts2 -i2 -l80 -cli0 proj.cpp
There's no point making everyone suffer because you can't be bothered to clean up the code a bit.

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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

double sarax = rand() % (RAND_MAX - 0 + 1), saray = rand() % (RAND_MAX - 0 + 1);
double calDistance(double, double, double, double);
double evaluate(double, double, double);

int main()
{
  srand(time(0));
  double x1 = rand() % (RAND_MAX - 0 + 1),
      y1 = rand() % (RAND_MAX - 0 + 1),
      x2 = rand() % (RAND_MAX - 0 + 1),
      y2 = rand() % (RAND_MAX - 0 + 1),
      x3 = rand() % (RAND_MAX - 0 + 1),
      y3 = rand() % (RAND_MAX - 0 + 1);
  double distance1 = 0, distance2 = 0, distance3 = 0;
  distance1 = calDistance(sarax, saray, x1, y1);
  distance2 = calDistance(sarax, saray, x2, y2);
  distance3 = calDistance(sarax, saray, x3, y3);
  double close = evaluate(distance1, distance2, distance3);
  cout << distance1 << endl;
  cout << distance2 << endl;
  cout << distance3 << endl;
  cout << "the closest supermarket is " << close << "m away" << endl;

  return 0;
}

double calDistance(double x1, double y1, double x2, double y2)
{
  double x = pow(x2, 2) - pow(x1, 2);
  double y = pow(y2, 2) - pow(y1, 2);
  double answer = sqrt(x) + sqrt(y);
  return answer;
}

double evaluate(double dis1, double dis2, double dis3)
{
  double close = dis1;
  if (dis2 < dis1) {
    close = dis2;
  }
  if (dis3 < dis1) {
    close = dis3;
  }

  return close;
}


Next, compile with some warnings enabled.
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
$ g++ -Wall -Wextra proj.cpp
proj.cpp:7:39: warning: integer overflow in expression [-Woverflow]
 double sarax = rand() % (RAND_MAX - 0 + 1), saray = rand() % (RAND_MAX - 0 + 1);
                                       ^
proj.cpp:7:76: warning: integer overflow in expression [-Woverflow]
 double sarax = rand() % (RAND_MAX - 0 + 1), saray = rand() % (RAND_MAX - 0 + 1);
                                                                            ^
proj.cpp: In function ‘int main()’:
proj.cpp:14:38: warning: integer overflow in expression [-Woverflow]
   double x1 = rand() % (RAND_MAX - 0 + 1),
                                      ^
proj.cpp:15:35: warning: integer overflow in expression [-Woverflow]
       y1 = rand() % (RAND_MAX - 0 + 1),
                                   ^
proj.cpp:16:35: warning: integer overflow in expression [-Woverflow]
       x2 = rand() % (RAND_MAX - 0 + 1),
                                   ^
proj.cpp:17:35: warning: integer overflow in expression [-Woverflow]
       y2 = rand() % (RAND_MAX - 0 + 1),
                                   ^
proj.cpp:18:35: warning: integer overflow in expression [-Woverflow]
       x3 = rand() % (RAND_MAX - 0 + 1),
                                   ^
proj.cpp:19:35: warning: integer overflow in expression [-Woverflow]
       y3 = rand() % (RAND_MAX - 0 + 1);
                                   ^

Start by fixing those.
hey salem thank you so much for all the information and am very sorry for the suffering i caused i really didn't know about the style tool xd

i will be sure to make the code into a good format before submitting it in the future. i enabled all the warnings in dev c++ yet i didn't get these warnings however it helped me alot to solve the problem, kinda
this is what i finally came up with after 4 hours of bashing my head to the keyboard xd
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
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
double sarax = rand() % (5-0+1), saray = rand() % (5-0+1);
double calDistance(double, double, double, double);
double evaluate(double, double, double);

int main()
{
  srand(time(0));
    double x1 = rand() ,
      y1 = rand() ,
      x2 = rand() ,
      y2 = rand() ,
      x3 = rand() ,
      y3 = rand() ;
  double distance1 = 0, distance2 = 0, distance3 = 0;
  distance1 = calDistance(sarax, saray, x1, y1) ;
  distance2 = calDistance(sarax, saray, x2, y2) ;
  distance3 = calDistance(sarax, saray, x3, y3) ;
  double close = evaluate(distance1, distance2, distance3);
  cout << distance1 << endl;
  cout << distance2 << endl;
  cout << distance3 << endl;
  cout << "the closest supermarket is " << close << "m away" << endl;

  return 0;
}

 double calDistance(double x1, double y1, double x2, double y2)
{
  double x = pow(x2, 2) - pow(x1, 2);
  double y = pow(y2, 2) - pow(y1, 2);
  double answer = sqrt(x) + sqrt(y);
  return answer;
}

double evaluate(double dis1, double dis2, double dis3)
{
  double close = dis1;
  if (dis2 < dis1) 
  {
    close = dis2;
    if(dis3<dis2)
    {
    close = dis3;	
	}
  }
  if(dis3<dis2)
  {
	close = dis3;
	{
		if(dis2<dis3)
		{
			close = dis2;
		}
	}
  }
  return close;
}
Topic archived. No new replies allowed.