Why it doesn't show me the greatest common divison? - c++

When i try to start the program it doesn't work and doesn't show me any error. Why?
#include <iostream>
using namespace std;
int main()
{
unsigned a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
{
while(a!=b)
{
if(a>b)
(a==a-b);
else
(b==b-a);
}
}
cout<<"cmmdc=",a;
return 0;
}

Replace a==a-b with a=a-b.
Replace b==b-a with b=b-a.
The operator == is comparison, it doesn't modify its arguments. The operator = is assignment, it modifies its left argument to the value of its right argument.
Replace cout<<"cmmdc=",a with cout<<"cmmdc="<<a, otherwise a won't be printed.

Even after changing the ==s to =s, you'll get an infinite loop if any but not both of a and b is 0. To avoid that, use this loop instead:
while (b != 0) {
const unsigned olda = a;
a = b;
b = olda % b;
}
// GCD is now in a.

It is the simplest way to find gcd of two numbers :
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a,b;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"GCD = " << __gcd(a,b);
}

Related

Using --a vs a-1 in recursion [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 2 years ago.
I was trying to calculate a factorial using recursion like this:
#include <iostream>
using namespace std;
int factorial(int a)
{
if(a == 0)
{
return 1;
}
return a*factorial(--a);
}
int main()
{
int a;
cin >> a;
cout << factorial(a) << endl;
return 0;
}
and it wasn't working. Then, I made a small change:
#include <iostream>
using namespace std;
int factorial(int a)
{
if(a == 0)
{
return 1;
}
return a*factorial(a-1);
}
int main()
{
int a;
cin >> a;
cout << factorial(a) << endl;
return 0;
}
... and it started working!
The problem is that I don't see any difference between these codes: Why didn't it work in the first code?
In your first code sample, the following line has undefined behaviour:
return a * factorial(--a);
This is because there is nothing in the C++ Standard that dictates whether or not the 'old' or 'new' (decremented) value of a is used to multiply the return value of the factorial function.
Compiling with clang-cl gives the following:
warning : unsequenced modification and access to 'a' [-Wunsequenced]
In your second code sample, there is no such ambiguity, as a is not modified.

Recursive (sum of even numbers from 2 to (2 times n) ex: input : n=6, output : 2+4+6

Can anyone fix this code to make it right?
I think it's almost right but the last number is correct number but followed by random number.
#include <iostream>
using namespace std;
int jumlah(int a, int b){
if(a*2==b){
cout<<b;
}else{
cout<<b<<"+";
cout<<jumlah(a, b+2);
}
}
int main(){
int a, b;
b=2;
cin>>a;
jumlah(a, b);
return 0;
}
Your function never returns anything, so printing the result of the recursion is undefined.
Remove the result from the function and recurse without printing.
void jumlah(int a, int b){
if(a*2==b){
cout<<b;
}else{
cout<<b<<"+";
jumlah(a, b+2);
}
}
Your code has undefined behavior since the function does not return anything.
Change it to:
int jumlah(int a, int b){
if ( a*2 == b){
return b;
}
cout << b << "+";
return jumlah(a, b+2);
}
and change the call in main to:
cout << jumlah(a, b);

Curious behaviour of bitwise AND

I was coding and the following code doesn't give the desired output. pos&1 is supposed to return the remainder when pos is divided by 2. When I replace pos&1 by pos%2 everything works just fine. What could be the problem?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"blue\n";
}
else
cout<<"red\n";
return 0;
}
1==0 takes more precedence than pos&1. Try if((pos&1)==0){

C++ template help, using a template for integrations

Hi i'm following a tutorial and video i found online , im trying to make a template to preform a numerical integration of a function where a user can decide which form of integration to preform, im trying to keep it to one file as not to use headers and not use massive ammounts of loops , the code for the first integration works fine on its own but when i run it through a template i get the wrong answer and the same value 1.9147e-307 for every input what am i doing wrong?
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include<conio.h>
using namespace std;
//declared function
double F(double X)
{
double f;
f = (X*X);
return f;
};
double unifRand()
{
return rand() / double(RAND_MAX);
};
template<typename T> class INTG{
private:
T a;
T b;
T n;
public:
INTG(T a, T b,T n){
INTG::a = a;
INTG::b = b;
}
~INTG(){}
T MC() {
// some code
return ans;}
T SIMPC(){ // Simpson integration code here
return a+b+n;
}
};
int main() {
double a,b,mc,simp,ans;
int OP,n;
cout<<"Enter 1 for Monte Carlo Integration , Enter 2 for Composite Simpson Integration, enter 3 for trapezoidal int...."<<endl;
cin>>OP;
clock_t start = clock();
if (OP == 1) {
cout<<"Enter lower limit of integration"<<endl;
cin>>a;
cout<<"Enter upper limit of integration"<<endl;
cin>>b;
cout<<"Enter number of iterations"<<endl;
cin>>n;
ans = INTG<double>::MC(a, b, n);
INTG<double> MyCalc(a,b,n);
cout<< ans <<endl;
//mc = INTG::MC(a, b, n);
getch();
}
}
ans is never assigned a value. That would account for your 1.9147e-307.
Did you intend
ans = MyCalc.MC();
before the cout ?
Also
INTG(T a, T b,T n){
INTG::a = a;
INTG::b = b;
}
Is better described as
INTG(T a, T b, T n):a(a),b(b),n(n) {}
Initializing instead of assigning, and remembering n.
So the calculation sequence would be
INTG<double> MyCalc(a,b,n);
ans = MyCalc.MC();
if (OP = 1) {
...should be...
if (OP == 1) {

Code Crashes Immediately After Running

Even at the bare minimum of 10 numbers to input, I get no errors but my code crashes immediately on running. I was also wondering, what should I do if I have a question similar to another question that I've already asked, but on another new problem?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime==true) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
}
}
I get no errors but the compiled code crashes immediately.
I changed it to
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a.push_back(2);
for (double i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
return a.size();
}
}
I addressed all of your problems. It still returns no errors and still crashes.
What makes you think you can do this?
vector<int> a;
a[1]=2;
vector<int> a;
a[1]=2;
You can't access a[1] until you've reserved space for it. You should probably use a.push_back(2) to append 2 to the end of a.
You have declared primer to return int, yet it returns nothing. Either make it void or return the number of primes.
i/a[ii]==floor(i/a[ii]) isn't going to do what you expect. i/a[ii] performs integer division. You should cast i to double before dividing.
if (prime==true) can be changed to simply if (prime), no need to compare a boolean to true.
Please improve your coding style. Use proper indentation and more commonly used variable names: i, j, k instead of i, ii, iii.
Here is another bug:
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
return a.size();
}
My understanding is that you can only return once from a function, main included. The execution will not loop here because of the return statement.
Did you really want a return statement inside a for loop?