• 07-09-2003
romeoz
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.
• 07-09-2003
quzah
Quote:

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.
• 07-09-2003
Speedy5
He wants you to find fractorials for numbers between 0 and 10 divided by that number times two.
• 07-09-2003
Zach L.
Quote:

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.
• 07-09-2003
joshdick
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.
• 07-09-2003
Speedy5
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
• 07-09-2003
JaWiB
Yes...don't listen to the prof, listen to us...we make the rules
• 07-10-2003
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.
• 07-10-2003
quzah
Quote:

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.
• 07-10-2003
JaWiB
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;`
• 07-10-2003
subdene
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; }```
• 07-10-2003
joshdick
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.
• 07-11-2003
subdene
joshdick - Just saw this line of code in your response
after I did mine.
Quote:

if( (n < 0)||(n > 10) ) return -1;
Pretty similiar, good stuff.