What am I supposed to do?

My professor gave this function to us:
Given a function factorial as follows:
1
2
3
4
5
6
7
int power(int exponent)
{
  int i, result=1;
  for(i=1;i<=exponent; i++)
   result = result * i;
  return result;
}

He wants us to write a main routine to test factorial (3) and factorial (10)
We are not using recursive. I know we can do factorial with recursive but that's our next assignment. I'm just wondering how am I supposed to code this. I coded it a few times but each time I got an errors.
This is what I came up with but its pretty messy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "stdafx.h"
#include<iostream> 
#include <stdio.h>
using namespace std; 

void main()
{
	int factorial()
	{
	int power(int exponent)
	{
		int i, result =1;
		for(i=1; i<= exponent; i++)
		{
			result = result*i;
			return result;
		}
	}
}
}

So what am I not understanding or what am I missing?
Wait, professor wants you to write a factorial routine?
Or just test one he has given you?

And that there is a power function, not a factorial function.

1
2
3
4
5
6
7
8
9
  if (factorial(3) == 3*2)
    cout << "factorial(3): ok\n";
  else
    cout << "factorial(3): FAIL\n";

  if (factorial(10) == 10*9*8*7*6*5*4*3*2)
    cout << "factorial(10): ok\n";
  else
    cout << "factorial(10): FAIL\n";

Duoas wrote:
that there is a power function, not a factorial function
Not sure I follow... It looks like a misnamed factorial function to me. A power function would need parameters for the base and the exponent.

@OP You can't declare functions within functions. It looks like your professor has already provided you with a factorial function (which (s)he erroneously calls 'power') and you just need to write int main() that contains the code in Duoas's post.
1
2
3
4
5
6
7
8
9
//#includes up here...

//copy-paste your professor's code here

int main()
{
    //Duoas's code here
    return 0;
}


EDIT: It also looks like your professor's code doesn't use some unnecessary braces, and this may be a source of confusion for you (judging by your attempt to replicate it). This code is practically identical to your professor's, but maybe it is easier to read:
1
2
3
4
5
6
7
8
9
int factorial(int input)
{
    int i, result=1;
    for(i=1;i<=input; i++)
    {
        result = result * i;
    }
    return result;
}

The biggest takeaway is to notice the placement of the return statement. It doesn't go inside the loop!
Last edited on
Ooops. result*i, not result*result == yes, a factorial.
Thanks for the help guys! The build succeeded but it keeps closing out. I even put a system pause at the end of the main function. Even thought I'm pretty sure I did not have to. So I cant even check to make sure the program is right. Any clues.
Just talked to my professor he just wanted us to cout the function.
1
2
std::cout << factorial(3) << std::endl;
std::cout << factorial(10) << std::endl;

?
Yes that is right! Thanks for help! Do you guys think you can help my with my other topic?
Topic archived. No new replies allowed.