The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+5 votes
79 views

Consider the code below, defining the functions $f$ and $g$:

f(m, n) {
    if (m == 0) return n;
    else {
        q = m div 10;
        r = m mod 10;
        return f(q, 10*n + r);
    }   
}
g(m, n) {
    if (n == 0) return m;
    else {
        q = m div 10;
        r = m mod 10;
        return g(f(f(q, 0), r), n-1);
    }
}

What does $g(m, n)$ compute, for nonnegative numbers $m$ and $n$?

in Algorithms by Veteran (100k points) | 79 views

1 Answer

0 votes
#include<iostream>
using namespace std;
int g(int m, int n);
int f(int m, int n);
int main()
{
    int m,n;
    m=4321;
    n=4;
    cout<<g(m,n);
}

int f(int m, int n) {
    int q,r;
    if (m == 0) return n;
    else {
        q = m /10;
        r = m %10;
        return f(q, 10*n + r);
    }
}
int g(int m, int n)
{
    int q,r;
    if (n == 0)
        return m;
    else {
        q = m/10;
        r = m%10;
        return g(f(f(q, 0), r), n-1);
    }
}

it will return same number let m=4321,n=4 //Number or digits in m first f() call will reverse then again reverse so it will be same number 

by Active (2.1k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,092 questions
55,239 answers
190,755 comments
85,991 users