C在主函数中显示(打印),而在自定义函数中返回值
这个程序需要让用户在主功能中输入两个数字:m和n。然后需要在自定义函数中计算n的所有因子,从m开始。然后需要在主功能中显示/打印这些因素 重要的是:不能从自定义函数打印因子,只能从主函数打印。也没有全局变量 我的程序只返回第一个因子(应输入1(m),18(n)=1,2,3,6,9,18) 主函数中的for()也有问题,我不知道应该使用什么作为限制,因为它返回的因子数是一个数字的数倍C在主函数中显示(打印),而在自定义函数中返回值,c,function,main,C,Function,Main,这个程序需要让用户在主功能中输入两个数字:m和n。然后需要在自定义函数中计算n的所有因子,从m开始。然后需要在主功能中显示/打印这些因素 重要的是:不能从自定义函数打印因子,只能从主函数打印。也没有全局变量 我的程序只返回第一个因子(应输入1(m),18(n)=1,2,3,6,9,18) 主函数中的for()也有问题,我不知道应该使用什么作为限制,因为它返回的因子数是一个数字的数倍 #include<stdio.h> #include <conio.h> int tot
#include<stdio.h>
#include <conio.h>
int toti_factori(int x,int y);
main(){
int m,n,i;
printf("intro m si n");
scanf("%d %d",&m,&n);
for(i=m;i<=n;i++)
{
printf("%d ",toti_factori(m,n));
}
}
int toti_factori(int x,int y){
static int i=1;
int t=0;
for(i=1;i<=y;i++){
if(y%i==0){
if(i>=x){
return i;
}
}
}
}
#包括
#包括
inttoti_因子(intx,inty);
main(){
int m,n,i;
printf(“简介m si n”);
scanf(“%d%d”,&m,&n);
for(i=m;i它只返回一个int,而不是int数组。在第二个for()的第一个循环中,它返回一个值并跳过该函数。请尝试将返回类型更改为int[]。无法打印任何内容,因为该方法在print方法之前完成
编辑:
#包括
#包括
inttoti_因子(intx,inty);
main(){
int m,n,i;
printf(“简介m si n”);
scanf(“%d%d”,&m,&n);
对于(i=m;i我的建议:
创建一个可以容纳所有因素的struct
从toti\u factori
返回struct
的对象
根据需要使用结构的内容
这是一个工作程序
#include <stdio.h>
#include <stdlib.h>
typedef struct Factors
{
size_t size;
int* data;
} Factors;
Factors toti_factori(int x, int y);
int main(){
int m, n, i;
printf("intro m si n: ");
scanf("%d %d", &m, &n);
// Get all the factors in one call.
Factors factors = toti_factori(m, n);
// Print the factors.
for ( i = 0; i < factors.size; ++i)
{
printf("%d ",factors.data[i]);
}
printf("\n");
// Deallocate memory before program ends.
free(factors.data);
return 0;
}
Factors toti_factori(int m, int n){
int count = 0;
int index = 0;
Factors factors;
int i = 0;
// Find out the number of factors.
for ( i = m; i <= n; ++i)
{
if ( n%i == 0)
{
++count;
}
}
// Allocate memory for the factors.
factors.size = count;
factors.data = malloc(count*sizeof(*factors.data));
// Gather the factors.
for ( i = m; i <= n; ++i)
{
if ( n%i == 0)
{
factors.data[index] = i;
++index;
}
}
return factors;
}
好的,但我希望它成功地返回1,然后返回2,3等(18的因子以1开始)-也不能使用arrays@Stack_token因此,修改函数,使其接收指示因子索引的附加参数,然后在main中对其进行迭代(就像我在第二个for方法中所说的那样)。你的问题是每次调用函数时你都做i=1。我该如何解决这个问题?我尝试使用静态,但没有正常工作,我不能使用数组或结构。一个朋友通过添加最后一个因子作为参数来解决这个问题。我不明白他是什么意思。我如何逐一返回这些因子(1然后2然后3)在我的程序中?谢谢,这个解决方案是完美的。
#include <stdio.h>
#include <stdlib.h>
typedef struct Factors
{
size_t size;
int* data;
} Factors;
Factors toti_factori(int x, int y);
int main(){
int m, n, i;
printf("intro m si n: ");
scanf("%d %d", &m, &n);
// Get all the factors in one call.
Factors factors = toti_factori(m, n);
// Print the factors.
for ( i = 0; i < factors.size; ++i)
{
printf("%d ",factors.data[i]);
}
printf("\n");
// Deallocate memory before program ends.
free(factors.data);
return 0;
}
Factors toti_factori(int m, int n){
int count = 0;
int index = 0;
Factors factors;
int i = 0;
// Find out the number of factors.
for ( i = m; i <= n; ++i)
{
if ( n%i == 0)
{
++count;
}
}
// Allocate memory for the factors.
factors.size = count;
factors.data = malloc(count*sizeof(*factors.data));
// Gather the factors.
for ( i = m; i <= n; ++i)
{
if ( n%i == 0)
{
factors.data[index] = i;
++index;
}
}
return factors;
}
#include <stdio.h>
int toti_factori(int x, int y);
int main(){
int m, n, i;
printf("intro m si n: ");
scanf("%d %d", &m, &n);
// Print the factors.
for ( i = m; i < n; ++i)
{
// Get the next factor and store it in i.
i = toti_factori(i, n);
printf("%d ", i);
}
printf("\n");
return 0;
}
int toti_factori(int m, int n){
int i = 0;
for ( i = m; i <= n; ++i)
{
if ( n%i == 0)
{
return i;
}
}
return i;
}