converting simple Python code to C...

Hi, I am having a bit of trouble converting a simple Python code for computing factorials into the C language.

Python Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import sys
def fac(n):
	f=1
	i=1
	while(i <= n):
		f = f*i
		i +=1
	return f
def main():
	n = int(sys.argv[1])
	f = fac(n)
	print "%d! = %d"%(n,f)
if __name__ == "__main__":
	main()


My C code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

/*declare functions*/
int fac(int);

int main(int argc, char* argv[])
{
	int f, i, n;
	n=atoi(argv[1]);
	f=fac(n);
	printf ("%d! = %d",n,f);
	return 0;
}

/*define functions*/
int fac(int n)
{
	int f=1, i=1;
	while(i<=n)
	{
		f=f*i;
		i +=1;
		return f;
	}
}


I am getting a whole bunch of errors at compiling..I am a beginner so I probably have done something wrong. Any help here?
Hi Plokij, there are many online and downloadable converters from python to c++.You can use for example Cython to convert to C (then its very easy to convert it to c++ by yourself) or Shed skin to convert from python to c++.There are many many more..
Good luck
Last edited on
I'm not too familiar with Cython, but this is for a class and I want to learn how to do it manually. Not sure if Cython will allow me to do that..I will definitely look into it..

But for now, can anyone look at my C code and tell me where I went wrong?
That compiles perfectly without change for me, and if I move the return f; out of the while loop, it works correctly. What errors do you get?

Last edited on
Paste the errors.
 
int fac(int);

 
int fac(int n);

First one
int fac(int);

That's a declaration, looks fine to me.

int fac(int n);

That doesn't exist in the code plokij posted. You've added a semi-colon.
Excuse me, my mistake.Everything looks fine.What are the errors?
"undefined reference to 'std::string::size() const'"
"undefined reference to 'std::string::operator[](unsigned int) const'"
"undefined reference to 'std::ios_base::Init::Init()'"

and a few more that look similar to those...never seen those types of errors before...and I'm using gcc..
Errors I can find:
Line 13: You should check if (argc > 1) before trying to use any argv[]
Line 27: You return in the middle of a loop. Return should be outside the loop.
Line 12: Define n = 1, in case no value is supplied through argv.
Line 12: No need for variable i; Unused

If this is pure C, then there is no iostream or using namespace std;

C++ Version of Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Help 31
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>

/*declare functions*/
int convert(std::string str);
int fac(int);

int main(int argc, char *argv[])
{
  // Factorials
  int factorial(0), number = 1;

  if (argc > 1)
    number = convert( argv[1] );
  factorial = fac(number);
  std::cout << number << "! = " << factorial << std::endl;

  return 0;
}

/*define functions*/
int fac(int n)
{
  int ans = 1;
  for (int i = 1; i <= n; ++i)
  {
    ans *= i;
  }

  return ans;
}

int convert(std::string str)
{
  int n;
  std::istringstream ss(str);
  ss >> n;
  return n;
}
Last edited on
Perfect! I've removed iostream and namespace and moved the "return f" out of the while loop, and now it works.. thank you everyone..=)

just wondering, though...why wasn't it working for me when i had the iostream and namespace in there? is it because i called from both the c library and c++ library? (i'm not sure if that even make sense, i'm quite a noob still...)
oh by the way, here is the updated code that works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>


/*declare functions*/
int fac(int);

int main(int argc, char* argv[])
{
	int f, n;
	n=atoi(argv[1]);
	f=fac(n);
	printf ("%d! = %d",n,f);
	return 0;
}

/*define functions*/
int fac(int n)
{
	int f=1, i=1;
	while(i<=n)
	{
		f=f*i;
		i +=1;
	}
		return f;
}
Undefined reference errors generally mean that the linker cannot find the actual library that provides that functionality.
Topic archived. No new replies allowed.