__gcd function in visual studio version 1.38.0 - c++

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;

Related

Unknown Override specifier when accessing methods/object across classes

What I want to do (c++ problem):
Enter main. Call Class A (and pass a value). Inside class A, I call Class B (and pass a value). Do some stuff in class B. Return value back to Class A. Do some more stuff in A. Return back to main function.
I get the error that obj3 is an unknown override specifier. I tried to create a simple program to showcase my problem;
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A
{
B obj3;
public:
int add3(int num)
{
int x = num + 1;
int y = obj3.add2(x);
return y;
}
};
class B
{
public:
int add2(int num2)
{
int y = num2 + 2;
return y;
}
};
int main()
{
int g;
A obj1;
cout << "enter a number: " << endl;
cin >> g;
int r = obj1.add3(g);
cout << r;
system("pause");
return 0;
}

How to solve this error of function pointer?

In a program a pointer is declared to function (p) which initialized to function (add)
i tried to read all the concepts related to function pointer.
But i couldn't solve it
Please help me to solve this program with out any errors.
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int *functocall(int,int);
int caller(int n1, int n2, int(*functocall)(int, int))
{
return (*functocall)(n1, n2);
}
int main()
{
int a, b, c;
cin >> b >> c;
int (*p)(int,int)=&add;
a=caller(b,c,(*functocall)(b,c));
printf("%d",a);
return 0;
}
If the input is 20 70
Output must be 90
(*functocall)(b,c) doesn't do what you expect, you're trying to call on the functocall. (Note that functocall is declared as a function, which takes two ints and returns a int*.)
You should pass the function pointer itself to caller, e.g.
a = caller(b, c, p);
or
a = caller(b, c, &add);
LIVE
Way too complicated. it's often the case when you don't understand something you make it more complicated then it needs to be
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int caller(int n1, int n2, int(*functocall)(int, int))
{
return (*functocall)(n1, n2);
}
int main()
{
int a, b, c;
cin >> b >> c;
a = caller(b,c,add);
printf("%d",a);
return 0;
}
Not too sure if you really need caller. Maybe you added than when you were trying to get it to work, but maybe not. An even simpler aternative is
#include <stdio.h>
#include <iostream>
using namespace std;
int add(int n1, int n2)
{
return n1 + n2;
}
int main()
{
int a, b, c;
cin >> b >> c;
int (*functocall)(int, int) = add;
a = functocall(b,c);
printf("%d",a);
return 0;
}

C++ biginteger class operator=

I am trying to implement a biginteger class, and after I created a biginteger class, with a proper header file, and at first I am trying to define a operator=() operator, so when I make a new biginteger object, I will be able to make it equals with a integer.
This is the main.cpp:
#include <iostream>
#include "bigint.h"
using namespace std;
int main()
{
bigint bela = 15;
cout << "Hello world!" << bela.mennyi() <<endl;
return 0;
}
And this is the biginteger header:
#ifndef BIGINT_H
#define BIGINT_H
#include <vector>
#include <iostream>
class bigint
{
public:
bigint();
void operator=(const int &a);
int mennyi();
protected:
private:
std::vector<int> numarray;
};
#endif // BIGINT_H
And the biginteger.cpp file:
#include "bigint.h"
#include <iostream>
using namespace std;
bigint::bigint()
{
numarray.resize(0);
}
void bigint::operator=(const int &a)
{
int b = a;
if(b >= 0)
{
numarray.resize(0);
while(b!=0){
numarray.push_back(b%10);
b = b/10;
}
}
}
int bigint::mennyi()
{
int ki = 0;
for(int i = (numarray.size())-1; i>=0; i--)
{
ki = ki*10 + numarray[i];
}
return ki;
}
When I start the debugging I get an error saying: conversion from 'int' to non-scalar type 'bigint' requested.
You should implement this constructor:
bigint::bigint(int);

Passing Pointers to Classes in C++

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.

Why is my vector not adding the struct?

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...