#Coordinates distance and more

1 messages · Page 1 of 1 (latest)

lapis jay
#

Hello, I need help with task, I already did to calculate distance, but IDK how to output the biggest coordination distance with numbers, and then sort all coordinates.

stiff sorrelBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

lapis jay
#
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

int main ()
{
    int n;

    fstream duom("duom.txt");
    fstream rez("rez.txt");

    duom >> n;

    int x[n], y[n];
    double distance[n];

   for (int i=1; i<=n; i++){

    duom >> x[i];
    duom >> y[i];

    distance[i]=sqrt(pow(0-x[i],2)+pow(0-y[i],2));

    cout << distance[i] << endl;
    }

    return 0;
}
#

this what I did, but I need to do how task asks but IDK how to get that the answer

#

idk how to do this

ripe relic
#

you don't actually have to sort all of the values bec9ause you only need to sort the highest so far value

#

so if you would have 3 points (in this order) with distance 1,4,2
then you would first store the distance of 1 (because it's the highest so far) and when the 4 comes you compare it to that. because 4 is greater than 1 you store it. then this becomes the new highest so far value. You would keep the value 4 after comparing it to 2 because it is still the highest so far value.

#

(You also need to keep track of the points while you are doing this but that can be handled in the same if-statement)

#

Another approach you could take is to store the values in a point struct and then std::sort it with a lambda that sorts based on distance

lapis jay
#

But i dont know how to do that

#

And I don't know how to make the coordinates be written to the answer according to distances

lapis jay
#

@ripe relic can you help me?

#

To do that

unborn sinew
lapis jay
#

not really

unborn sinew
lapis jay
#

Not yet

unborn sinew
#

or double array

unborn sinew
#

do you know how to sort arrays?

lapis jay
#

I know how sort but I dont know how to sort coordinates number by them distance

#

I already did distance now I just need to do on 1.1 the that the answer is the largest distance and its coordinates, but I don't know how to write the code

unborn sinew
#

because

#

you can define a point as a struct with 2 integers

#

so you will have

lapis jay
#

But I dont know how to use struct 🙄

unborn sinew
#

since your teacher aparently wants to make you miserable

lapis jay
#

You can do it in any way, because you don't specify which way to do it, but then I will need to learn "struct"

unborn sinew
lapis jay
#

Yes, because he expects us to learn more at home

unborn sinew
#

give me 10 minutes

lapis jay
#

Okay

unborn sinew
# lapis jay Okay
#include <iostream>
#include <fstream>
#include <algorithm>

struct point
{
    int x;
    int y;
};

double getDistanceToOrigin(const point& p)
{
    return sqrt(p.x*p.x + p.y*p.y);    
}

bool compareDistanceToOrigin(const point& left, const point& right)
{
    return getDistanceToOrigin(left) < getDistanceToOrigin(right);
}

int main ()
{
    
    std::ifstream inputFile("duom.txt");
    //std::ofstream results("result.txt");
    
    const int MAX = 50 * 1000;
    
    point arr[MAX];
    
    int n = 0;
    inputFile>>n;
    
    if(n > MAX)
    {
        std::cout<<"error n is too large\n";
        exit(1);    
    }
    
    for (int i=0; i<n; i++){
        inputFile>>arr[i].x;
        inputFile>>arr[i].y;
    }
    
    std::sort(arr, arr+n, compareDistanceToOrigin);
    
    for (int i=0; i<n; i++){
        std::cout<<arr[i].x<<" "<<arr[i].y<<" distance to origin = "<<getDistanceToOrigin(arr[i])<<"\n";
    }
    
    
    
    return 0;
}
#

@lapis jay
test it

lapis jay
#

1.2 working

unborn sinew
lapis jay
#

trying to understand now

#

but at first not getting 1.1

unborn sinew
#

away from origin

lapis jay
#

so how can I get this answer to file

unborn sinew
#

obviously

lapis jay
#

but only need 1.1 to file

#

and 1.2 to console

unborn sinew
#

well then

#
#include <iostream>
#include <fstream>
#include <algorithm>

struct point
{
    int x;
    int y;
};

double getDistanceToOrigin(const point& p)
{
    return sqrt(p.x*p.x + p.y*p.y);    
}

bool compareDistanceToOrigin(const point& left, const point& right)
{
    return getDistanceToOrigin(left) < getDistanceToOrigin(right);
}

int main ()
{
    
    std::ifstream inputFile("duom.txt");
    std::ofstream results("result.txt");
    
    const int MAX = 50 * 1000;
    
    point arr[MAX];
    
    int n = 0;
    inputFile>>n;
    
    if(n > MAX)
    {
        std::cout<<"error n is too large\n";
        exit(1);    
    }
    
    for (int i=0; i<n; i++){
        inputFile>>arr[i].x;
        inputFile>>arr[i].y;
    }
    
    std::sort(arr, arr+n, compareDistanceToOrigin);
    
    results<<arr[n-1].x<<" "<<arr[n-1].y<<" distance to origin = "<<getDistanceToOrigin(arr[n-1])<<"\n";
    for (int i=0; i<n; i++){
        std::cout<<arr[i].x<<" "<<arr[i].y<<" distance to origin = "<<getDistanceToOrigin(arr[i])<<"\n";
    }
    
    
    
    return 0;
}
#

@lapis jay

lapis jay
#

oh okay

#

Now I will try to understand everything how working

unborn sinew
lapis jay
#

btw thank you very much

#

Yea I will ask

#

What is the std before sort/cout/ifstream/ofstream?

#

Never used that before

unborn sinew
#

the standard namespace

#

all things from the standard C++ library are taken from there

#

other libraries can have their own namespace

lapis jay
#

Okay

#

So the struct is to group several related variables into one place, how I understand

stiff sorrelBOT
#

@lapis jay Has your question been resolved? If so, run !solved :)

lapis jay
#

can you explain this

#

and how understand bool in this code

ripe relic
#
const point& p```means that you are referencing a variable instead of copying it
ripe relic
lapis jay
lapis jay
#

Thank you @unborn sinew

#

!solved