Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Power and Factorial (http://www.go4expert.com/forums/power-and-factorial-t22739/)

nasnoma 17Jul2010 01:36

Power and Factorial
 
My program basically asks the users for a value (s) then calculates and displays the First, Middle, Last, and Final. The users have a choice to enter the value either in integer, real or character. If the user enters it in real or character, it will be converted into integer value before the required value is computed. I have written the power , factorial and the sigma functions which I will use in calculating the first, middle and the last. Can somebody assist in checking my codes to see if its correct and show how I will those functions to calculate the first and the last which a bit more complicated?

First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!
Middle = n sigma i = 1 sqrt(i/x)
last = 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - ....X^n/n!
Final = first/last + middle

Code:

#include<iostream>
#include<cmath>
using namespace std;


int powerfunc (int x, int n)
{
  int x, n;
  int  p, i;
while (i <=n)
 {
  p = p * x;
  i = i + 1;
  return p;
 }
}

int factorial (int n)
{
  int i, n;
  int f = 1;
for (i = 1; i <= f; i++)
  {
      f *= i;
    return f;
    }
}

int sigma(int x, int n)
{
  int i = 1, sum = 0;
  int x, n;
while (i <= n)
  {
    sum += i *  i;
      i++;
    sum = sqrt(sum/x)
      return sum;
    }
}

int main()
{
 
  int x, n;
  int first, middle, last, final;

do {
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;

 middle = sigma(x, n)
 final = fist/(last + middle);

 cout << “The value for first is << first << endl;
 cout << “The value for middle is << middle << endl;
 cout << “The value for last is << last << endl;
 cout << “The value for final is << final << endl;


cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;

} while (ans == 'y' || ans == 'Y');
..
getch ();





return 0;
}


virxen 17Jul2010 02:59

Re: Power and Factorial
 
check the functions again all are wrong.

hint: return is placed in wrong places in all functions.

Code:

int factorial (int n){
  int f = 1;
for (int i = 1; i <=n; i++) f *= i;
    return f;//outside the loop ,last command in function
}

and for calculating the formulas you want
hint: (-1)^n ,n=0,2,4,6..... =1
(-1)^m,m=1,3,5,...=-1
X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!---->(-1)^0*X^1/1! +(-1)^1* X^3/3! +(-1)^2* X^5/5! +(-1)^3* X^7/7! + (-1)^4*X^9/9! +....X^n/n!

nasnoma 21Jul2010 21:42

Re: Power and Factorial
 
Can you Please put the hint in code so that I can have a good idea on how to solve the problem? I am grateful that you assist. I will be happy if you can do more

nasnoma 21Jul2010 22:34

Re: Power and Factorial
 
Quote:

Originally Posted by virxen (Post 70317)
check the functions again all are wrong.

hint: return is placed in wrong places in all functions.

Code:

int factorial (int n){
  int f = 1;
for (int i = 1; i <=n; i++) f *= i;
    return f;//outside the loop ,last command in function
}

and for calculating the formulas you want
hint: (-1)^n ,n=0,2,4,6..... =1
(-1)^m,m=1,3,5,...=-1
X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!---->(-1)^0*X^1/1! +(-1)^1* X^3/3! +(-1)^2* X^5/5! +(-1)^3* X^7/7! + (-1)^4*X^9/9! +....X^n/n!

Is it gonna be something like this when I write the code for the above hint?
Code:

int n = 0,1,2,4,6.... =1;
int m = 135,...=-1;

pow((-1),0)*pow(x,1)/factorial (1) + pow((-1),1)*pow(x,3)/factorial(3) + pow((- 1),2)*pow(x,5)/factorial(5) + pow((-1),3)*pow(x,7)/factorial(7) + pow((-1),4)*pow(x,9)/factorial(9) + .........pow(x,n)/factorial(n);

Here is my code which I improve a bit
Code:


#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;


int factorial(int n)    // factorial function
{
if (n<=1)
return(1);
else
n=n*factorial(n-1); // notice we just keep calling this over until we get to 1!
return(n);
}

int first (int n)  //  function for first
{
 
  while (n <= 1)
  {
    f = f + pow (x,n) / factorial (int n);
      return (f);
      n-=2;
    }
}

int last (int n)      //  function for last
{
 
  while (n <= 1)
  {
    l = l + 1 - pow (x,n) / factorial (int n);
      return (l);
      n-=2;
    }
}

int sigma(int x, int n)
{
  int i = 1, sum = 0;

while (i <= n)
  {
    sum += i *  i;
      i++;
    sum = sqrt(sum/x)
      return sum;
    }
}

int main()
{
 
  int x, n;
  int first, middle, last, final;
  char ans;

do {
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;

 First = first (n);
 Middle = sigma(x, n);
 Last = last (n);
 Final = Frist/(Last + Middle);

 cout << “The value for first is << first << endl;
 cout << “The value for middle is << middle << endl;
 cout << “The value for last is << last << endl;
 cout << “The value for final is << final << endl;


cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;
*
} while (ans == 'y' || ans == 'Y');
*
getch ();





return 0;
}


virxen 22Jul2010 03:46

Re: Power and Factorial
 
1) C is case sensitive
first is NOT the same with First

2) for calculating First you need 2 parameters
X and n you only pass one
the correct would be
Code:

int first (int x,int n){
    int result;
    .....
    return result;
}

3)your code doesn't compile
have you tried to fix errors?

4) you still put returns inside loops which terminates functions before even they
start computing.

now for the First function

First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!

you see that it is only a sum of numbers and you see also an increment
from 1 to n ......> for(int i=1;i<=n;i++){.....} maybe?
hint for the sign:....pow(-1,i-1)*X^i/i!


fix them and send your new code again for the rest.

virxen 23Jul2010 03:49

Re: Power and Factorial
 
Quote:

Originally Posted by virxen (Post 70576)
now for the First function

First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!

you see that it is only a sum of numbers and you see also an increment
from 1 to n ......> for(int i=1;i<=n;i++){.....} maybe?
hint for the sign:....pow(-1,i-1)*X^i/i!


fix them and send your new code again for the rest.

the above is wrong

the correct would be
int counter=0;
for(int i=1;i<=n;i=i+2){
...pow(-1.0,counter++)*.....//for the sign changing
.....
}


All times are GMT +5.5. The time now is 03:28.