#Coordinates distance and more
1 messages · Page 1 of 1 (latest)
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.
#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
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
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
do you know how structs work
not really
have you used struct before
Not yet
have you implemented bubble sort on an int array?
or double array
only did that on this
they are asking you to sort the points right?
do you know how to sort arrays?
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
this is why I was asking about struct
because
you can define a point as a struct with 2 integers
so you will have
But I dont know how to use struct 🙄
okay fine, we will do this the hard way
since your teacher aparently wants to make you miserable
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"
so you are telling me I can use thing you have not learned yet?
Yes, because he expects us to learn more at home
alright
give me 10 minutes
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
1.2 working
so do you understand what I wrote
when the points are sorted, the element arr[n-1] is the point whose distance is the largest
away from origin
so how can I get this answer to file
aha
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
you know, you can ask me
btw thank you very much
Yea I will ask
What is the std before sort/cout/ifstream/ofstream?
Never used that before
that is the namespace std
the standard namespace
all things from the standard C++ library are taken from there
other libraries can have their own namespace
Okay
So the struct is to group several related variables into one place, how I understand
@lapis jay Has your question been resolved? If so, run !solved :)
yes
const point& p```means that you are referencing a variable instead of copying it
wdym?
oh oaky
nvm its okay now