The file point2d search hint.cpp reads in a file named point2d.csv containing two commaseparated floating point numbers per line. It parses this into a Point2d variable and then prints out the length of the point. Modify the program to identify the point furthest from the origin and output its x- and y-coordinates to the terminal (and no nother points).
The file point2d search hint.cpp reads in a file named point2d.csv containing two commaseparated floating point numbers per line. It parses this into a Point2d variable and then prints out the length of the point. Modify the program to identify the point furthest from the origin and output its x- and y-coordinates to the terminal (and no nother points).
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
The file point2d search hint.cpp reads in a file named point2d.csv containing two commaseparated
floating point numbers per line. It parses this into a Point2d variable and
then prints out the length of the point.
Modify the program to identify the point furthest from the origin and output its x- and
y-coordinates to the terminal (and no nother points).
#include <string>
#include <fstream>
#include <iostream>
#include <cmath>
struct Point2d
{
double x;
double y;
void print()
{
// example: (2.5,3.64)
std::cout << "(" << x << "," << y << ")" << std::endl;
}
double length() const
{
return std::sqrt(x * x + y * y);
}
// const Point2D & ==> function can't modify 'other'
// the second const ==> function can't modify x, y
Point2d add(const Point2d &other) const
{
Point2d result;
result.x = x + other.x;
result.y = y + other.y;
return result;
}
};
// Example of how to parse a comma-separated string
// The variable 'line' must contain a string like "-160.0,231.00"
Point2d parse(std::string &line)
{
Point2d p;
// find index of comma character
int idxOfComma = line.find(',');
// create sub-string of everything before comma
std::string xAsString = line.substr(0, idxOfComma);
p.x = std::stod(xAsString);
// create sub-string of everything after comma
std::string yAsString = line.substr(idxOfComma + 1); // from idxOfComma+1 to end
p.y = std::stod(yAsString);
return p;
}
int main(int argc, char *argv[])
{
// point2d.csv must be in same directory as program
std::string path = "point2d.csv";
std::ifstream file(path);
if (!file.is_open())
#include <fstream>
#include <iostream>
#include <cmath>
struct Point2d
{
double x;
double y;
void print()
{
// example: (2.5,3.64)
std::cout << "(" << x << "," << y << ")" << std::endl;
}
double length() const
{
return std::sqrt(x * x + y * y);
}
// const Point2D & ==> function can't modify 'other'
// the second const ==> function can't modify x, y
Point2d add(const Point2d &other) const
{
Point2d result;
result.x = x + other.x;
result.y = y + other.y;
return result;
}
};
// Example of how to parse a comma-separated string
// The variable 'line' must contain a string like "-160.0,231.00"
Point2d parse(std::string &line)
{
Point2d p;
// find index of comma character
int idxOfComma = line.find(',');
// create sub-string of everything before comma
std::string xAsString = line.substr(0, idxOfComma);
p.x = std::stod(xAsString);
// create sub-string of everything after comma
std::string yAsString = line.substr(idxOfComma + 1); // from idxOfComma+1 to end
p.y = std::stod(yAsString);
return p;
}
int main(int argc, char *argv[])
{
// point2d.csv must be in same directory as program
std::string path = "point2d.csv";
std::ifstream file(path);
if (!file.is_open())
{
std::cout << "ERROR: the file " << path << " did not open" << std::endl;
return -1;
}
std::string line;
while (std::getline(file, line))
{
Point2d point = parse(line);
std::cout << point.length() << std::endl;
}
return 0;
}
std::cout << "ERROR: the file " << path << " did not open" << std::endl;
return -1;
}
std::string line;
while (std::getline(file, line))
{
Point2d point = parse(line);
std::cout << point.length() << std::endl;
}
return 0;
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY