Like Tree1Likes
  • 1 Post By Elysia

How to write a given function

This is a discussion on How to write a given function within the C++ Programming forums, part of the General Programming Boards category; please help me to write a function with two arguments double x and int n.the function should return a value ...

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    4

    How to write a given function

    please help me to write a function with two arguments double x and int n.the function should return a value of type double and it should perform sum of following series;

    1-[(x)/1!]+[(x^2)/3!]-[(x^3)/5!]+[(x^4)/7!]....upto n terms

    thank you

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You could start by giving us what you have so far and maybe a more specific question. We're not going to do homework.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    4
    Code:
    #include <iostream>
    #include<string.h>
    #include<math.h>
    using namespace std;
    int factorial(int j)
    {
    
    
       if (j==1)
        return 1;
       else
       return j*factorial(j-1);
    }
    double sumsequence(double x,int n){
    int sign=+1;
    double sum=0;
    
    
    int term;
    sum +=1;
    for (int i =1;i<=n;i++)
    {
        sign *= -1;
        term =  (sign * pow(x,i))/factorial(i);
        sum+=term;
    
    
    
    
    
    
    
    
    
    
    
    
    }
    return sum;
    }
    
    
    int main(){
    double a;
    int b;
    
    
    cin >> a>>b;
    sumsequence(a,b);
    
    
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Cleaned up code: indented, removed unnecessary whitespace, replaced C headers with appropriate C++ headers, moved temporaries into loop instead of top of function:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int factorial(int j)
    {
    	if (j == 1)
    		return 1;
    	else
    		return j * factorial(j - 1);
    }
    
    double sumsequence(double x, int n)
    {
    	int sign = 1;
    	double sum = 1;
    	
    	for (int i = 1; i <= n; i++)
    	{
    		sign *= -1;
    		int term = (sign * pow(x, i)) / factorial(i);
    		sum += term;
    	}
    	return sum;
    }
    
    int main()
    {
    	double a;
    	int b;
    	
    	cin >> a >> b;
    	sumsequence(a, b);
    }
    Now, the problems are two-fold. First, note that (sign * pow(x, i)) / factorial(i) may not be an int! You are dividing by a number, so it may be a floating point, so you should make the temporary a double.
    Secondly, the factorial(i). The formula says that the factorial shall be incremented by 2 on each iteration, but you only increment it by one.
    Kartik Watwani likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2014
    Posts
    4
    still not working ....this is what i have now

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    
    int factorial(int j)
    {
        if (j == 1)
            return 1;
        else
            return j * factorial(j - 1);
    }
    
    
    double sumsequence(double x, int n)
    {
        int sign = 1;
        double sum = 1;
        int k=1;
    
    
        for (int i = 1; i <= n; i++)
        {
            sign *= -1;
            double term = (sign * pow(x, i)) / factorial(k);
            sum += term;
            k+=2;
        }
        return sum;
    }
    
    
    int main()
    {
        double a;
        int b;
    
    
        cin >> a >> b;
        sumsequence(a, b);
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You need to be more specific. What is not working? The code is giving correct results for a = 10, b = 2 and b = 3, according to my tests. Are you testing some other inputs?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    4
    it is just returning 0.here is the screenshot of output.
    Name:  Untitled.jpg
Views: 26
Size:  57.0 KB

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    If you implying the "process returned 0," you can ignore that. That has nothing to do with the actual program. You aren't actually printing the result of your function, though. You're just throwing your calculation away.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-04-2012, 12:25 AM
  2. Function Write()
    By samuelmoneill in forum C Programming
    Replies: 3
    Last Post: 04-17-2009, 04:36 AM
  3. help w/ write() function and open() function
    By Landroid in forum C Programming
    Replies: 1
    Last Post: 04-10-2005, 11:38 AM
  4. How do I write this function?
    By Cyb3rT3k in forum C Programming
    Replies: 12
    Last Post: 05-02-2004, 04:23 PM
  5. help me to write this function ^^
    By samidang in forum C Programming
    Replies: 1
    Last Post: 06-06-2002, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21