help with a simple logic coding, please help

So I am making a program that generates 3 random locations of ambulances on a 9x9 board, then you choose the coordinates to an accident. Now I have to know the nearest location comparing the three arrays of the ambulances with the array of the accident. Can you help me with developing this method? I thought about comparing or subtracting the three arrays individually with the array of the accident so the one with the smallest result is the nearest? its a homework assignment and I just couldnt get to the result, my world to you if you help me. thank you

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
#include <iostream>
    #include <windows.h>
    #include <string>
    #include <cmath>
    #include <stdio.h>
    #include <limits>
    #include <time.h>
    #include <iomanip>

    using namespace std;
void table ()
{
     int ambulancias=0, X, Y;
        int boardm[9][9] = {0}; // Set whole array to zeroes
        srand((unsigned)time(0));
         for(X=0; X<9; X++){
    for( Y=0; Y<9; Y++)
                {
                    cout << boardm[X][Y] << " "; //
                }
                cout << endl;
                    }}
    void thecall(){
        cout << "Emergency call, type enter to follow instructions" << endl;
    Beep(523,500);
    cin.get();
    }


    int main()
    {

int x1;
int y1;
        int ambulancias=0, X, Y;
        int boardm[9][9] = {0};
        srand((unsigned)time(0));


{
  system("cls");
       thecall();
        Sleep(1000);
}


table();
cout<<"enter coordinates of the accident"<<endl;
cout<<"enter x:"<<endl;
cin>>x1;
cout<<"enter y"<<endl;
cin>>y1;

system("cls");


system("pause");
        do
        {
boardm[x1][y1] = 2;

            do
            {
                X = rand()%9; // random row
                Y = rand()%9; // random column
            }while (boardm[X][Y] !=0 ); // do again if event is located there already
            boardm[X][Y] = 1; // place ambulance at location
            ambulancias++; // increase count
        } while (ambulancias < 3); 

        for(X=0; X<9; X++)
        {
                for( Y=0; Y<9; Y++)
                {
                    cout << boardm[X][Y] << " ";
                }
                cout << endl; // 
        }
        cout << "There are "  << ambulancias << " ambulances\n\n";  

     
        cout << "Which ambulance is closer to the accident?";
         }
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
102
103
104
105
106
107
108
109
110
111
112

#include <iostream>
    #include <windows.h>
    #include <string>
    #include <cmath>
    #include <stdio.h>

    #include <limits>
    #include <time.h>
    #include <iomanip>

    using namespace std;
void tablero ()
{
     int ambulancias=0, X, Y;
        int boardm[9][9] = {0}; // Set whole array to zeroes
        srand((unsigned)time(0));
         for(X=0; X<9; X++){
    for( Y=0; Y<9; Y++)
                {
                    cout << boardm[X][Y] << " "; // Print out board
                }
                cout << endl; // After print a row, do a new line, and start a new row}
                    }}
    void llamada(){
        cout << "Llamada entrante de emergencia.." << endl;
    Beep(523,500);
    cin.get();
    }


    int main()
    {
int x1;
int y1;
int X2,Y2,X3,Y3;

        int ambulancias=0, X, Y;
        int XY;

        int boardm[9][9] = {0}; // Set whole array to zeroes
        srand((unsigned)time(0));


        //cout << "Ubicando ambulancias aleatoriamente.." << endl;
       // Sleep(5000);
        //system("cls");
       // llamada();
        //Sleep(1000);

tablero();
cout<<"Ingrese las coordenadas del accidente"<<endl;
cout<<"ingrese x"<<endl;
cin>>x1;
cout<<"ingrese y"<<endl;
cin>>y1;

system("cls");


system("pause");


            do
            {

                X = rand()%9;
                Y = rand()%9;
                X2 = rand()%9;
                Y2 = rand()%9;
                 X3 = rand()%9;
                Y3= rand()%9;
            }while (boardm[x1][y1] !=0 );
            boardm[x1][y1] = 1;
            boardm[X][Y] = 2;
            boardm[X2][Y2] = 2;
            boardm[X3][Y3] = 2;

        for(x1=0; x1<9; x1++)
        {
                for( y1=0; y1<9; y1++)
                {
                    cout << boardm[x1][y1] << " ";
                }
                cout << endl;
        }

if (X-x1<0)
X-x1==X-x1*-1;
if (X2-x1<0)
{X2-x1==X2-x1*-1;}
if (X3-x1<0)
{X3-x1==X3-x1*-1;}

        if((X-x1<=X2)&&(X-x1<=X3)){
cout<<"La coordenada de la ambulancia mas cercana es: "<<X<<","<<Y;}
else if ((Y-y1<Y2)&& (Y-y1<Y3)){
cout<<"La coordenada de la ambulancia mas cercana es: "<<X<<","<<Y;}
 else if((X2-x1<=X3)&&(X2-x1<=X))
 {cout<<"La coordenada de la ambulancia mas cercana es: "<<X2<<","<<Y2;}
 else if ((Y2-y1<Y)&&(Y2-y1<Y3))
{cout<<"La coordenada de la ambulancia mas cercana es: "<<X2<<","<<Y2;}
 else if((X3-x1<=X2)&&(X3-x1<=X))
{cout<<"La coordenada de la ambulancia mas cercana es: "<<X3<<","<<Y3;}
else if((Y3-y1<Y2)&&(Y3-y1<Y))
{cout<<"La coordenada de la ambulancia mas cercana es: "<<X3<<","<<Y3;}




    return 0;
}

can anyone fix the logic? it doesn't give the result of the nearest coordinate
Last edited on
Please use code tags to make your code readable, like you did in your OP.
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
#include <iostream>
#include <cmath>

int taxicab_distance( int x1, int y1, int x2, int y2 )
{ return std::abs(x1-x2) + std::abs(y1-y2) ; }

int main ()
{
    int a1x = 5, a1y = 7 ; // ambulance one
    std::cout << "ambulance one is at: (" << a1x << ',' << a1y << ")\n" ;

    int a2x = 2, a2y = 3 ; // ambulance two
    std::cout << "ambulance two is at: (" << a2x << ',' << a2y << ")\n" ;

    int ptx = 4, pty = 1 ; // accident
    std::cout << "accident is at: (" << ptx << ',' << pty << ")\n" ;

    int a1_to_pt = taxicab_distance( a1x, a1y, ptx, pty ) ;
    std::cout << "distance from ambulace one: " << a1_to_pt << '\n' ;

    int a2_to_pt = taxicab_distance( a2x, a2y, ptx, pty ) ;
    std::cout << "distance from ambulace two: " << a2_to_pt << '\n' ;

    if( a1_to_pt < a2_to_pt ) std::cout << "ambulance one is nearer\n" ;
    else if( a1_to_pt > a2_to_pt ) std::cout << "ambulance two is nearer\n" ;
    else std::cout << "both are equally near\n" ;
}









ambulance one is at: (5,7)


ambulance two is at: (2,3)


accident is at: (4,1)


distance from ambulace one: 7


distance from ambulace two: 4


ambulance two is nearer
Topic archived. No new replies allowed.