using Char Array

Hİ there i am a beginner and i have a c++ project there is one part of it that i dont understand it says "you need to create one Char and 2 double Arrays to keep the type and location as X and Y coordinates of the units " ...the program is all about storing 20 different buildings e.g hospitals , unversities, and pharmacy and storing their respective locations and asking the user to enter their location and the display which hospital or pharmacy or uni is the nearest i dont have an idea of the part i mentioned above could someone give me an example even if it is a rough one i would really appriciate

thanks

If I read the question correctly, you need to store the location and type of a building in double arrays and char arrays. The logical approach would be to use the two double arrays for the coordinates and the char array for the type.

An example:

1
2
3
double xlocations[20]; //Array of twenty x-coordinates
double ylocations[20]; //Array of twenty y-coordinates
char types[20]; //Array of twenty types 


The first thing you need to do is fill the arrays somehow, this shouldn't be too much of a problem. Using a simple for loop you should be able to figure this out yourself. Next thing to do would be to store the users location:

 
double userx, double usery;


Fill them from standard input like you filled up the locations, this shouldn't be a problem.

Next we need a formula for the distance. We're working in a 2D grid, so we can calculate the distance between two points using simple Pythagoras. The general formula is:

1
2
double distance = std::sqrt(std::pow(xlocation[i]-userx,2) 
        + std::pow(ylocation[i]-usery,2));


This can be called in a for loop. If I read your question correctly, there are only 3 types of buildings: hospital, university and pharmacy. We'll each give these a number: hospital = 0, university = 1 and pharmacy = 2. We can then use a switch to determine the type:

1
2
3
4
5
6
7
8
9
10
11
12
switch(types[i])
{
    case 0:
        if(distance < hospitaldistance)
        {
            hospitaldistance = distance;
            hospitalx = xlocations[i];
            hospitaly = ylocations[i];
       }
       break;
   //Etc for the other types
}


Then after the for loop, we should print the found result. I think you should now have an idea of how to implement such a program. Good luck with it.
Last edited on
Thank you so mUch you saved me a big time am so greatful :D
okey after doing what you have told me to do i came up with this nd got stuck here again like how should the program know what letter is assigned to what building


#include <iostream>
using namespace std;

int main(){

char buildingtypes[20];

double xlocations[20];

double ylocations[20];

cout<<"please enter the first letter of the building"<<endl;
cin>>buildingtypes[0];


cout<<"Please enter X coordinate location"<<endl;

cin>>xlocations[0];

cout<<"Please enter Y coordinate location"<<endl;

cin>>ylocations[0];

double userx, usery;

cout<<"Please enter your X coordinate location"<<endl;
cin>>userx;

cout<<"Please enter your Y coordinate location"<<endl;
cin>>usery;






the user enters the location and then we do the pythogream formula as u said i understand all that so up there when the user enters the building name how is the program going to find the building and what coordinate it belongs to a little help please i really need to finish this :((
I *think* they mean this:
1
2
3
const unsigned NUM_BUILDINGS = 20;
double x[NUM_BUILDINGS], y[NUM_BUILDINGS];
char type[NUM_BUILDINGS];

Whether or not you give the number 20 a fancy name, you should fill up all 20 buildings. Use a loop to fill them all:

1
2
3
4
5
6
for(int i = 0; i < 20; ++i)
{
    std::cin >> types[i];
    std::cin >> xlocation[i];
    std::cin >> ylocation[i];
}


That's how you fill it up (you could show the user some message telling them what to input). Next you ask for the users location, after that you calculate the distance for every building, and store the least value.
Topic archived. No new replies allowed.