Need to find 3 points that make up the largest perimeter - c++

I have a structure called point. Obviously, it provides coordinates of a point( x and y). I have an array of points.
What I need to do is find 3 points that make up the largest triangle. I've tried a lot of options and I didn't make it.
Can you think of an algorithm that would create the outcome I need?
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
struct point {
double x, y;
};
int main()
{
double n, c1, c2;
double max(0), max1(0), max2(0), temp;
point *a = NULL;
cout << "Enter the number of points: ";
cin >> n;
a = new point[n];
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a[i] = dot;
};
for (int i = 0; i < n-1; i++) { // here I'm running out of ideas
for (int j = 1; j < n; j++) {
temp = sqrt((a[i].x + a[i].y)*(a[i].x + a[i].y)- (a[j].x + a[j].y)*(a[j].x + a[j].y));
if (temp > max)
}
}

You can just iterate over all sets of three points. Note that it is better to use a vector instead of an array and it is useful to put the code to calculate the perimeter in a separate function
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct point {
double x, y;
};
double perim(point p1, point p2, point p3)
{
double result = 0;
result += sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
result += sqrt(pow(p2.x - p3.x, 2) + pow(p2.y - p3.y, 2));
result += sqrt(pow(p3.x - p1.x, 2) + pow(p3.y - p1.y, 2));
return result;
}
int main()
{
double n, c1, c2;
double max(0), temp;
int p1 = 0, p2 = 0, p3 = 0;
vector <point> a;
cout << "Enter the number of points: ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a.push_back(dot);
};
for (int i = 0; i < n - 2; i++) { // here I'm running out of ideas
for (int j = i+1; j < n - 1; j++) {
for (int k = j+1; k < n; k++) {
temp = perim(a[i], a[j], a[k]);
if (temp > max) {
max = temp;
p1 = i; p2 = j; p3 = k;
}
}
}
}
cout << p1 << " "<<p2<< " "<<p3;
}

Related

I am getting stack smash error specifically on moodle

I have ran the same code on my local compiler and it works perfectly but for some reason on moodle it runs , gives an output and at the end gives a stack smash error. Is it because of the sscanf?
Here is the input:
10
(1,3)
(12,10)
(6,5)
(22,13)
(2,15)
(35,-10)
(15,-15)
(20,5)
(12,-8)
(1,-10)
#include<iostream>
#include<vector>
#include<cstdio>
#include<string>
#include<stdlib.h>
using namespace std;
int arr[20] , arr2[20],end = 0;
struct Point
{int x, y;
};
Point p0;
Point nextToTop(vector<Point> &S)
{
Point p = S.front();
S.erase(S.begin());
Point res = S.front();
S.insert(S.begin(),p);
return res;
}
void swap(Point &p1, Point &p2)
{Point temp = p1;
p1 = p2;
p2 = temp;
}
int distSq(Point p1, Point p2)
{return (p1.x - p2.x)*(p1.x - p2.x) +
(p1.y - p2.y)*(p1.y - p2.y);
}
int orientation(Point p, Point q, Point r)
{int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
int compare(const void *vp1, const void *vp2)
{
Point *p1 = (Point *)vp1;
Point *p2 = (Point *)vp2;
int o = orientation(p0, *p1, *p2);
if (o == 0)
return (distSq(p0, *p2) >= distSq(p0, *p1))? -1 : 1;
return (o == 2)? -1: 1;
}
void convexHull(Point points[], int n)
{
int ymin = points[0].y, min = 0;
for (int i = 1; i < n; i++)
{
int y = points[i].y;
if ((y < ymin) || (ymin == y &&
points[i].x < points[min].x))
ymin = points[i].y, min = i;
}
swap(points[0], points[min]);
p0 = points[0];
qsort(&points[1], n-1, sizeof(Point), compare);
int m=1;
for (int i=1; i<n; i++)
{
while (i < n-1 && orientation(p0, points[i],
points[i+1]) == 0)
i++;
points[m] = points[i];
m++;
}
if (m < 3) return;
vector<Point> S;
S.insert(S.begin(),points[0]);
S.insert(S.begin(),points[1]);
S.insert(S.begin(),points[2]);
for (int i = 3; i < m; i++)
{
while (S.size()>1 && orientation(nextToTop(S), S.front(), points[i]) != 2)
S.erase(S.begin());
S.insert(S.begin(),points[i]);
}
vector<Point> S2 = S;
int k=0;
while(S2.size()>0){
Point a = S2.front();
arr[k] = a.x;
arr2[k] = a.y;
S2.erase(S2.begin());
k++;
}
int c,d;
for(int j = 0; j<k;j++){
for(int i = j+1; i<k;i++){
if(arr[j]>arr[i]){
c = arr[j];
d = arr2[j];
arr[j] = arr[i];
arr2[j] = arr2[i];
arr[i] = c;
arr2[i] = d;
}else if(arr[j] == arr[i]){
c = arr[j];
d = arr2[j];
if(arr2[j] > arr2[j]){
arr[j] = arr[i];
arr2[j] = arr2[i];
arr[i] = c;
arr2[i] = d;}}}}
for(int j =0;j<k;j++)
{cout << "(" << arr[j] << "," << arr2[j] <<")" << endl;
}}
int main()
{int n;
cin>>n;
Point points[n] ;
int a,b;
char c[5];
for(int i=0;i<n;i++){
cin>>c;
sscanf(c, "(%d,%d)", &a,&b);
points[i] = {a,b};}
convexHull(points, n);
return 0;
}
Here is the output picture
I looked at your code, and I think problem is in sscanf, and solution is actually simple.
Try to change declaration of string ( char array ) in main function from char c[5]; to char c[10]; it can be any other number, but I think, if you enter even bigger numbers and c[10] will make error, so I recommend you to make it char c[50]; or because you're using integers I think char c[25]; will be fine.

In Cpp for is not working properly from the function

I need to do this exercise, with struct and include the function in the struct to do the calculations, but for some reason, it's not working.
I recommended you to check the image for a better idea.
#include <iostream>
using namespace std;
struct Alfa{
double h,x,n,a;
void shuma(){
cout << "enter n: "; cin >> n;
cout << "enter a: "; cin >> a;
for (int i = 1; i >= n; i++){
x = 2 * i + a;
}
};
};
int main() {
Alfa alf;
alf.shuma();
alf.h = (alf.x / 2) + 3;
cout << alf.h;
return 0;
}
You are not following the formula in the image. Use this instead:
double sum = 0;
for (int i = 1; i <= n + 1; i++) {
if (i != 4) {
sum += 2 * i + a;
}
}
h = x / 2 + 3 * sum;
for (int i = 1; i >= n; i++) means i initiated to 1, while i>=n do the loop, then inc i. So when n is bigger then 1, it will never enter the loop. Maybe you want for (int i = 1; i <= n+1; i++) ?

Which algorithm should I use to calculate the given math sequence?

The user must enter a number n (1; infinity). Then the program does this:
cos1/sin1 * (cos1+cos2)/(sin1+sin2) * … * (cos1+cos2+...+cos⁡ n)/(sin1+sin2+...+sin ⁡n )
I tried to calculate that:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
double res;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++)
{
res = cos(i) / sin(i);
}
cout << res;
}
But I don't know which algorithm would do it correctly.
Since you need to divide the two sums in each step, you need to store those sums, and multiply an accumulating product with the result of dividing them.
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
double cos_sum = 0.0;
double sin_sum = 0.0;
double res = 1.0;
for (int i = 1; i <= n; i++)
{
cos_sum += cos(i);
sin_sum += sin(i);
res *= cos_sum / sin_sum;
}
cout << res;
}
There are basically three quantities that accumulate during the iteration. They are:
C[N] = cos1+cos2+cos3+cos3+ .... cosN
S[N] = sin1+sin2+sin3+sin3+ .... sinN
X[N] = C[1] / S[1] * C[2] / S[2] * ... C[N] / S[N]
The recursive relations are:
C[0] = 0
C[N + 1] = C[N] + cosN+1
S[0] = 0
S[N + 1] = S[N] + sinN+1
X[0] = 1
X[N + 1] = X[N] * C[N+1] / S[N+1]
Using this it should be straightforward to write the loop:
#include <iostream>
int main() {
int n;
double X = 1;
double C = 0;
double S = 0;
cout << "Enter n: ";
cin >> n;
for (int i = 1; i < n; i++) {
C += cos(i);
S += sin(i);
X *= C / S;
}
cout << X;
}

Having trouble implementing pseudocode for Subset Sum

so I am trying to implement the following pseudocode but it will not work as it is supposed to. Here is the problem description in the slide, "Given an integer bound, "W", and a collection of "n" items, each with a positive integer weight "wi", find a subset S of items that: maximizes Sigma sub i where i is an element of S "wi" while keeping this sum less than or equal or to W. I will attach the following slides for where I am getting the problem description and pseudocode from. The problem with my implementation is that it will only find the total max value and not the value that is less than or equal to the weight. So for example, if I had Weight 10 (W = 10) and items 3 (n = 3) with item weights 1, 4, & 8 then the following answer should be 9; however, my solution gives 12. Here are the slides (*Please not, where it says w[j] it is meant to be w[i] - the slide had a typo):
Here is my code that implements the pseudocode:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int max(int a, int b, int c) {
if (a >= b)
return a;
else
return b;
}
int optimal_weight(int W, const vector<int> &wt, int n){
vector<vector<int> > M;
M.resize(n+1);
for(int i = 0; i < n+1; ++i){
M[i].resize(W+1);
}
for(int w = 0; w < W+1; w++){
M[0][w] = 0;
}
for(int i = 1; i < n+1; i++){
M[i][0] = 0;
}
for(int i = 1; i < n+1; i++){
for(int w = 0; w < W+1; w++){
if(wt[i] > w){
M[i][w] = M[i-1][w];
}
M[i][w] = max(M[i-1][w], wt[i] + M[i-1][W-wt[i]], W);
}
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= W; j++)
printf ("%4d", M[i][j]);
printf("\n");
}
return M[n][W];
}
int main()
{
//int val[] = {1, 1, 1};
int W;
int n;
cin >> W >> n;
vector<int> wt(n);
for(int i = 0; i < n; i++){
cin >> wt[i];
}
cout << optimal_weight(W, wt, n) << endl;
}
Thank you for any help!
I figured it out! Here is my solution:
#include <iostream>
#include <vector>
using namespace std;
using std::vector;
int optimal_weight(int W, const vector<int> &wt) {
//write your code here
int n = wt.size();
vector<vector<int> > matrix;
matrix.resize(W+1);
for(int i = 0; i < W+1; i++){
matrix[i].resize(n);
}
for(int j = 0; j < n; j++){
matrix[0][j] = 0;
}
for(int w = 0; w < W + 1; w++){
matrix[w][0] = 0;
}
for(int i = 1; i < n; i++){
for(int w = 1; w < W+1; w++){
matrix[w][i] = matrix[w][i-1];
if(wt[i] <= w){
//cout << wt[i] << endl;
int val = matrix[w-wt[i]][i-1] + wt[i];
if(matrix[w][i] < val){
matrix[w][i] = val;
}
}
}
}
return matrix[W][n-1];
}
int main() {
int n, W;
std::cin >> W >> n;
vector<int> wt(n+1);
for (int i = 1; i < n+1; i++) {
wt[0]=0;
std::cin >> wt[i];
}
std::cout << optimal_weight(W, wt) << '\n';
}

Why is the axis for my program off

//============================================================================
// Name : Assignment.cpp
// Author : Tim Bialecki
// Version :
//============================================================================
#include <iostream>
#include <math.h>
using namespace std;
void circle(int x, int y, int radius);
void line(int a, int b, int c, int d);
bool buffer[26][81];
char drawSpace[26][81];
int main() {
int a = 75;
int b = 5;
int c = 4;
int d = 26;
/*cout << "please enter an x coordinate for the center of the circle";
cin >> x;
cout << "please enter a y coordinate for the center of the circle";
cin >> y;
cout << "please enter a value for the radius of the circle";
cin >> radius;*/
circle(a, b, c);
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
return 0;
}
void circle(int x, int y, int radius){
/*if (x + radius >= 81 || y + radius >= 26 || y - radius <= 26){
cout << "the coordinates provided for the circle will not fit on the screen" << endl;
return;
}*/
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int a = abs (x - j);
int b = abs (y - i);
int distance = pow(a, 2) + pow(b, 2);
int realDistance = pow(radius, 2);
if (abs(realDistance - distance) <= 3){
buffer[i][j] = true;
}
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n]){
drawSpace[m][n] = 42;
}
else
drawSpace[m][n] = 32;
}
}
}
void line(int a, int b, int c, int d){
int intercept = 0;
double rise = d - b;
double run = c - a;
double slope = rise/run;
intercept = b - (slope*a);
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}
}
for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n])
drawSpace[m][n] = 42;
else
drawSpace[m][n] = 32;
}
}
}
This code is a work in progress, but I'm trying to write a program that takes inputs for the coordinates and dimensions of both a line and a circle and prints them out in the terminal window as if it were a 81x26 graph. I have just supplied sample inputs to test this out, but for some reason the shapes are not printing with the appropriate orientation to what should be the x and y axises. I have tried a bunch of different ways of trying to fix this problem and have had no luck. Hoping someone can help. Thanks
Looks OK to me:
***
** **
* *
* *
* *
* *
* *
** **
***
It's not perfectly round because characters are taller than they are wide.
EDIT: That's only the first few rows on my output. Based on the comment and a second look at the code, I think rows and columns are getting mixed up.
for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
There's a newline after every "column". Swapping the two for lines may produce what you want.