[英]I am getting an error in C code: invalid operands to binary expression ('long long (int)' and 'int') and I don't know how to fix
I am getting the above stated error in my C code. 我在C代码中收到了上述错误。 I am also getting a warning about the same thing saying the pointer to integer conversion is incompatible.
我也收到一个警告,说明整数转换的指针是不兼容的。 The objective of the assignment is to print the factorial and two approximating factorials of the integer the user inputs.
赋值的目的是打印用户输入的整数的阶乘和两个近似阶乘。 Could I just get help on how to fix the errors?
我可以获得如何解决错误的帮助吗?
Here's the code: 这是代码:
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
#define e 2.71828
#define TRUE 1
#define FALSE 0
long long fact(int n);
double stirling1(int n);
double stirling2(int n);
/*--------------------------- main function -------------------------------
Purpose: The purpose of this program is to give the user the value of the
factorial of the nonnegative integer the user inputs.
---------------------------------------------------------------------------*/
int main ()
{
char ch = 'y';
int flag, again;
int n;
again = TRUE;
while ( again == TRUE )
{
printf("Please enter a nonnegative integer:"); //ask the user for the input
flag = scanf("%d\n\n", &n);
if ( flag != 1 ) //run this statement if the user inputs a noninteger
{
printf("Input must be an integer.");
continue;
}
else if ( n < 0 ) //run this statement if the user inputs a negative integer
{
printf("The factorial is undefined...");
continue;
}
else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
{
printf("Number Factorial Aprroximation Aproxximation2\n------------------------------------------------------------------\n"); //prints the header for first table
for ( n = 0; n <= 14; n++ )
{
++n;
printf("%d%1411lld%9e%9e\n", n, fact( n ), stirling1( n ), stirling2( n )); //calls functions to input factorials
}
}
else //run this statement if the user inputs a number greater than 14
{
printf("Number Approximation1 Approximation2\n-----------------------------------------------------\n"); //prints the header for second table
for ( n = 0; n > 14; n++ )
{
++n;
printf("%3d%9e%e\n", n, stirling1( n ), stirling2( n )); //calls functions to input approximate factorials
}
}
printf("Do you want to compute another factorial? (y/n):"); //ask user if they want another factorial of a different number
scanf("%c\n", &ch);
if (ch != 'y')
again = FALSE; //if user does not input 'y' then do not compute another factorial
}
printf( "**** Program Terminated ****" ); //ends program
}
long long fact( int n ) //function to find exact factorial
{
fact *= n; //equation for exact factorial
return fact; //return exact factorial to main
}
double stirling1( int n ) //function to find first approximate factorial
{
n = pow( n, n ) * pow( e, -n ) * sqrt( 2 * PI * n); //equation to find first approximate factorial
return n; //return approximate factorial to main
}
double stirling2( int n ) //function to find second approximate factorial
{
n = pow( n, n ) * pow( e, -n ) * sqrt( 2 * PI * n) * ( 1 + ( 1 / (12 * n) ));
//equation to find second approximate factorial
return n; //return approximate factorial to main
}
Here's the error: 这是错误:
Lab_Assignment_4_Sarah_H.c:97:7: error: invalid operands to binary
expression ('long long (int)' and 'int')
fact *= n; //equation for exact factorial
~~~~ ^ ~
Lab_Assignment_4_Sarah_H.c:99:9: warning: incompatible pointer to integer
conversion returning 'long long (int)' from a function with result type
'long long' [-Wint-conversion]
return fact; //return exact factorial to main
^~~~
You're attempting to use the name of a function as a variable, using it in a context where an integer type is expected. 您试图将函数的名称用作变量,在期望整数类型的上下文中使用它。
What you want to do is call the function again with a value of n-1
, multiply the result by n
, then return the result of the multiplication. 你想要做的是再次使用值
n-1
调用函数,将结果乘以n
,然后返回乘法的结果。
return n * fact(n-1);
There's still one piece missing. 还有一件丢失。 If you just do that, there's nothing to stop the recursive function calls.
如果你这样做,没有什么可以阻止递归函数调用。 The function will keep calling itself until you run out of stack space.
该函数将一直调用自己,直到你的堆栈空间不足为止。
What's needed is a base case which stops the recursion. 需要的是一个停止递归的基本情况 。
if (n <= 1) {
return 1;
} else {
return n * fact(n-1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.