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.
Related
I have coded up a gauss seidel method that works just fine but i cannot seem to figure out how to convert that to jacobi... I know it should be easy so i must be missing something simple. For the assignment i had to make my own vector and matrix classes so that is why Vector is capital and called differently. Here is my working gauss seidel code:
else if (mode == 3) {
Vector temp;
temp.allocateData(b.numElems());
Vector old = temp;
Vector sum;
double f = 50;
int y = 4;
double tol = 1e-12;
double error = 10;
int max = 999999;
int count = 0;
while ( error > tol && max > count) {
for (int i = 0; i < row_; i++) {
temp.setVal(i, b.getVal(i) / M[i][i]);
for (int j = 0; j < col_; j++) {
if (j == i) {
continue;
}
temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * temp.getVal(j)));
old.setVal(j, temp.getVal(i));
}
cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
old = temp;
}
cout << "\n";
count++;
}
}
and here is my attempt at jacobi:
else if (mode == 2) {
Vector temp;
temp.allocateData(b.numElems());
Vector old = temp;
Vector sum;
double f = 50;
int y = 4;
double tol = 1e-12;
double error = 10;
int max = 999999;
int count = 0;
while ( error > tol && max > count) {
old.allocateData(b.numElems());
for (int i = 0; i < row_; i++) {
old.setVal(i, temp.getVal(i));
temp.setVal(i, b.getVal(i) / M[i][i]);
for (int j = 0; j < col_; j++) {
if (j == i) {
continue;
}
temp.setVal(i, temp.getVal(i) - ((M[i][j] / M[i][i]) * old.getVal(j)));
}
cout<<"x"<< i + 1 << "="<< temp.getVal(i) <<" \n";
error = abs(temp.getVal(i)-old.getVal(i))/abs(temp.getVal(i));
}
cout << "\n";
count++;
}
}
thanks everyone ahead of time for the help!!!
I've read many posts but I can't figure out where is my error so I go with it.
My program throws the segfault at the cout<<endl. When I erase it, the program doesn't even print anything. It just prints the segfault. Aparently the program never reach to sort the vector.
#include <iostream>
#include <vector>
using namesapce std
void inserctionSort(std::vector<double> &v, int i, int j)
{
double temp;
for(i; i < v.size(); i++)
{
temp = v[i];
j = i - 1;
while((v[j] > temp) && (j >= 0))
{
v[j+1] = v[j];
j--;
}
v[j+1] = temp;
}
}
void merge_(std::vector<double> &v, int i, int k, int j)
{
std::vector<double> w(v.size());
int n = j - i + 1;
int p = i;
int q = k + 1;
for(int l = 0; l < n; l++)
{
if(p <= k && (q > j || v[p] <= v[q]))
{
w[l] = v[p];
p++;
}else
{
w[l] = v[q];
q++;
}
}
for(int l = 0; l < n; l++)
v[i - 1 + l] = w[l];
}
void mergeSort(std::vector<double> &v, int i, int j)
{
int n = j - i + 1, n0 = 3;
int k;
if(n <= n0)
{
inserctionSort(v,i,j);
}else
{
k = i - 1 + n / 2;
mergeSort(v, i, k);
mergeSort(v, k + 1, j);
merge_(v, i, k, j);
}
}
int main()
{
vector<double> v1 = {3.2,4.1,55.42,2.24,5.424,667.32,35.54};
cout<<"Vector desordenado: ";
for(int i = 0; i < v1.size(); i++)
cout<<v1[i]<<", ";
cout<<"hola";
cout<<endl;
cout<<"hola";
mergeSort(v1, 0, v1.size()-1); //--> Core generado
//quickSort(v1, 0, v1.size()-1);
cout<<"Vector ordenado: ";
for(int i = 0; i < v1.size(); i++)
cout<<v1[i]<<", ";
return 0;
}
You have problems in your code with vector indices assuming value -1 inside a couple of loops.
I have corrected these mistakes below, a working version of your code:
#include <iostream>
#include <vector>
using namespace std;
void inserctionSort(vector<double> &v, int i, int j)
{
int v_size = v.size();
double temp;
for (; i < v_size; i++) {
temp = v[i];
j = i - 1;
while ( (j >= 0) && (v[j] > temp) ) { // swapped conditions, as when j=-1, v[j]>temp is undefined
v[j+1] = v[j];
j--;
}
v[j+1] = temp;
}
}
void merge_(vector<double> &v, int i, int k, int j)
{
vector<double> w( v.size() );
int n = j - i + 1;
int p = i;
int q = k + 1;
for (int l = 0; l < n; l++) {
if ( p <= k && (q > j || v[p] <= v[q]) ) {
w[l] = v[p];
p++;
} else {
w[l] = v[q];
q++;
}
}
for(int l = 0; l < n; l++)
v[i + l] = w[l]; // deleted -1 from v[i - 1 + l], as it leads to v[-1] for i,l = 0
}
void mergeSort(vector<double> &v, int i, int j)
{
int n = j - i + 1, n0 = 3; // n = v.size()
int k;
if (n <= n0) {
inserctionSort(v,i,j);
} else {
k = i - 1 + n / 2;
mergeSort(v, i, k);
mergeSort(v, k + 1, j);
merge_(v, i, k, j);
}
}
int main()
{
vector<double> v1 = {3.2,4.1,55.42,2.24,5.424,667.32,35.54};
cout<<"Vector desordenado: ";
for (unsigned i = 0; i < v1.size(); i++)
cout<<v1[i]<<", ";
cout << "hola";
cout << endl;
cout << "hola";
mergeSort(v1, 0, v1.size()-1); //--> Core generado
//quickSort(v1, 0, v1.size()-1);
cout<<"Vector ordenado: ";
for (unsigned i = 0; i < v1.size(); i++)
cout << v1[i] << ", ";
return 0;
}
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;
}
hello my problem is that i have to extract the coefficients and exponent of a polynomial given by the user.
when i tried my code it just worked for the coefficient, and for the exponent it gives me a zero. p.s a is just for testing
int main() {
char x[10];
char y[10];
char a[100] = "53x2+4x^3";
for (int i = 0; a[i] != '+'; i++)
{
if (a[i] != 'x')
{
x[i] = a[i];
}
}
for (int i = 0; a[i] != '+'; i++)
{
if ((a[i] == 'x') && (a[i + 1] == '^')) {
y[i] = a[i + 2];
}
}
double w;
int z;
w = atof(x);
z = atoi(y);
cout << w << endl;
cout << z << endl;
return 0;
}
You need to initialize your coefficient and exponent buffers with null terminators so that they are properly null terminated when the output console reads them.
#include <iostream>
using namespace std;
int main() {
char x[10] = {'\0'};
char y[10] = {'\0'};
char a[100] = "53x^2+4x^3";
for (int i = 0, j = 0; a[i] != '+'; i++)
{
if (a[i] != 'x')
{
x[j] = a[i];
j++;
}
}
for (int i = 0, j = 0; a[i] != '+'; i++)
{
if ((a[i] == 'x') && (a[i + 1] == '^')) {
y[j] = a[i + 2];
j++;
}
}
double w;
int z;
w = atof(x);
z = atoi(y);
cout << w << endl;
cout << z << endl;
return 0;
}
My algorithm for the best-first, branch and bound knapsack problem is giving me a max profit of 80 when it should be 90. I'm wondering where I went wrong... my thinking is the priority queue is a bit off.
Given input:
4,16 // 4 items to follow , 16 capacity knapsack
2,40 // item1.. weighs 2, costs 40
5,30 // item2.. weighs 5, costs 30
10,50 // item3.. weighs 10, costs 50
5,10 // item4.. weighs 5, costs 10
Code:
#include <iostream>
#include <string>
#include <queue>
#include <utility>
typedef struct node{
int level;
int profit;
int weight;
int bound;
} node;
struct node_cmp{
bool operator()(const node& a, const node& b) const{
return a.bound < b.bound;
}
};
int KWF2(int i, int weight, int profit, int *w, int *p, int C, int n){
int weight1 = weight;
int bound = profit;
int j;
float x[n+1];
for(j = i; j <= n; j++){
x[j] = 0;
}
while(weight1 < C && (i <= n)){
if(weight1 + w[i] <= C){
x[i] = 1;
weight1 += w[i];
bound += p[i];
}
else{
x[i] = ((float)C-(float)weight1)/(float)w[i];
weight1 = C;
bound = bound + p[i] * x[i];
}
i++;
}
return bound;
}
void knapsack(int *w, int *p, int C, int maxprofit, int n){
int maxp = maxprofit;
std::priority_queue<node,std::vector<node>,node_cmp> PQ;
node u,v;
v.level = 0;
v.profit = 0;
v.weight = 0;
v.bound = KWF2(v.level+1,v.weight,v.profit,w,p,C,n);
PQ.push(v);
while(!PQ.empty()){
v = PQ.top();
PQ.pop();
if(v.bound > maxp){
u.level = v.level + 1;
//yes child
u.weight = v.weight + w[u.level];
u.profit = v.profit + p[u.level];
if((u.weight <= C) && (u.profit > maxp)){
maxp = u.profit;
}
if(KWF2(u.level+1,u.weight,u.profit,w,p,C,n) > maxp){
PQ.push(u);
}
//no child
u.weight = v.weight;
u.profit = v.profit;
u.bound = KWF2(u.level+1,u.weight,u.profit,w,p,C,n);
if(u.bound > maxp){
PQ.push(u);
}
}
}
printf("%d\n",maxp);
}
int main(int argc, char **argv){
int n,C;
FILE *in = fopen(argv[1],"r");
fscanf(in,"%d,%d",&n,&C);
int w[n+1];
int p[n+1];
float ratio[n+1];
for(int i = 0; i < n; i++){
fscanf(in,"%d,%d",&w[i+1],&p[i+1]);
ratio[i+1] = (float)p[i+1]/(float)w[i+1];
}
int temp_w,temp_p;
float temp_r;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
if(ratio[i] < ratio[j]){
temp_w = w[i];
temp_p = p[i];
temp_r = ratio[i];
w[i] = w[j];
p[i] = p[j];
ratio[i] = ratio[j];
w[j] = temp_w;
p[j] = temp_p;
ratio[j] = ratio[i];
}
}
}
int maxprofit = 0;
knapsack(w,p,C,maxprofit,n);
fclose(in);
return 0;
}