Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I write my program calculation:
#include <stdio.h>
int cal(int a, int b){
if (a == 0){
return 1;
}
else if (a == 1){
return b;
}
else{
int c = a / b;
return (cal(a, c) + 1);
}
}
int main(){
printf("Enter values: ");
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", cal(a, b));
return 0;
}
But when I runing my program. I have an error.
So. my program wrong or what problem with programming C, C++?
Thanks for view my question.
I'm fine! Thanks for everyone. This is my fail.
I change my program same:
#include <stdio.h>
int cal(int a, int b){
if (a == 0){
return 0;
}
else if (a == 1){
return b;
}
else if (b == 0){
return 1;
}
else if (b == 1){
return 0;
}
else{
return (cal(a, b/a) + 1);
}
}
int main(){
printf("Enter values: ");
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", cal(a, b));
return 0;
}
int c = a / b;
Will give you a runtime error if you do not check that b is not allowed to be 0.
Add a condition to verify that b is in fact not 0 and if it is, do not perform the division operation.
You run into an infinite recursion. It looks as if you had a termination condition, but the exits without recursion check on a, which will always be the same in your calls.
As is, your algorithm calls cal(a, b) and cal(a, a / b) respectively in odd and even recursion steps. Eventually, you run out of stack.
Revise your algorithm so that deeper recursions progress towards a base state, whcih you should check.
If you want to calculate the logarithm of a to the base b, you should keep the base b constant across calls and sucessively divide a by the base until you reach the case where the value a falls below the base:
int intlog(int a, int b)
{
if (a < b) return 0;
return cal(a / b, b) + 1;
}
Related
https://github.com/mehedihasrifat
Please correct my mistake**
How can I solve this issue?
Did I do something wrong here?**
I have been trying to debug this code but ultimately I can't. Please help me, I'm totally new to this platform.
This picture shows the following code
/*
Written by Mehedi Hasan Rifat
Written on October 2, 2022
*/
#include <stdio.h>
int main()
{
int a, b, t, gcd, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a == 0)
gcd = b;
else if (b == 0)
gcd = a;
else
{
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
}
lcm = (a * b) / gcd;
printf("LCM: %d\n", lcm);
return 0;
}
As jasonharper says in the comment, when you finished the gcd calculation, b will be always zero.
One quick fix is to add
int original_a = a;
int original_b = b;
and calculate lcm using:
lcm = (original_a * original_b) / gcd;
Or just use the __gcd() function in the <algorithm> library.
#include <algorithm>
int main()
{
...
lcm = (a * b) / std::__gcd(a, b);
}
#include <bits/stdc++.h>
using namespace std;
int gcd(long long int a, long long int b){
if(a||b==0){
return 0;
}
else if(b==a){
return a;
}
else if(a>b){
return gcd(a-b,b);
}
else{
return gcd(a,b-a);
}
}
int lcm(long long int a,long long int b){
return a*b/(gcd(a,b));
}
int main(){
long long int answer=1;
for (int i = 2; i<=20; i++) {
answer=lcm(i,answer);
cout<<answer;
}
cout<<answer;
return 0;
}
i wrote this code for problem 5 in project euler. however the output screen is showing nothing and is getting hanged. i put a few debugging cout statements and i understood that in the main function the it is entering the loop but it is not continuing the excution after the call for lcm.
the program is to find the lcm of numbers from 1 to 20. i used the formula llcm= a*b/gcd(a,b). where in gcd also i used the recursive euclidian algorithm. i am not able to trace out the reason for this bug . could anyone help pls.
also if there any suggestions regarding my coding style (indentation, type casting, variable names, algorithm or anything) please point it out. i am beginner so i do not know much regarding c++ and programming styles.
Your program is becoming stuck because of this line:
if (a || b == 0) {
The == operator has higher precedence than ||, so the condition is in fact the same as:
if (a || (b == 0)) {
Which in C(++) is the same as:
if ((a != 0) || (b == 0)) {
That is, if a is non-zero OR b is zero. a will be non-zero straight away, hence your program will always try to divide by zero, which causes problems. I am not sure where you found this version of the algorithm, a cursory search results in a much simpler variant:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, (a % b));
}
}
As for the second part of your question, there are many little (stylistic) issues in your code that I would change. Inconsistent spacing, unnecessary use of long long int (an int would do just fine here) … But for these, I recommend the codereview StackExchange.
As mentioned here: gcd(a,b) = gcd(-a,b) = gcd(-a,-b). However when I use following code, I get different output for input being (-4,-8).
gcd(x,y) gives -4 wheras gcd(abs(x),abs(y)) gives 4.
Can some one help me understand where I am wrong.
int gcd(int a ,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
int main()
{
int x,y;
cin>>x>>y;
cout<<gcd(x,y)<<endl; // gives -4
cout<<gcd(abs(x),abs(y))<<endl; //gives 4
return 0;
}
You're not taking into account that the output range has a plus or minus sign in it which breaks your algorithm, it's asserting that negative numbers and positive numbers are to be treated as the positive integers. Formal set theory in discrete mathematics has a different jargon set for symbols that are incompatible with most programming languages.
Greatest Common Denominator in C++ using recursion
int GCD(int A,int B)
{
if(A==0) return B;
if(B==0) return A;
A=abs(A);
B=abs(B);
if(A>B) return GCD(B,A);
return GCD(B%A,A);
}
Your algorithm cannot cover all positive and negative numbers. Use the code below.
int gcd(int a, int b){
a = abs(a);b = abs(b);
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
I am using Euclid algorithm but it is causing run time error due to stack overflow.
I am unable to calculate HCF of a very large number and a small number
I believe you're writing a function like this:
int hcf(int a, int b){
if (a == 0){
return b;
}
else if (b == 0){
return a;
}
else if (a > b){
return hcf(b, a - b); // this is subtraction
}
else if (a < b){
return hcf(a, a - b); // this is subtraction
}
}
...and you're calling it with something like
int q = hcf(100000000, 1);
Well... Without optimisation that will create 1 billion recursion calls. It's definite that your program will run out of stack capacity.
My personally preferred solution is give up recursive methods and use an iterative one. The code can then be simplified to a single loop:
int hcf(int a, int b){
while(a != 0 && b != 0){
if (a > b){
a = a - b;
}
else{
b = b - a;
}
}
if (a == 0){
return b;
}
else{
return a;
}
}
If you insist on using recursive methods, replace subtraction with modulus.
else if (a > b){
-> return hcf(b, a % b); // this is modulus
}
else if (a < b){
-> return hcf(a, a % b); // this is modulus
}
Correctly implemented algorithm shall use at most log(number) steps, and thus not cause stack overflow. I suppose you use the following algorithm:
gcd(a, 0) = a
gcd(a, b) = gcd(a-b, b)
which looks like this in C++:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(std::max(a, b) - std::min(a, b), std::min(a, b));
}
}
This is not optimal. Instead you shall use the following relation
gcd(a, 0) = a
gcd(a, b) = gcd(b, a mod b)
which looks like this in C++:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
This code will actually take only log(ab) steps, and thus not cause stack overflow
Also you may try to enable optimisation: it should allow to collapse both of the functions call into non-recursive versions (as this is a tail recursion). Note that it is not certain if it will increase speed.
As a matter of caution: be careful with the negative numbers, the % operator works incorrectly for them
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I tried to compile this program manually but it does't seem to work I need someone to show me what the final result is including the execution of the program ( i am looking for the execution table )
int g = 0 ;
int fun1(int a, int b)
{
int m = a % b;
return m;
}
int ggT( int a, int b)
{
g = g + a ;
int Null = 0;
if (b == Null)
return a;
else
return ggT(b , a_mod_b);
}
int main(void)
int a = 7;
int b = 14;
{
int a = 7 ;
int g = ggT( b, a);
b = g;
}
a = g;
return 0;
}
The code isnt compiling because you write things were you shouldnt like you tried to declare a method inside a method, it can be done but is not that way, so the problem is that you declared methods inside other methods
And others errors like tried to use modulus as mod while it is a%b
More or like it will be like that:
int g = 0;
int fun1(int a, int b)
{
int m = a % b;
return m;
}
int ggT(int a, int b)
{
g += a;
if (b == 0)
return a;
else
return ggT(b, a%b);
}
int main()
{
int a = 7;
int b = 14;
int g = ggT(b, a);
b = g;
a = g;
}
It is fixed of errors but i dont know if it is what you want to do or get the answer you want
At final, b and a get g value, wich it is 7, so it is a maximun comun divisor algorithm? Looks like