I am new to recursion so I am trying to write a simple program that adds to the nth number of the series 1/n. So if the user enters n = 4, the program will add 1 + 1/2 + 1/3 + 1/4. My program keeps outputting that the sum of the series is 0. Can someone please explain what I am doing wrong? I'd appreciate the help. Here is my code:
#include <stdio.h>
double sum(double n);
int main() {
double n;
double total;
printf("Enter a positive integer greater than 0: ");
scanf("%lf", &n);
total = sum(n);
printf("Sum: %lf", total);
return 0;
}
double sum(double n) {
if (n == 1)
return 1;
else
return ((1 / n) + sum(n - 1));
}
The problem is in the definition of this function:
double sum(int n) {
if (n == 1)
return 1;
else
return ((1 / n) + sum(n - 1));
}
n is int so 1/n will be always evaluated as int since both 1 and n are integers. Thus 1/n is always 0 for each n>1.
The solution would be to define n as double :
double sum(double n) {
if (n <= 1.0)
return 1.0;
else
return ((1.0 / n) + sum(n - 1.0));
}
Check your base case. sum(1) should return 1.
Also, an int divided by an int returns an int. Use a floating point number in your division.
It should be
double sum(double n) {
if (n == 1)
return 1.0;
else
return ((1.0 / n) + sum(n - 1));
}
1/n will always return 0 since 'n' acts as an integer value and not a float value even though the datatype is double as the value assigned to it is of type integer.
Related
I am trying to make a program in C that uses a recursive function to calculate the factorial for Euler's number, and sends that data to main where the recursive function is to stop once two successive values have a difference of 0.0000001, however i cannot seem to get my program to work as it keeps returning -inf. Anyone know what im doing wrong?
EDIT: With this current code i can get the program to print result as 0.5, but it does not increment n past 2.
#include <stdio.h>
double factorial(double n);
int main ()
{
double n;
double sum = 0;
double last;
double result = 0;
for (n = 1; result <=0.0000001; n++)
{
last = sum;
sum = factorial(n);
result = (1 / last) - (1 / sum);
printf("result is %lf\n", result);
}
printf("result is %lf\n", result); // troubleshooting
return 0;
}
double factorial(double n)
{
if (n > 0)
return ( n * factorial(n-1));
else
return 1;
}
On the first iteration in main:
sum == 0
last = sum; => last == 0
result = (1 / sum) - (1 / last); => 1 / last == 1 / 0 == inf
Then you subtract (1 / last), which is inf, from (1 / sum), and get negative infinity.
Also, the loop never iterates more than once because you return result on the very first iteration.
I am getting negative and positive result : when I enter(n value odd) odd term produces even or and even term produces odd value. I have already made function for factorial it works fine.
#include <stdio.h>
#include <math.h>
#define PI 3.14159f
int factorial(int n);
float sine(float , int);
int i;
void main(){
float degree;
float radian;
float result;
int n;
printf("Enter the angle in degree: ");
scanf("%f",°ree);
printf("Enter the iteration: ");
scanf("%d",&n);
radian = degree * PI / 180;
result = sine(radian,n);
printf("%d",factorial(n));
printf("\n");
printf("sin%.2f = %.3f",degree,result);
}
int factorial(int n)
{
if(n==0)
return 1;
else if (n==1)
return 1;
else
return (n*factorial(n-1));
}
float sine(float an, int n)
{
if (an==0)
return 0;
else if(n>=0)
if(n%2==1)
return (sine(an,n-2) - pow(an,n)/factorial(n)) * pow(-1,n);
else
return (sine(an,2*n-1) - pow(an,2*n+1)/factorial(2*n+1)) *-1 ;
}
float sine(float an, int n)
{
if (an == 0)
return 0;
The above condition returns 0 for sin(0) so it is of no use in rest of the recursion and it works fine.
else if(n >= 0)
if(n%2 == 1)
return (sine(an,n-2) - pow(an,n)/factorial(n)) * pow(-1,n);
else
return (sine(an,2*n-1) - pow(an,2*n+1)/factorial(2*n+1)) *-1 ;
Lets see where this part of your function goes, by substituting the value of n:
Suppose we are start with degree = 30 and number of iterations = 3
Then:
n = 3;
n is odd so function returns:
((sine(an, 3-2) - (float)pow(an, 3) / factorial(3)) * -1);
n = 1;
n is again odd so function returns:
((sine(an, 1-2) - (float)pow(an, 1) / factorial(1)) * -1);
n = -1;
This time n < 0 so if-else conditions are skipped and some garbage value is returned because you did not tell your program what to return at n = 0
So you need your sine-function to return a default value when n = 0
pow(-1,n) will always return -1 for odd values of n, and 1 for even values of n. So the sign of output of sine() function is not changing alternately like in the series.
Answering to the comment: Why the sign seems to change for odd or even,
In your code values of n are skipping the even numbers, instead of decreasing n by 1 per call, you are passing only odd numbers.
return (sine(an,n-2) - pow(an,n)/factorial(n)) * pow(-1,n);
so what you get in the end is a sum of negatives; And when you start with a even value of n like 2, then sine() is called with odd value of n the second time which is (2*n-1) which again returns a sum of negatives.
Heres what you can do:
float sine(float an, int n)
{
if (an == 0 || n == 0)
return 0; //to end the recursion when number of iterations are finished
else
return -1*pow(-1,n)*pow(an,2*n-1)/factorial(2*n-1) + sine(an, n - 1);
}
// `-1*pow(-1,n)` returns a negative term for even value of n, and postive term for odd value of n as required. You dont need separate if-else for that
I have to write a program that will find a square root using the while loop. I was given this new_guess = (old_guess + (n / old_guess)) / 2.0; but I dont fully understand what to do with it, this is what I have:
int main(void)
{
double n, x, new_guess, old_guess, value;
printf("Enter a number:");
scanf("%lf", &n);
x = 1.00000;
while (new_guess >= n) {
new_guess = (old_guess + (n / old_guess)) / 2.0;
printf("%10.5lf\n", fabs(new_guess));
}
return 0;
}
x is the initial guess. Im really lost on how to do it. This is C also. I know its really wrong but I really dont understand how to make it start because when I enter a number it just stop right away.
Your program has undefined behavior because both new_guess and old_guess are uninitialized when you enter the loop.
The condition is also incorrect: you should stop when new_guess == old_guess or after a reasonable maximum number of iterations.
Here is a modified version:
#include <math.h>
#include <stdio.h>
int main(void) {
double n, x;
int i;
printf("Enter numbers:");
while (scanf("%lf", &n) == 1 && n >= 0.0) {
x = 1.0;
/* Using a while loop as per the assignment...
* a for loop would be much less error prone.
*/
i = 0;
while (i < 1024) {
double new_guess = (x + (n / x)) / 2.0;
if (new_guess == x)
break;
x = new_guess;
i++;
}
printf("%g: %.17g, %d iterations, diff=%.17g\n",
n, x, i, sqrt(n) - x);
}
return 0;
}
Given the start value, the number of iterations grows with the size of n, exceeding 500 for very large numbers, but usually less than 10 for small numbers. Note also that this algorithm fails for n = 0.0.
Here is a slightly more elaborate method, using the floating point break up and combine functions double frexp(double value, int *exp); and double ldexp(double x, int exp);. These functions do not perform any calculation but allow for a much better starting point, achieving completion in 4 or 5 iterations for most values:
#include <math.h>
#include <stdio.h>
int main(void) {
double n, x;
int i, exp;
printf("Enter a number:");
while (scanf("%lf", &n) == 1 && n >= 0.0) {
if (n == 0) {
x = 0.0;
i = 0;
} else {
frexp(n, &exp);
x = ldexp(1.0, exp / 2);
for (i = 0; i < 1024; i++) {
double new_guess = (x + (n / x)) / 2.0;
if (new_guess == x)
break;
x = new_guess;
}
}
printf("%g: %.17g, %d iterations, diff=%.17g\n",
n, x, i, sqrt(n) - x);
}
return 0;
}
Is there any way to round systemGuess up. In this case the outcome of systemGuess is 5.5 I want it to be 6 how do I do this?
See code below:
int main(void){
int systemGuess = 0;
stystemGuess = (10 - 1)/2 + 1;
printf(" %d ", stystemmGuess);
}
Use floating point division and ceil:
stystemGuess = ceil((10 - 1)/2.0) + 1;
If you want to round 0.4 down, use round instead.
OP wants to perform an integer division with the result rounded-up.
// If the quotient fraction > 0, return next larger number.
unsigned udiv_ceiling(unsigned n, unsigned d) {
return (n + d - 1)/d;
}
// If the quotient fraction >= 0.5, return next larger number.
unsigned udiv_nearest_ties_up(unsigned n, unsigned d) {
return (n + d/2)/d;
}
stystemGuess = udiv_ceiling(10 - 1, 2) + 1;
// or
stystemGuess = udiv_nearest_ties_up(10 - 1, 2) + 1;
Additional code needed to handle negative numbers and in corner cases, protect against n + d - 1 overflow.
You can use
systemGuess = (10 - 1)/2.0 + 1 + 0.5;
The problem is that you do integer calculation.
So e.g. 9/2 is 4. If you use 9/2.0 you have floating point division, which gives you 4.5. Adding 0.5 in the end gives you 6.0 instead of 5.5, so when storing it in systemGuess, you get 6 instead of 5.
Integer division in C truncates toward 0, so if you do the math on the other side of 0 (i.e., on negative numbers), it will "round up". You might do this by subtracting an amount from the dividend and adding half that amount back to the result:
int main(void)
{
int systemGuess = 0;
//systemGuess = (10 - 1)/2 + 1;
systemGuess = (10 - 1 - 20)/2 + 1 + 10;
printf(" %d ", systemGuess);
}
Probably in your real program there is a more elegant way to make this happen.
Here you go:
#include <stdio.h>
#include <stdlib.h>
int divide(int x, int y);
int main(void){
int systemGuess = 0;
int x = 10-1;
int y = 2;
systemGuess = divide(x,y) + 1;
printf(" %d ", systemGuess);
}
int divide(int x, int y) {
int a = (x -1)/y +1;
return a;
}
How would you write a recursive function that calculates the average of the elements of an array?
Here's what I have so far:
int media(int numeros[], int i, int n) {
if (i == n-1) return numeros[i]/n;
return numeros[i]/n + media(numeros, i + 1, n);
}
But it doesn't work, and I think it's because of the line: if (i == n-1) return numeros[i]/n;
How about this:
double media(int numeros[], int i, int n) {
if (i == n - 1) {
return numeros[i];
}
if (i == 0)
return ((numeros[i] + media(numeros, i + 1, n)) / n);
else
return (numeros[i] + media(numeros, i + 1, n));
}
What you're doing is dividing by the number of elements each call - doing that will give you the incorrect average. The code above does what you were doing, but as its about to return the result (when i==0) it'll calculate the actual average based on the total of all the elements
The problem is that if you divide each element, because of rounding (to integer), you'll quite probably get an incorrect answer (integer divide by integer returns integer).
For {1,2,3,4}, we'll divide each element by 4 - 1/4 + 2/4 + 3/4 + 4/4, but each division returns an integer, so all of them are 0 and the result is 0.
So, either make it double: (but it's still better to also divide at the end, because of rounding)
double media(int numeros[], int i, int n){
if (i == n-1) return 1.0*numeros[i-1]/n;
return 1.0*numeros[i]/n + media(numeros, i + 1, n);
}
Or divide at the end: (either the way Sean Landsman suggested, or like below)
int media(int numeros[], int i, int n){
if (i == -1) return media(numeros, 0, n)/n;
if (i == n-1) return numeros[i-1];
return numeros[i] + media(numeros, i + 1, n);
}
Caller:
int media(array, -1, n);