
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Determine the position of the third person on regular N sided polygon in C++ Program
In this tutorial, we are going to learn how to find the position of a third person on a regular N-sided polygon.
We have given a regular N-sided polygon. And there are two persons on two different points already. Our task is to find the third point to place the third person such that the distance between the first two persons and the third person is minimized.
Let's see the steps to solve the problem.
Initialize the N and two points A and B.
Initialize the position of the third person, and the minimum sum to find the position.
Iterate from 1 to N.
If the current position is A or B, then skip it.
Find the sum of the absolute difference between the current position and A, B.
Compare it with the minimum sum.
If the current sum is less than the minimum sum, then update the position and minimum sum.
Print the position of the third person.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int findThirdPersonStandingVertex(int N, int A, int B) { int position = 0; int minimum_sum = INT_MAX, sum; for (int i = 1; i <= N; i++) { // skipping the predefined vertices if (i == A || i == B) { continue; } else { // length between the current vertext to A and B sum = abs(i - A) + abs(i - B); // checking whether the current sum is less than previous sum if (sum < minimum_sum) { // updating the minimum sum and position of vertext minimum_sum = sum; position = i; } } } return position; } int main() { int N = 7, A = 5, B = 7; cout << "Vertex: " << findThirdPersonStandingVertex(N, A, B) << endl; return 0; }
Output
If you execute the above program, then you will get the following result.
Vertex: 6
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Determine the position of the third person on regular N sided polygon in C++?
- Apothem of a n-sided regular polygon in C++
- Area of largest Circle inscribe in N-sided Regular polygon in C Program?
- Area of largest Circle inscribed in N-sided Regular polygon in C Program?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a n-sided regular polygon with given Radius?
- Area of a n-sided regular polygon with given side length in C++
- Find number of diagonals in n sided convex polygon in C++
- Probability that the pieces of a broken stick form a n sided polygon in C++
- Program to find the Circumcircle of any regular polygon in C++
- Find the number of sides of a regular polygon if the exterior angle is one-third of its interior angle.
- FabricJS – Finding the current cursor position on the clicked Polygon object?
- Program to find number of possible position in n-person line with few person at front and back in Python
- Program to find the Interior and Exterior Angle of a Regular Polygon in C++
- The interior angle of a regular polygon is $156$ . Find the number of sides of the polygon.
