here is my code, when the input is 21,15 I got the output is 0. what I expected is 3. the return value of the function divisor seems return a wrong value.
#include<stdio.h>
int divisor(int a, int b){
//when b is 0, got the GCD
if(b==0){
printf("when b is 0, a=%d\n", a);
return a;
}
else{
//recursion
printf("the input a=%d,b=%d\n", b, a%b);
divisor(b, a%b);
}
// return res;
}
int main(void){
int a, b;
scanf("%d,%d", &a, &b);
int r = divisor(a, b);
printf("%d", r);
return 0;
}
You can try this, but it will only work for positive integers.
int divisor(int a, int b){
if (a * b == 0){
return a+b;
}
return divisor(b % a, a % b);
}
Related
How can you use this function pointer declaration?
int (* get_function(char c)) (int, int);
I have three functions int function_a(int a, int b), int function_b(int a, int b) and int function_c(int a, int b). I want to use the above function pointer to call one of my functions conditionally based on c.
Here is an example:
#include <stdio.h>
int function_a(int a, int b)
{
printf("Inside function_a: %d %d\n", a, b);
return a+b;
}
int function_b(int a, int b)
{
printf("Inside function_b: %d %d\n", a, b);
return a+b;
}
int function_c(int a, int b)
{
printf("Inside function_c: %d %d\n", a, b);
return a+b;
}
int function_whatever(int a, int b)
{
printf("Inside function_whatever: %d %d\n", a, b);
return a+b;
}
int (* get_function(char c)) (int, int)
{
switch(c)
{
case 'A':
return function_a;
case 'B':
return function_b;
case 'C':
return function_c;
}
return function_whatever;
}
int main(void) {
get_function('B')(3, 5);
return 0;
}
get_function('B') returns a function pointer to function_b and get_function('B')(3, 5); also calls that function.
https://ideone.com/0kUp47
Liechtenstein in c programming always return infinite loop this is my code i try many solution and i try to stock variables and use pointers but always i have the infinite loop i think it's because the 3 recursive calls but in the doc of Liechtenstein algorithm i found this implementation
#include "distance.h"
#include "sequence.h"
#include "string.h"
#include <stdarg.h>
int max(int a, int b){
if (a>b) return a;
return b;
}
float min(float a,float b, float c){
if((a<b) && (a<c)) return a;
if((c<b) && (c<a)) return c;
return b;
}
float dif(int a,int b){
int d;
d = a-b;
if((a==32)||(b==32)) return 1.5; //verification d'espaceC
if(d==0.0) return d;
if((abs(d)==17.0) || (abs(d)==6.0)) return 1.0;
return 2;
}
float distance_D1(SEQUENCE S1, SEQUENCE S2){
float d = 0.0; int a,b; int m; int i;
m = max(S1.l,S2.l);
for(i=0;i < m; i++){
a = S1.tc[i];
b = S2.tc[i];
d = d + dif(a,b) ;
printf("%.1f\n",d);
}
return d;
}
float DistanceMinRec(char* a,char* b,int n,int m){
printf("%s \n",a);
printf("%s \n",b);
printf("%d \n",n);
printf("%d \n",m);
float l,k,j,d;
if (m <= 0) return n;
// If second string is empty, the only option is to
// remove all characters of first string
if (n <= 0) return m;
// If last characters of two strings are same, nothing
// much to do. Ignore last characters and get count for
// remaining strings.
if (a[m] == b[n]) {
return DistanceMinRec(a, b, m-1, n-1);
}
// If last characters are not same, consider all three
// operations on last character of first string, recursively
// compute minimum cost for all three operations and take
// minimum of three values.
l=DistanceMinRec(a, b, m, n-1)+dif(a[m],b[n-1]);
k=DistanceMinRec(a, b, m-1, n)+dif(a[m-1],b[n]);
j=DistanceMinRec(a, b, m-1, n-1)+dif(a[m-1],b[n-1]);
d= min (l , // Insert
k, // Remove
j // Replace
);
return d;
}
the main file
int n=strlen(S1.tc);
int m=strlen(S2.tc);
char* X = S1.tc;
char* Y = S2.tc;
// char* X = "sunday";
// char* Y = "saturday";
//affiche_sequence(S1);
//affiche_sequence(S2);
d = DistanceMinRec(X,Y,n,m);
printf("Distance entre %s et %s = %.1f\n", argv[1],argv[2],d);
exit(0);
i think that the flush of variable is the problem i comment the block of printf in recursive function and it work but im not sure that is the correct output
I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}
#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
when I am giving input as -1 1 0 my output should be 1 but it is giving 0
why?
it is all about adding three numbers
int calsum (int x,int y,int z)
{
return ;
}
Your function is declared and defined to return an int, but your return statement is expressionless. It's a language constraint violation.
The behavior of your program is undefined. Funny results is a possible outcome in this case.
Update your calsum function as below. You are assigning d=1 in else part but not returning it.
int calsum (int x,int y,int z){
int d;
d=x+y+z;
if(d>2)
return d;
else
return 1;
}
While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}