Structures

Run Settings
LanguageC++
Language Version
Run Command
// Structures #include "pch.h" #include <iostream> using namespace std; struct point2D { int x; int y; void input() { cout << "\nInput X: "; cin >> x; cout << "\nInput Y: "; cin >> y; } void input(int a, int b) { x = a; y = b; } void showInfo(int n) { std::cout << "\nPoint" << ++n << " (" << x << ", " << y << ")\n"; } void showInfo() { std::cout << "\nPoint (" << x << ", " << y << ")\n"; } double getZeroDistance() { return sqrt(x*x + y * y); } }; struct point3D { int x; int y; int z; void input() { cout << "\nInput X: "; cin >> x; cout << "\nInput Y: "; cin >> y; cout << "\nInput Z: "; cin >> z; } void input(int a, int b, int c) { x = a; y = b; z = c; } void showInfo() { std::cout << "\n(" << x << ", " << y << ", " << z << ")\n"; } }; int main() { /* point3D p1; p1.input(); p1.showInfo(); point3D p2; p2.input(1, 4, 8); p2.showInfo(); */ /* point2D p1, p2, p3; p1.input(1, 7); p2.input(2, 6); p3.input(3, 5); cout << endl << p1.getZeroDistance(); cout << endl << p2.getZeroDistance(); cout << endl << p3.getZeroDistance(); */ const int N = 10; point2D points[N]; for (int i = 0; i < N; i++) { points[i].input(rand()%20, rand() % 20); points[i].showInfo(i); } int indexOfNearest = 0; for (int i = 1; i < N; i++) { if (points[i].getZeroDistance() < points[i-1].getZeroDistance()) indexOfNearest = i; } cout << "Nearest point is: "; points[indexOfNearest].showInfo(indexOfNearest); cout << endl << "It's distance is: " << points[indexOfNearest].getZeroDistance(); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines