I asked the prof and still not direct answer

This is a discussion on I asked the prof and still not direct answer within the C++ Programming forums, part of the General Programming Boards category; Hereis my last problem for the week. I talked to the prof on email and still dont understand what he ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    I asked the prof and still not direct answer

    Hereis my last problem for the week. I talked to the prof on email and still dont understand what he wants. I am going to post my problem and could someone explain to me what this actually means, I am really lost.
    Code:
    Write a C++ Program to calculate the following function:
    PowerInt(n) = (n!)/n(2) , if n<=1 and n<=10
    Power Int (0) = 1
    PowerInt (n) = error, if n<0 or n>10
    Where n! = 1*2*3...*n for n>=1.
    
    For example: 
    PowerInt (1) = 1!/1(2) =1
    PowerInt (2) = 2!/2(2) = (1*2)/(2*2) = 2/4 =0.5
    PowerInt (3) = 3!/3(2) = (1*2*3)/(3*3) = 6/9 =0.667
    PowerInt (4) = 4!/4(2) = (1*2*3*4)/(4*4) = 24/16 =1.5
    We can not use math or cmath library in thie program.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For example:
    PowerInt (1) = 1!/1(2) =1
    PowerInt (2) = 2!/2(2) = (1*2)/(2*2) = 2/4 =0.5
    PowerInt (3) = 3!/3(2) = (1*2*3)/(3*3) = 6/9 =0.667
    PowerInt (4) = 4!/4(2) = (1*2*3*4)/(4*4) = 24/16 =1.5
    Poorly worded, I agree. However, from looking at what the latter portion of each line looks like, you should be able to come up with something that will churn out the desired results:
    Code:
    float p(int i)
    {
        if( i < 10 && i > 1 )
        {
            int x = i*i, /* store the first number, squared */
                 y; /* a counter */
            float z /* the sum */
    
            /*
            I will leave the inner workings to you.
            Use your counter to loop i times.
            Sum z with that number.
            */
    
            return z / (float)x;
        }
        return 1.0;
    }
    I believe that should give you what you need.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    He wants you to find fractorials for numbers between 0 and 10 divided by that number times two.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Speedy5
    He wants you to find fractorials for numbers between 0 and 10 divided by that number times two.
    By the number squared (from the examples given).

    Would be better written as compute (n!)/(n^2) for 1 <= n <= 10.
    My advice... Break up the operations.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    double fac(double n);
    double square(double x);
    double PowerInt(double n);
    
    int main()
    {
      for(int i = 0; i <= 10; ++i)
        cout << i << ": " << PowerInt(i) << endl;
      system("pause");
      return 0;
    }
    
    double fac(double n)
    {
        if(n < 2) return 1;
        return n * fac(n - 1);
    }
    
    double square(double x)
    {
        return x * x;
    }
    
    double PowerInt(double n)
    {
        if(n == 0) return 1;
        if( (n < 0)||(n > 10) ) return -1;
        return fac(n)/square(n);
    }
    
    /*Output
    
    0: 1
    1: 1
    2: 0.5
    3: 0.666667
    4: 1.5
    5: 4.8
    6: 20
    7: 102.857
    8: 630
    9: 4480
    10: 36288
    Press any key to continue . . .
    */
    I was bored. Yeah yeah, I know recursion takes up lots of memory and yada, yada, yada. I use the recursive solution to finding factorials because I can remember it off the top of my head. For your class you probably ought to go with an iterative factorial function instead.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well the question itself from the teacher has a problem. The first part clearly shows multiplication, the examples show sqauring... lol

    Or maybe n is a function... Or maybe some really wierd notation... It should be n^2...


    Write a C++ Program to calculate the following function:
    PowerInt(n) = (n!)/n(2) , if n<=1 and n<=10
    Power Int (0) = 1
    PowerInt (n) = error, if n<0 or n>10
    Where n! = 1*2*3...*n for n>=1.

    For example:
    PowerInt (1) = 1!/1(2) =1
    PowerInt (2) = 2!/2(2) = (1*2)/(2*2) = 2/4 =0.5
    PowerInt (3) = 3!/3(2) = (1*2*3)/(3*3) = 6/9 =0.667
    PowerInt (4) = 4!/4(2) = (1*2*3*4)/(4*4) = 24/16 =1.5

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes...don't listen to the prof, listen to us...we make the rules
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    I was reading over all these codes given on here and checking in my textbook and I dont understanf what y; does and float z;, I'm really lost, could you maybe explain that a little more into detail. I have never even seen this until now and I dont know where to start let alone what that code means that you wrote to help me out. sorry about that.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by romeoz
    I was reading over all these codes given on here and checking in my textbook and I dont understanf what y; does and float z;, I'm really lost, could you maybe explain that a little more into detail. I have never even seen this until now and I dont know where to start let alone what that code means that you wrote to help me out. sorry about that.
    Here you go, an explanation:
    Code:
    float p(int i) // take an int, return a float
    {
        if( i < 10 && i > 1 ) //check boundaries
        {
            // declare 2 integers, x and y
            int x = i*i,
                 y;
            // declare a floating point number
            float z;
    
            // make a loop, use y as the loop
            // counter to move through it.
            // something like:
            for( y = ... ; ... ; ... )
                // do stuff here
    
            // you have to use a float to store
            // the sum, since you're returning
            // a decimal value
            return z / (float)x;
        }
        return 1.0;
    }
    I left the loop itself out intentionally, since you provided on effort of your own, only what you needed done. The loop was left out so you could learn how to do it on your own.

    Not that that matter, since not matter what your question, some one will do the work for you. It always works that way.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Just a note, "float" means it can contain a decimal point (I was always confused by that...)

    A couple things that I thought look odd in quzah's code:

    Code:
     if( i < 10 && i > 1 ) //check boundaries
    to make it more clear:
    Code:
    if (  (i<10)  &&  (i>1)  )
    and the
    Code:
    y;
    is actually being declared as an int,
    Code:
    int x = i*i,  y;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Code:
    #include <iostream.h>
    
    double long PowerInt(int Num);
    
    int main(void)
    {
      for(int i = 0; i < 5; i++) cout << PowerInt(i) << "\n";
    
      getchar();
    
      return 0;
    }
    
    double long PowerInt(int Num)
    {
      unsigned Sq = Num * Num, FactResult = 1;
    
      if(Num < 0 || Num > 10) return -1; else if(!Sq) Sq++;
    
      for(int i = 1; i <= Num; i++) FactResult *= i;
    
      return (double long) FactResult / (double long) Sq;
    }
    Be a leader and not a follower.

  12. #12
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I would just like to say that it's interesting to see how a few different people all looked at the exact same problem yet coded very different looking programs that still yield the exact same results.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  13. #13
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    joshdick - Just saw this line of code in your response
    after I did mine.
    if( (n < 0)||(n > 10) ) return -1;
    Pretty similiar, good stuff.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

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