Saumay Paul
Saumay Paul

Reputation: 425

Printing values for a range of numbers in C by calling the function

I'm doing a project to calculate a value, ratio here, I want to print the values of ratio in range within 3 to 5.

I want to print the values for the range 3 to 5 such that if ratio< cratio, then ratios in ascending order within range from 3 to 5 should be printed. And if ratio> cratio then ratio should be in descending order within range from 5 to 3 should be printed. Well I want to print only one set of values. I am getting both the set i.e ratios is printed as - 3.05,3.52,4.5,4.5,3.52,3.05 and so on. Where to improve my code so that only temp2,Ls,Lg is shown for the ratio between 3 to 5?

Here's my code:

float Calculate_RG(float r, int d, float cratio) {
    temp2 = d;
    y = ( pg / (p0 - pg) ) * (r / dm);
    LgD = 422 * ( (T * Z * Qg) / P ) * sqrt(y);
    Lg = LgD / temp2;
    Ls = Lg + (temp2 / 12);
    ratio = 12 * (Ls / temp2);

    if (ratio < 3) {
        temp2--;
        Calculate_RG(r, temp2--, ratio);
    } else if (ratio > 5) {
        temp2++;
        Calculate_RG(r, temp2, ratio);
    } else if (ratio > 3 && ratio < 5) {

        if (ratio > cratio) {
         printf("%d\t\t %0.2f\t\t %0.2f\t\t %0.2f\n\n", temp2, Lg, Ls, ratio);
            temp2--;
            Calculate_RG(r,temp2,ratio);
        } else if(ratio < cratio){
            printf("%d\t\t %0.2f\t\t %0.2f\t\t %0.2f\n\n", temp2, Lg, Ls, ratio);
            temp2++;
            Calculate_RG(r,temp2,ratio);            
        }
    }
cratio= ratio;
return cratio;
}

Where am I doing it wrong?? Suggestions Please!!

Upvotes: 0

Views: 488

Answers (2)

ChuckCottrill
ChuckCottrill

Reputation: 4444

You declare your function to return float, but you never return values.

Since C is pass-by-value, you are not changing values of the parameters inside your recursively called Calculate_RG.

What types are temp2, Lg, Ls, and ratio? Did you declare them outside this function, or ignore compiler warnings? What are the initial values of these (global) variables?

You use recursion for most branches of your code. You should have a clear condition (recursion guard) where the recursion should stop. What termination condition (values of ratio, et al) should cause your recursion to stop? Also one typically expects with recursion that you assign the return value from recursion to a variable your calling function either uses or returns.

Add a printf to display the various values just above your conditionals. You might also pass around a 'depth' parameter so that you can print your recursion depth (would help with debugging).

printf("r:%f, d:%d, cratio:%f, temp2:%d, Lg:%d, Ls:%d, ratio:%d\n",
    r,d,cratio,temp2,Lg,Ls,ratio);
if(ratio < 3)

That function would show you which branch(es) your function takes as it traverses down the recursion calls.

Your conditional expressions handle values for ratio,

  • ratio < 3 (should this be <= 3?)
  • ratio > 3 && ratio < 5 (should this be >= 3 or <= 5?)
  • ratio > 5 (should this be >= 5?)

Your conditional would do nothing when ratio ==3 or ==5, so you should change boundary conditions to include these two points, or add an else to detect these cases. (Always handle all domain values, even if you just provide a comment which states you are ignoring those values).

  • ratio == 3
  • ratio == 5

You also do the same comparison for ratio and cratio,

  • ratio > cratio
  • ratio < cratio
  • what do you want to do when ratio == cratio?

When your code decrements temp2 (twice), the first one could be --temp2 or temp2-- (no semantic difference), but the second decrements after you call the function, which has no subsequent effect on your program (temp2 is never used/referenced again). You do not print anything when ratio has a value <3 or >5.

    temp2--;
    Calculate_RG(r, temp2--, ratio);
....
    temp2++;
    Calculate_RG(r, temp2, ratio);

Upvotes: 1

Weather Vane
Weather Vane

Reputation: 34585

You are calling Calculate_RG() recursively but you don't define its variables locally. So they must be global variables and for that reason the code will fail.

Also you don't make any use of the function's return value.

Finally in 21st century never use float unless you can use double.

Upvotes: 0

Related Questions