gpt4 book ai didi

c - 如何创建一个空数组来存储根据 C 程序给定计算得出的值

转载 作者:行者123 更新时间:2023-11-30 21:02:03 25 4
gpt4 key购买 nike

我在将数组初始化为如何从 for 循环给出的值时遇到困难,我想丢弃计算出的素数,然后将非素数保留在数组中,数组的大小从一开始就必须未知。然后,我必须调用从数组中收集数字,然后将卡迈克尔方程应用于它们以找到卡迈克尔数。

将方程应用于数组中的值也是我发现的困难。这是我的代码:

int main(int argc, char **argv){
int a ,b;
int notPrime[] = {};
printf("Type in two non negative intergers: \n");
scanf("%d" "%d", &a , &b);
int i=a , k=0 ,count=0, j, p, m;
for (i < b;){ //from the integers given b must be the larger of the two
if (i==0 || i==1) { //to calculte the prime numbers
notPrime[k]=i;
}else{
for( j=2;j<i;j++){
if(i%j==0){
break;
}else{
notPrime[k]=i;
}
}
}
i++;
k++;
}
p=0;
if (p<=k){
for (m=1;m<notPrime[p];m++){
if((pow(m,notPrime[p])-m)%notPrime[p]==0){ //use of the values in the array and use of the carmichael equation
count=count+1;
}
if(count==notPrime[p]-1){
Printf( "%d \n", &notPrime[p]) ;
}
}
}
return 0;
}

我在for (i < b;){上有错误, if((pow(m,notPrime[p])-m)%notPrime[p]==0){最后是打印语句 Printf( "%d \n", &notPrime[p])

请给我一点帮助,让我朝着正确的方向前进,我会尽力纠正。

x狂喜

最佳答案

for (i < b;){    

这不是 for 循环的有效语法。你可以这样写 -

for (;i < b;){    

还有这个 -

if((pow(m,notPrime[p])-m)%notPrime[p]==0){ //

pow 返回 doublenotPrime[p]intdoubleint 上的 % 运算符。

pow的结果显式转换为int

还有这个-

 printf( "%d \n", &notPrime[p]) ;
^ this is not needed

不要传递int的地址。

编辑-

您可以在输入 ab 后将数组声明为这样 -

 int notPrime[a] = {0};           // or size b as you desire 

关于c - 如何创建一个空数组来存储根据 C 程序给定计算得出的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com