HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Recurrence, recursive function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
recursivefunctionrecurrence

Problem

Type a number \$n\$. Then calculate \$f(n)\$, where

$$ f(n) =
\begin{cases}
n-10 & \text{if}~ n > 100\\[1.5ex]
f(f(n + 11)) & \text{otherwise}
\end{cases}
$$

The output must show all the computations, such as
$$f(99) = f(f(110)) = f(100) = f(f(111)) = f(101) = 91$$

Is there a shorter and better way I can solve this?

#include 
using namespace std;

int main(){
    int i=0, n, x, k;

cin>>n;

if(n>100) {
cout100){

        x=0;
        k=0;
        i=i-1;
        n=n-10;

        while(x100){

        x=0;
        k=0;
        i=i-1;
        n=n-10;

        if(i>=0){

        while(x<=i){                         
            x++;
            cout<<"f(";
        }
        cout<<n;
        while(k<=i){
            k++;
            cout<<")";

        }cout<<"=";

        goto loop;
    }
         break;
    } 
    }
    }cout<<n;
 }

Solution

There's no way to tell this in a friendly manner: your indentation is abysmal.

Let's fix that up first. Note that there's plenty left to improve, like giving your operators some space, but I'll leave that as an exercise for yourself. Keep in mind space is cheap and readability is very important in code. If you can add readability by using some extra newlines or space, do it. But only if it helps. Don't throw them needlessly around just for the sake of it.

#include 
using namespace std;

int main(){
    int i=0, n, x, k;
    cin>>n;
    if(n>100) {
        cout100){
                    x=0;
                    k=0;
                    i=i-1;
                    n=n-10;
                    while(x100){
                x=0;
                k=0;
                i=i-1;
                n=n-10;
                if(i>=0){
                    while(x<=i){                         
                        x++;
                        cout<<"f(";
                    }
                    cout<<n;
                    while(k<=i){
                        k++;
                        cout<<")";
                    }
                    cout<<"=";
                    goto loop;
                }
                break;
            } 
        }
    }
    cout<<n;
 }


Much better, but plenty left to improve.

Namespaces

using namespace std; is considered bad practice. Short code is not a requirement in C++, clear code is preferred. It's a thing commonly taught to new C++ programmers because it's 'easier', but it will royally bite you in the behind when conflicts arise.

goto

I'm not going to re-iterate how evil goto is, but please take a look at this well-written answer by 200_success. Basically, one should only use it in C++ if you know exactly what you're doing and there's no better alternative. There usually is a better alternative!

Be consistent!

x++ is usually reserved for for loops. Doing it in while loops indicates you're using the wrong tool for the job. You use x++, x += 1 and x = x + 1 or the negative version of it all over the place. There usually is no reason to write i = i -1, i -= 1 will do.

But the worst of it is:

Your code is simply incomprehensible. It takes too long to understand what it does and all the re-declaring of variables makes it hard to track what's going on. One-letter variables are usually not a good idea, iterators in for loops are an acceptable exception.

You have 6 while loops in your main function. 6!. I'd expect at least 3 different functions to keep this readable, with sensible returns passing the data along and there can't be a good reason to have this many while loops.

So?

Write down what you want to do as a flow-chart. Simplify it as much as possible. Implement that. The result will be half as complicated and twice as readable, at least. What you currently have is the result of a design flaw. I'm afraid a re-design, with this answer in the back of your head, is the only sensible solution here.

Code Snippets

#include <iostream>
using namespace std;

int main(){
    int i=0, n, x, k;
    cin>>n;
    if(n>100) {
        cout<<"f("<<n<<")=";
        n=n-10;
    }
    else {
        cout<<"f("<<n<<")=";
        while(n<=100){
            loop:       
                x=0;
                k=0;
                i++;
                n+=11;
                while(x<=i){                         
                    x++;
                    cout<<"f(";
                }
                cout<<n;
                while(k<=i){
                    k++;
                    cout<<")";  
                }
                cout<<"=";  
                if(n>100){
                    x=0;
                    k=0;
                    i=i-1;
                    n=n-10;
                    while(x<=i){                         
                        x++;
                        cout<<"f(";
                    }
                    cout<<n;
                    while(k<=i){
                        k++;
                        cout<<")";
                }
                cout<<"=";
            }
            if(n>100){
                x=0;
                k=0;
                i=i-1;
                n=n-10;
                if(i>=0){
                    while(x<=i){                         
                        x++;
                        cout<<"f(";
                    }
                    cout<<n;
                    while(k<=i){
                        k++;
                        cout<<")";
                    }
                    cout<<"=";
                    goto loop;
                }
                break;
            } 
        }
    }
    cout<<n;
 }

Context

StackExchange Code Review Q#141640, answer score: 12

Revisions (0)

No revisions yet.