3 Function plus Main Function C 程序计算和显示阶乘
3 Function plus Main Function C Program calculating and displaying a Factorial
第一个函数需要获取数字并将其传递回 main,然后需要将该值传递给计算阶乘的第二个函数并将该值传递回 main,结果在第三个函数中打印和最终功能。
程序计算输入数字的阶乘。我需要保留 for 循环。我不确定出了什么问题,我得到了垃圾值,所以我想我在某处丢失了一个值或者没有存储一个值。
也欢迎任何一般性帮助。
#include <stdio.h>
void GetData(int &x)
{
printf("Please enter a number:\n");
scanf("%d%*c", &x);
return;
}
int Factorial(int x)
{
int factorial = 1;
int i;
for(i = 1; i <= x; i++)
{
factorial = factorial * i;
}
return(x);
}
void PrintResults(int factorial)
{
printf("The factorial = %d\n", factorial);
return;
}
int main()
{
int x, factorial;
GetData(x);
Factorial(x);
PrintResults(factorial);
return(0);
}
首先,您应该将对 GetData
的调用更改为:
GetData(&x);
因为你想传递一个指针。然后,其声明应更改为:
void GetData(int *x)
{
printf("Please enter a number:\n");
scanf("%d%*c", x);
}
那么,您应该 return 变量 factorial
而不是 x
。换行:
return(x);
至:
return(factorial);
然后调用 Factorial
函数如下:
factorial = Factorial(x);
现在,变量 factorial
未初始化,如您所说,将其传递给 Factorial
您将得到垃圾。
#include <stdio.h>
void GetData(int &x)
{
printf("Please enter a number:\n");
scanf("%d%*c", &x);
}
int Factorial(int x)
{
int factorial = 1;
for(int i = 1; i <= x; i++)
{
factorial = factorial * i;
}
return(factorial);
}
void PrintResults(int factorial)
{
printf("The factorial = %d\n", factorial);
}
int main()
{
int x; // Declaring x;
getData(x); // Initializing x
int factorial = Factorial(x); // Using x to compute factorial and store result in variable
printResult(factorail); // Print that variable
}
试试这个,它应该可以工作,但我现在无法编译它。
我没有使用指针,因为您似乎不需要它们,但更喜欢使用 C++ ?
我删除了你的 return;
作为你的函数 return void
,即使写它也没用而且令人困惑。
第一个函数需要获取数字并将其传递回 main,然后需要将该值传递给计算阶乘的第二个函数并将该值传递回 main,结果在第三个函数中打印和最终功能。
程序计算输入数字的阶乘。我需要保留 for 循环。我不确定出了什么问题,我得到了垃圾值,所以我想我在某处丢失了一个值或者没有存储一个值。
也欢迎任何一般性帮助。
#include <stdio.h>
void GetData(int &x)
{
printf("Please enter a number:\n");
scanf("%d%*c", &x);
return;
}
int Factorial(int x)
{
int factorial = 1;
int i;
for(i = 1; i <= x; i++)
{
factorial = factorial * i;
}
return(x);
}
void PrintResults(int factorial)
{
printf("The factorial = %d\n", factorial);
return;
}
int main()
{
int x, factorial;
GetData(x);
Factorial(x);
PrintResults(factorial);
return(0);
}
首先,您应该将对 GetData
的调用更改为:
GetData(&x);
因为你想传递一个指针。然后,其声明应更改为:
void GetData(int *x)
{
printf("Please enter a number:\n");
scanf("%d%*c", x);
}
那么,您应该 return 变量 factorial
而不是 x
。换行:
return(x);
至:
return(factorial);
然后调用 Factorial
函数如下:
factorial = Factorial(x);
现在,变量 factorial
未初始化,如您所说,将其传递给 Factorial
您将得到垃圾。
#include <stdio.h>
void GetData(int &x)
{
printf("Please enter a number:\n");
scanf("%d%*c", &x);
}
int Factorial(int x)
{
int factorial = 1;
for(int i = 1; i <= x; i++)
{
factorial = factorial * i;
}
return(factorial);
}
void PrintResults(int factorial)
{
printf("The factorial = %d\n", factorial);
}
int main()
{
int x; // Declaring x;
getData(x); // Initializing x
int factorial = Factorial(x); // Using x to compute factorial and store result in variable
printResult(factorail); // Print that variable
}
试试这个,它应该可以工作,但我现在无法编译它。 我没有使用指针,因为您似乎不需要它们,但更喜欢使用 C++ ?
我删除了你的 return;
作为你的函数 return void
,即使写它也没用而且令人困惑。