Stuck in an Array Problem

Hey guys, I'm going to start off by saying that I'm a total beginner noob, and that I'm posting here because I need help with this array program I have to make as my term project in college.

So, the concept of the program is to implement 3 arrays, one char and two double ones, to find the nearest unit, in which the char array is used to store the type of the building's first character (Hospital, University or Pharmacy) and find the closest unit in it's coordinate system (x and y coordinates, each in its respective double array), for 20 buildings (array size), by asking the user to store the types of buildings and their coordinates, followed by his own location. Finally, creating a menu displaying the the location of each building, asking the user which type of building he's looking for and using the Euclidean distance to find the nearest one.
I'm actually stuck in the user's input stage and I need help to know how to proceed with the program.

I read the posts here on the website on how to work with multi dimensional arrays but so far, this is all the code I have.

Thanks in advance and please excuse my beginner mistakes, I only have a couple of months worth of C++ programming knowledge.


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
 
#include <iostream>
#include <conio.h>
using namespace std;

int main() {
	const int numBuildings = 20, numX = 20, numY = 20;
	int x[20], y[20], i, j, k;
	char buildings[numX][numY];
	double x[numX], y[numY];


	for (i = 0; i < numBuildings; i++){
		cout << "Enter the type of building:" << endl;
		cin >> buildings[j];

		for (j = 0; j < numX; j++){
			cout << "Enter the" << j + 1 << ". X coordinate of building:" << endl;
			cin >> x[j];

			for (k = 0; k < numY; k++) {
				cout << "Enter the" << k + 1 << ". Y coordinate of building:" << endl;
				cin >> y[k];
			}
		}
		
	}







	return 0;
}
Just a few points.

In line 8 you have declared int x[20], y[20],
and in line 10 double x[numX], y[numY];.
You can't have two variables with the same name.

To store the characters for the building you need only a one-dimesional array: char buildings[numBuildings];

To input the data you only need one loop:

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
int main()
{
  const int numBuildings = 20;

  int x[20], y[20];
  char buildings[numBuildings];

  for (int i = 0; i < numBuildings; i++)
  {
    cout << "Enter the type of building: ";
    cin >> buildings[i];

    cout << endl << "Enter the" << i + 1 << ". X coordinate of building: ";
    cin >> x[i];

    cout << endl <<"Enter the" << i + 1 << ". Y coordinate of building: ";
    cin >> y[i];
  }
  // Just to test the input
  for (int i = 0; i < numBuildings; i++)
  {
    cout << "Building "<< i << "\tType: " << buildings[i] << "\tX: " << x[i] << "\tY: " << y[i] << endl;
  }
  system("pause");
}

Thank you so much for such a fast reply, I'll be sure to correct my code right now.
Any other tips would be much appreciated.
Okay so I edited my program, and I think I'm going the right way, this is what I have so far, I declared another function to calculate the Euclidean distance between the user and the buildings, but now I need to find the three nearest buildings of the user's choice. Please point me in the right direction if there's any problem. Thanks in advance !

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
#include <iostream>
#include<conio.h>
#include<math.h>

using namespace std;

const int numBuildings = 20;
double userX[1], userY[1];
double x[20], y[20];
char buildings[numBuildings];

double x1 = userX[1] - x[1]; //calculating number to square in next step
	double y1 = userY[1] - y[1];
	double dist;

double distanceCalculate()
{
    cout<<"Enter Your X Coordinate :"<<endl;
    cin>>userX[1];
    cout<<"Enter Your Y Coordinate :"<<endl;
    cin>>userY[1];

	dist = pow(x1, 2) + pow(y1, 2);       //calculating Euclidean distance
	dist = sqrt(dist);

	return dist;
}
int main()
{

	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Enter the type of building " << i + 1 << ":"<<endl;
		cin >> buildings[i];

		cout << endl << "Enter the building X coordinate";
		cin >> x[i];

		cout << endl << "Enter the Y coordinate of building: ";
		cin >> y[i];
	}

	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Building " << i + 1 << "\tType: " << buildings[i] << "\tX: " << x[i] << "\tY: " << y[i] << endl;
	}



	double dist;
	dist = distanceCalculate(userX, userY, x1, y1);    //initiate equation
	cout << "Distance Between (" << userX << " , " << userY << ") and (" << x1 << " , " << y1 << ") = " << dist;


 return 0;
}
Last edited on
It still doesn't compile.
 
dist = distanceCalculate(userX, userY, x1, y1);    //initiate equation 

error C2660: 'distanceCalculate' : function does not take 4 arguments line 51
So I edited it but it's still wrong and won't run. Could you please help me out by pointing out what I'm doing wrong here ? I'm trying to calculate the distance between the user inputted coordinates and the ones in the x and y arrays of the buildings.

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<conio.h>
#include<math.h>

using namespace std;

const int numBuildings = 20;
double userX, userY;
double x[20], y[20];
char buildings[numBuildings];
double dist;

double distanceCalculate(x, y)
{
   dist = pow(x1, 2) + pow(y1, 2);       //calculating Euclidean distance
	dist = sqrt(dist);

	return dist;
}

int main()
{

    cout<<"Enter Your X Coordinate :"<<endl;
    cin>>userX;
    cout<<"Enter Your Y Coordinate :"<<endl;
    cin>>userY;

	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Enter the type of building " << i + 1 << ":"<<endl;
		cin >> buildings[i];

		cout << endl << "Enter the building X coordinate";
		cin >> x[i];

		cout << endl << "Enter the Y coordinate of building: ";
		cin >> y[i];

}

	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Building " << i + 1 << "\tType: " << buildings[i] << "\tX: " << x[i] << "\tY: " << y[i] << endl;
	}


for (int i = 0; i < numBuildings; i++)
	{
		double dist;

	dist = distanceCalculate(userX, userY, x[i], y[i]);
    cout << "Distance Between (" << userX << " , " << userY << ") and (" << x1 << " , " << y1 << ") = " << dist;

	}


 return 0;
}

Line 14: double distanceCalculate(x, y)

Line 53: dist = distanceCalculate(userX, userY, x[i], y[i]);

If you need to pass 4parameters to the function you need to declare it accordingly.
Okay, so I managed to build up on my code with a couple of things I found online and this is what I have, although, its not giving me the right output for the buildings, what am I doing wrong ? Now, I'm completely clueless, I thought the code was well structured, but apparently, I did something wrong with the distance arrays. Here it goes:

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

#include <iostream>	
#include <cmath>
using namespace std;
int userX, userY;
double locX[20], locY[20];


int main()
{
	cout << "Enter Your X coordinate: " << endl;
	cin >> userX;
	cout << "Enter Your Y coordinate: " << endl;
	cin >> userY;

	const int numBuildings = 20;
	int x[20], y[20];
	char buildings[numBuildings];

	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Enter the type of building (U, P or H): ";
		cin >> buildings[i];

		cout << endl << "Enter the" << i + 1 << ". X coordinate of building: ";
		cin >> x[i];

		cout << endl << "Enter the" << i + 1 << ". Y coordinate of building: ";
		cin >> y[i];
	}


	for (int i = 0; i < numBuildings; i++)
	{
		cout << "Building " << i << "\tType: " << buildings[i] << "\tX: " << x[i] << "\tY: " << y[i] << endl;
	}

	double distArray[20];

	for (int i = 0; i<20; i++) {
		//Distance Algorithm
		distArray[i] = pow((pow(locX[i] - userX, 2) + pow(locY[i] - userY, 2)), 0.5);
	}


	//Nearest 3 Buildings
	cout << "The nearest three buildings are: " << endl;
	switch (buildings[0]) {
	case 'U': cout << "University"; break;
	case 'H': cout << "Hospital"; break;
	case 'P': cout << "Pharmacy"; break;
	}
	cout << " at coordinates (" << locX[0] << ", " << locY[0] << ")\n Distance: " << distArray[0] << endl;
	cout << "-The ";
	switch (buildings[1]) {
	case 'U': cout << "university"; break;
	case 'H': cout << "hospital"; break;
	case 'P': cout << "pharmacy"; break;
	}
	cout << " at coordinates (" << locX[1] << ", " << locY[1] << ")\n Distance: " << distArray[1] << endl;
	cout << " The ";
	switch (buildings[2]) {
	case 'U': cout << "university"; break;
	case 'H': cout << "hospital"; break;
	case 'P': cout << "pharmacy"; break;
	}
	cout << " at coordinates (" << locX[2] << ", " << locY[2] << ")\n Distance: " << distArray[2] << endl;


	//Find Nearest Buildings
	for (int i = 0; i<20; i++) {
		if (buildings[i] == 'U') {
			cout << "The nearest university is at X: " << locX[i] << " And Y : " << locY[i] << " With Distance Equal to : " << distArray[i] << endl;
			break;
		}
	}
	for (int i = 0; i<20; i++) {
		if (buildings[i] == 'H') {
			cout << "The nearest hospital is at X: " << locX[i] << " And Y : " << locY[i] << " With Distance Equal to :  " << distArray[i] << endl;
			break;
		}
	}
	for (int i = 0; i<20; i++) {
		if (buildings[i] == 'P') {
			cout << "The nearest pharmacy is at X: " << locX[i] << " And Y: " << locY[i] << " With Distance Equal to : " << distArray[i] << endl;
			break;
		}
	}



	return 0;
}
You could print the distance array to see if it contains the correct values.
I implement one version using the standard containers...
see below...

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
#include<iostream>
#include<map>
#include<cmath>

using namespace std;

pair<double,double> findNearest(multimap<char,pair<double,double>>,char,double,double);

int main()
{
    multimap<char,pair<double,double>> buildings;
    cout<<"Please input 20 buildings(type h or u or p) and their coordinates: "<<endl;
    char c;
    double x,y;
    int i = 20;
    while(cin>>c>>x>>y && i--)
    {
        buildings.insert({c,{x,y}});
    }
    cout<<"the 20 buildings and their coordinates are listed below:"<<endl;
    for(auto iter = buildings.begin(); iter !=buildings.end(); ++iter)
    {
        switch(iter->first)
        {
        case 'h':
            cout<<"Building Type: Hospital  \t"<<"Location: x="<<iter->second.first<<" y="<<iter->second.second<<endl;
            break;
        case 'u':
            cout<<"Building Type: University\t"<<"Location: x="<<iter->second.first<<" y="<<iter->second.second<<endl;
            break;
        case 'p':
            cout<<"Building Type: Pharmacy  \t"<<"Location: x="<<iter->second.first<<" y="<<iter->second.second<<endl;
            break;
        default:
            break;
        }
    }
    while(true)
    {
        cout<<"Please input the building type you want to search and your location:"<<endl;
        cin>>c>>x>>y;
        auto result=findNearest(buildings,c,x,y);
        switch(c)
        {
        case 'h':
            cout<<"The nearest hospital is at Location: x="<<result.first<<"  y="<<result.second<<endl;
            break;
        case 'u':
            cout<<"The nearest university is at Location: x="<<result.first<<"  y="<<result.second<<endl;
            break;
        case 'p':
            cout<<"The nearest pharmacy is at Location: x="<<result.first<<"  y="<<result.second<<endl;
            break;
        default:
            cout<<"Input Building Type Error!"<<endl;
        }
        cout<<"search again? (y for yes n for no):"<<endl;
        string s;
        cin>>s;
        if(s[0] == 'n')
            break;
    }
}

pair<double,double> findNearest(multimap<char,pair<double,double>> buildings,char c,double x,double y)
{
    auto lowerIter = buildings.lower_bound(c);
    auto upperIter = buildings.upper_bound(c);
    auto ret = lowerIter->second;
    double nearestDistance = pow(ret.first-x,2)+pow(ret.second-y,2);
    while(++lowerIter != upperIter)
    {
        double distance = pow(lowerIter->second.first-x,2)+pow(lowerIter->second.second-y,2);
        if(distance<nearestDistance)
        {
            nearestDistance=distance;
            ret = lowerIter->second;
        }
    }
    return ret;
}
Thank You guys for all the support, I managed to create the program, and still got to learn a lot from all your answers ! Means a lot !

For those interested, here goes the code :)

P.S.
I did it with just one function, the main one, instead of creating one for the distance calculation as well, figured I could just do it and store the values in an array.

Thanks once again.

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	char T[20];
	double X[20], Y[20], dist[20], temp;
	int i;
	double userX, userY;


	cout << "Welcome to the nearest location helper! " << endl;
	cout << "Note that the following letters represents the corresponding location type " << endl;
	cout << "H: Hospital    U : University      P : Pharmacy" << endl;
	cout << "Now we need to get information about your location" << endl;
	cout << "Please enter your location X coordinate" << endl;
	cin >> userX;
	cout << "Now your location Y coordinate" << endl;
	cin >> userY;

	for (i = 0; i < 20; i++)
	{
		cout << "Please enter the letter of the location number[" << i << "] = " << endl;
		cin >> T[i];
		cout << "Now you entered " << T[i] << "Please enter its coordinates X then Y" << endl;
		cout << "X coordinates" << endl;
		cin >> X[i];
		cout << "Y coordinates" << endl;
		cin >> Y[i];
		dist[i] = sqrt(pow(userX - X[i], 2) + pow(userY - Y[i], 2));
	}



	for (int i = 0; i < 20; i++)
	{
		cout << "Building " << i << "\tType: " << T[i] << "\tX: " << X[i] << "\tY: " << Y[i] << endl;
	}

	// Bubble Sort
	for(int u=0;u<20;u++){
        for(int j=0;j<19;j++){
            if(dist[j]>dist[j+1]){
                temp=dist[j];
                dist[j]=dist[j+1];
                dist[j+1]=temp;

                temp=X[j];
                X[j]=X[j+1];
                X[j+1]=temp;

                temp=Y[j];
                Y[j]=Y[j+1];
                Y[j+1]=temp;

                temp=T[j];
                T[j]=T[j+1];
                T[j+1]=temp;
            }
        }
    }

    //Prints out closest building which is the first position in each array.
    cout << "The Closest Building ";
    if(T[0] == 'U')
        cout << "university at coordinates (" << X[0] << "," << Y[0] << ")";
    if(T[0] == 'H')
        cout << "hospital at coordinates (" << X[0] << ", " << Y[0] << ")";
    if(T[0] == 'P')
        cout << "pharmacy at coordinates (" << X[0] << ", " << Y[0] << ")";

    cout << " The distance is" "  " << dist[0] << " km" << endl;

//Nearest three buildings
    cout << "Nearest three buildings: " << endl;

    switch (T[0]){
        case 'U': cout << "university";
        break;
        case 'H': cout << "hospital";
        break;
        case 'P': cout << "pharmacy";
        break;
    }

    cout << " at coordinates (" << X[0] << ", " << Y[0] << ") Distance: " << dist[0] << "km"<<endl;


    switch (T[1]){
        case 'U': cout << "university";
        break;
        case 'H': cout << "hospital";
        break;
        case 'P': cout << "pharmacy";
        break;
    }

    cout << " at coordinates (" << X[1] << ", " << Y[1] << ") Distance: " << dist[1] << "km"<<endl;

    switch (T[2])
    {
        case 'U': cout << "university"; break;
        case 'H': cout << "hospital"; break;
        case 'P': cout << "pharmacy"; break;
    }
    cout << " at coordinates (" << X[2] << ", " << Y[2] << ") Distance: " << dist[2]<< "km"<<endl;


    //find nearest of each of the buildings
    for(int a=0;a<20;a++)
        {
        if(T[a]=='U')
        {
            cout << "The nearest university is the one at coordinates (" << X[a] << ", " << Y[a] << ") which is " << dist[a] << "km away." << endl;
            break;
        }

    }
    for(int b=0;b<20;b++)
        {
        if(T[b]=='H')
        {
            cout << "The nearest hospital is the one at coordinates (" << X[b] << ", " << Y[b] << ") which is " << dist[b] << "km away." << endl;
            break;
        }

    }
    for(int c=0;c<20;c++)
        {
        if(T[c]=='P')
        {
            cout << "The nearest pharmacy is the one at coordinates (" << X[c] << ", " << Y[c] << ") which is " << dist[c] << "km away.\n" << endl;
            break;
        }

    }


	return 0;
}
Topic archived. No new replies allowed.