Reputation: 91
This program is supposed to get a range of values from the user, then it is to add only the even numbers to the range. I have my recursive function running and displaying exactly what it needs to display. I'm just not sure how to do the actual adding of only the even numbers. Could someone help me?
Here is my recursive_function:
int recursive_function(int first_number, int second_number)
{
int even_range_sum = 0;
printf("\n Entering sum function for range %d to %d",
first_number, second_number);
if(first_number <= second_number)
{
if(is_even(first_number) == 1)
{
printf("\n Adding: %d", first_number);
recursive_function(first_number + 1, second_number);
}
else
{
printf("\n Skipping: %d", first_number);
recursive_function(first_number + 1, second_number);
}
}
printf("\n Exiting sum function for range %d to %d ",
first_number, second_number);
printf("with result: %d", even_range_sum);
return even_range_sum;
}
Right now, everything is getting the value zero. Since the variable is redeclared at the beginning of every function and I am not do anything with the variable. Could someone help me know where I am supposed to do the adding. (I can only have one return statement I think) If you need further information just ask. Thanks
Upvotes: 2
Views: 2142
Reputation: 1
Well, I am a beginner in C, so I was not able to correct your code. But I have made one (which works). So you can go through if it helps you correcting your code. Hope it helps.
#include<stdio.h>
void main()
{
int num,num2,temp,sum,num1 ;
printf("enter range -from:\n ");
scanf("%d", &num1);
printf("to:");
scanf("%d", &num2);
if(num1>num2)
{
temp=num1;
num1=num2;
num2=temp;
}
sum=sum_eve(num1,num2);
printf("\n%d", sum);
}
int sum_eve(int m,int n)
{
int sum;
if(m%2!=0)
{
m=m+1;
}
if(n%2!=0)
{
n=n-1;
}
if(m>n)
{
return(0);
}
if(n==m)
{
return (m);
}
else
{
sum = n + sum_eve(m,n-2);
return (sum);
}
}
Upvotes: 0
Reputation: 2828
You had forgotten to carry forward the results of each prior sum. The version below works as a complete program and includes a test. The "is_even" function was missing but you probably had that listed elsewhere; it was simple to add using the modulo operator at your "is_even" conditional.
Hope this helps.
#include <stdio.h>
int recursive_function(int first_number, int second_number);
int main(int argc, const char * argv[])
{
recursive_function(1, 10);
return 0;
}
int recursive_function(int first_number, int second_number)
{
// Base Case:
int even_range_sum = 0;
printf("\n Entering sum function for range %d to %d", first_number, second_number);
if (first_number <= second_number) {
if( first_number%2==0 ) {
printf("\n Adding: %d \n", first_number);
even_range_sum += first_number;
} else {
printf("\n Skipping: %d", first_number);
}
even_range_sum += recursive_function(first_number + 1, second_number);
}
printf("\n Exiting sum function for range %d to %d \n", first_number, second_number);
printf("with result: %d \n", even_range_sum);
return even_range_sum;
}
Upvotes: 1
Reputation: 48745
Seems like homework so I won't post a full solution. Fill in the <??>
's
Are you allowed to make a helper function? Then your helper function could have 3 arguments. The sum, the first even number and the last number to be in the range (not need to be even).
The original function would then add 1 to the start value if it's odd so that it turns in to the very first even number. Since any number that is 1 after arithmetic anding with 1 is odd, you can do this:
int even_summer(int range_start, int range_end)
{
return even_summer_recursion(0, range_start & 1 ? range_start + 1 : range_start, range_end);
}
int even_summer_recursion(int acc, int current_even, int range_end)
{
if( current_even > range_end )
return acc; // return the accumulated result
else // add the current to the acc and increment current to the next even value
return even_summer_recursion( <??>, <??>, range_end);
}
If recursion is not a requirement you could easily make yourself a iterative version by making the first even number in the same way as a start value in a for loop and had the accumulator as a local variable in the function.
int even_summer(int range_start, int range_end)
{
int acc = 0;
for(int current_even = ( is_even(range_start) ? range_start : range_start + 1);
current_even < range_end;
current_even += <??>) {
acc += <??>;
}
return acc;
}
Here I actually used you is_even
function.
Upvotes: 0
Reputation: 2333
You need to capture the return variable from your recursive calls, in even_range_sum
, and then in the even case, add that number in.
if(is_even(first_number) == 1)
{
printf("\n Adding: %d", first_number);
even_range_sum = recursive_function(first_number + 1, second_number);
even_range_sum += first_number;
}
else
{
printf("\n Skipping: %d", first_number);
even_range_sum = recursive_function(first_number + 1, second_number);
}
Since the recursive call is the same in both cases, you could factor that out of the if as well:
even_range_sum = recursive_function(first_number + 1, second_number);
if(is_even(first_number) == 1)
{
printf("\n Adding: %d", first_number);
even_range_sum += first_number;
}
else
{
printf("\n Skipping: %d", first_number);
}
Upvotes: 0
Reputation: 361625
The logic needs two changes:
If the first number is even then you want to add it to the sum, otherwise do nothing. That's what should happen in the inner if/else statements. Currently your code does the same thing in both parts (aside from differing printouts).
No matter what, you should then call your function recursively to add the rest of the sum on. This happens outside the if/else, because you want to add the rest of the range whether or not the first number is even.
And importantly, you need to use the return value. Your function returns a (partial) sum, so you need to do something with its return value. If you just call it but ignore its return value then nothing has happened.
Result:
if (first_number <= second_number)
{
if (is_even(first_number))
{
printf("\n Adding: %d", first_number);
even_range_sum += first_number;
}
else
{
printf("\n Skipping: %d", first_number);
}
even_range_sum += recursive_function(first_number + 1, second_number);
}
Upvotes: 3