编写一个计算n的子程序.使用此子程序时,制作一个计算(a+b)的程序
Write a subprogram that calculates n!. While using this subprogram make a program that calculates (a+b)!
我的任务是
编写一个计算
n!
的子程序。在使用此子程序时,制作一个计算CCD_ 2的程序。
我写了以下代码:
using namespace std;
int f(int n) {
for(int i=1;i<=n;i++){
f=f*i;
}
return f;
}
int main() {
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"factorial of a+b="<<(a+b)*f;
return 0;
}
当我编译时,我得到了这个错误:
In function 'int f(int)':
7:27: error: invalid operands of types 'int(int)' and 'int' to binary 'operator*'
8:8: error: invalid conversion from 'int (*)(int)' to 'int' [-fpermissive]
In function 'int main()':
16:34: error: invalid operands of types 'int' and 'int(int)' to binary 'operator*'
17:9: error: expected '}' at end of input
这是您的错误
return f;
返回函数f()
的函数指针。此外,您还在使用函数指针进行计算。
在C++中,您需要声明一个可用于计算和返回的变量:
int f(int n) {
int result = 1;
for(int i=1;i<=n;i++) {
result=result*i;
}
return result;
}
相关文章:
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 不计算一个范围内的完美数
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 为什么我必须添加一个小数才能在C++中正确计算此数学
- 视觉我希望一个函数在另一个函数C++中进行计算
- 计算单词中的音节数,但考虑到单词中彼此相邻的任何元音都算作一个音节
- 如何使用条件计算 3D 网格中从一个点到另一个点的所有路径
- 计算在同一位置至少包含一个常用字符的不同字符串对
- 返回一个计算了参数的函数
- 我在c++中遇到了一个奇怪的错误,其中一个计算2个小整数加法的语句溢出到一个长值中
- 在C++中,是否可以编写一个条件的lambda,即只是一个计算结果为真或假的条件
- 你能在C++中制作一个计算的goto吗?
- 为什么需要 FPU 重置以防止 NaN 结果传播到下一个计算结果?
- 创建一个C++计算奇数、偶数和零数的程序
- 在C++中是否已经为文件系统迷你过滤器驱动程序编写了一个计算MD5哈希的函数
- 编写一个计算n的子程序.使用此子程序时,制作一个计算(a+b)的程序
- 将数组从C/C++传递给Fortran,并返回一个计算的数组
- 扩展一个计算线路频率的程序