I'm having problems with the last part of my project on this algorithm. I'm having a problem with the .size() of my vector. It doesn't give me anything. Everytime I try to debug it, it's 0. I don't know if that means that my function is not working properly.
I'm new to programing and I need some help.
Edit1: I've added the new code that is giving me an exception.
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
using namespace System;
using namespace std;
typedef struct Linea {
int a;
int q;
int b;
int r;
} SLinea;
int algoritmoeuclides(int a,int b)
{
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
std::pair<int, int>division(int dividendo,int divisor)
{
int resultado=0;
int residuo=0;
residuo=dividendo%divisor;
resultado=dividendo/divisor;
std::pair<int, int>retorno;
retorno.first=resultado;
retorno.second=residuo;
return retorno;
}
std::pair<int, int>EuclidesExtendido(int a,int b,vector<SLinea*> aux)
{
std::pair<int, int> tsqt;
int q,r,s,t;
SLinea *var;
if (b == 0)
return std::pair<int, int> (1, 0);
else
{
std::pair<int, int>qr = division(a, b);
q=qr.first;
r=qr.second;
std::pair<int, int>st = EuclidesExtendido(b, r,aux);
s=st.first;
t=st.second;
}
tsqt.first=t;
tsqt.second=s-q*t;
var->a=a;
var->b=b;
var->q=q;
var->r=r;
aux.push_back(var);
return tsqt;
}
int main()
{
int a=23;
int b=120;
int s,t;
vector<SLinea*> Ecuacion;
std::pair<int, int>st=EuclidesExtendido(a,b,Ecuacion);
s=st.first;
t=st.second;
int resultado=algoritmoeuclides(a,b);
int n=Ecuacion.size();
for (int i=0;i<n;i++)
{
printf("%d=%dx%d+%d\n",Ecuacion[i]->a,Ecuacion[i]->b,Ecuacion[i]->q,Ecuacion[i]->r);
}
printf("MCD= %d\n ",resultado);
printf("s=%d \nt=%d",s,t);
system("pause");
}
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
using namespace System;
using namespace std;
typedef struct Linea {
int a;
int q;
int b;
int r;
} SLinea;
int algoritmoeuclides(int a,int b)
{
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
std::pair<int, int>division(int dividendo,int divisor)
{
int resultado=0;
int residuo=0;
residuo=dividendo%divisor;
resultado=dividendo/divisor;
std::pair<int, int>retorno;
retorno.first=resultado;
retorno.second=residuo;
return retorno;
}
std::pair<int, int>EuclidesExtendido(int a,int b,vector<SLinea> aux)
{
std::pair<int, int> tsqt;
int q,r,s,t;
SLinea var;
if (b == 0)
return std::pair<int, int> (1, 0);
else
{
std::pair<int, int>qr = division(a, b);
q=qr.first;
r=qr.second;
std::pair<int, int>st = EuclidesExtendido(b, r,aux);
s=st.first;
t=st.second;
}
tsqt.first=t;
tsqt.second=s-q*t;
var.a=a;
var.b=b;
var.q=q;
var.r=r;
aux.push_back(var);
return tsqt;
}
int main()
{
int a=23;
int b=120;
int s,t;
vector<SLinea> Ecuacion;
std::pair<int, int>st=EuclidesExtendido(a,b,Ecuacion);
s=st.first;
t=st.second;
int resultado=algoritmoeuclides(a,b);
int n=Ecuacion.size();
for (int i=0;i<n;i++)
{
printf("%d=%dx%d+%d\n",Ecuacion[i].a,Ecuacion[i].b,Ecuacion[i].q,Ecuacion[i].r);
}
printf("MCD= %d\n ",resultado);
printf("s=%d \nt=%d",s,t);
system("pause");
}
std::pair<int, int> EuclidesExtendido(int a,int b,vector<SLinea> aux)
That function takes the parameter aux by value, which means that it gets a copy of whatever its used as an argument. I'll let you take it from there...
Related
I'm trying to find out the GCD in C++. I'm using __gcd code as mentioned on this website : https://www.geeksforgeeks.org/stdgcd-c-inbuilt-function-finding-gcd/
Appended is my code. Can someone please guide as to what is wrong
#include <iostream>
#include <string>
#include <numeric>
#include <algorithm>
using namespace std;
class Rational {
private:
int num;
int denom;
public:
Rational(int a, int b){
num = a;
denom = b;
}
int add(){
return num + denom;
}
int sub(){
return num - denom;
}
int mul(){
return num * denom;
}
void gcd(){
cout <<__gcd(num,denom);
}
int simplify(){
int gcd1 = gcd(num,denom);
return (num/gcd1,denom/gcd1);
}
};
int main(){
Rational r(2,6);
cout<<r.add()<<endl;
cout<<r.sub()<<endl;
cout<<r.mul()<<endl;
cout<<r.gcd()<<endl;
after searching i had found amazing code for integration by
quadrature boost library.
rather than
log(x)/(1+x)
want to integrate
(poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x). But, i do not
know how to insert the vector
poly
to
struct f
or even how to call these operators from main function. The code :
#include<iostream>
#include<boost/math/constnats/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
struct f
{
double operator()(double x) const {
return (log(x)/(1+x); }
};
int main()
{
vector<cpp_dec_float_50> poly(0);
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(f(),0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
cpp_dec_float_50 polynomial(int k ,int n)
{
.
.
.
}
Also, when changing the double operator, to cpp_dec_float_50 operator in
struct f
many problems arise. and the later type is necessary in my project. Any one can fix that ?
EDIT
i tried this, but i do sth wrong
#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{ const cpp_dec_float_50 s=0;
vector<cpp_dec_float_50> poly;
cpp_dec_float_50 sum()const{
for(int i=0;i<=poly.size();i++)
s+=poly[i];
return s
}
double operator()(double x) const {
return
s*log(x)/(1+x); }
};
int main()
{
int n=2;
f fun;
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
double polynomial(int k ,int n)
{
return k;
}
Edit
when using Patstew suggestion
Two errors occur
Try something along the lines of:
struct f
{
vector<cpp_dec_float_50> poly;
double operator()(double x) const {
return (poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x); }
};
int main()
{
f fun;
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
EDIT: RE you own answer, you never call sum (and s is const so you couldn't change it if you did) so s is always 0 and you will always get 0 as your answer. Also you are iterating all the way up to poly.size() in sum(), but poly[poly.size()-1] is the last element. I think you really want your sum function to calculate a polynomial? Try this:
#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{
vector<double> poly;
double polysum(double x) {
double s = poly[0];
double p = 1;
for(int i=1;i<poly.size();i++) {
p = p*x;
s+= p*poly[i];
}
return s
}
double operator()(double x) {
return polysum(x)*log(x)/(1+x); }
};
int main()
{
int n=2;
f fun;
double p = 0;
for (int i=0;i<=n;i++)
{
p=polynomial(i,n);
fun.poly.push_back(p);
}
double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
double polynomial(int k ,int n)
{
return k;
}
I am trying to call this recursive method,but I don't understand why it's not reaching the else statement to return mul. I am passing value 0,in the main function,which is less than n,and on x becoming greater than n,it should move to else function.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n,mul=0;
vector <int> a;
int calc(int x)
{ int mul,chk;
int l=n;
if(x<n)
{
chk=a[x]*(l-1);
l--;
if(mul<chk)
{
mul=chk;
}
calc(x+1);
}
else
{ return mul;}
}
int main() {
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
int z=calc(0);
cout<<z;
return 0;
}
Wihtout knowing the exact error message, I am not 100% sure what is the problem, but I guess it is the following:
Your function looks like this (simplified):
int calc(int x) {
if(someCondition){
if(otherCondition){}
calc(x+1);
} else {
return mul;
}
}
The problem is that if someCondition is true, the function does not return anything. You probably want to do:
return calc(x+1);
instead of just
calc(x+1);
I am trying to pass a pointer into my classes function, have it incremented, and have the variable retain it's value using pointers. Heres my code, it doesnt increment.
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int y;
};
test::test(int * currentY):
y(*currentY)
{
}
int test::addTo()
{
y++;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
int * number = &pointedAt;
test t(number);
t.addTo();
cout <<*number;
char f;
cin >>f;
}
}
This should do it:
#include "stdafx.h"
#include <iostream>
using namespace std;
class test
{
public:
int addTo();
test(int * currentY);
private:
int *y;
};
test::test(int *currentY):
y(currentY)
{}
int test::addTo()
{
++*y;
return 0;
}
int main ()
{
for (;;)
{
int pointedAt = 1;
test t(&pointedAt);
t.addTo();
cout << pointedAt;
}
}
You have to store a pointer to the integer, so it refers to the same address as the original variable.
I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)