Thread: Power and Factorial

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you have return statements inside your loops.
    So you're only running the first iteration.

    Better indentation would help you spot some of these problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You would have picked up a lot of concerns if you actually tried to compile and run your program. A compiler will complain bitterly on some parts of your code. If you get the program running, you will get results you don't expect.

    For example, look a bit more closely at your sigma() function. It redeclares x (and n) inside the function. Having a function with an argument named x and a variable inside the body of that named x causes some ambiguity about what "x" actually refers to. You're also returning from inside your while() loop, which means you only execute the loop body once.

    An int is a bounded type (i.e. there is a maximum value it can hold). Raising a value to a power or computing a factorial can easily produce a result that exceeds what can be stored in an int (regardless of what size int your compiler actually supports). If your compiler supports a 32 bit int (typical of many compilers, but not universal) overflow will occur if trying to compute factorial of 13 or so. Because of that, you'll need to consider some smart way of computing a term x^n/n!

    sqrt() works with a floating point (double) type. Dividing two integers gives an integer result. Truncation of some form will be happening which may, or may not, be what you want. There is no magic that makes the division of two integers become a real number.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Whatever they don't spot, maybe someone else can find through cross-posting

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah - just found that
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Fix your indentation and compile the thing fist, you lazy so and so.
    Test the functions one at a time before combining them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with return value in C
    By jericjones45 in forum C Programming
    Replies: 8
    Last Post: 03-27-2010, 05:17 PM
  2. probability
    By sycamorex in forum Windows Programming
    Replies: 0
    Last Post: 11-25-2006, 06:10 AM
  3. Factorial conversion
    By micha_mondeli in forum C++ Programming
    Replies: 7
    Last Post: 04-07-2005, 07:07 AM