如何在C中创建一个空数组来存储从给定from程序计算出的值
我在初始化一个数组时遇到了困难,无法确定for循环中的值是如何给出的。我想放弃计算出的素数,然后将非素数保留在一个数组中,数组的大小从一开始就必须是未知的。然后我必须调用收集数组中的数字,然后对它们应用Carmichael方程来找到Carmichael数字 我发现将方程应用于数组中的值也是困难的。 这是我的代码:如何在C中创建一个空数组来存储从给定from程序计算出的值,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我在初始化一个数组时遇到了困难,无法确定for循环中的值是如何给出的。我想放弃计算出的素数,然后将非素数保留在一个数组中,数组的大小从一开始就必须是未知的。然后我必须调用收集数组中的数字,然后对它们应用Carmichael方程来找到Carmichael数字 我发现将方程应用于数组中的值也是困难的。 这是我的代码: int main(int argc, char **argv){ int a ,b; int notPrime[] = {}; printf("Type in
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", ¬Prime[p]) ;
}
}
}
return 0;
}
我有错误,因为我
请给我一点帮助,让我朝着正确的方向走,我会努力改正的
狂喜
for (i < b;){
还有这个-
if((pow(m,notPrime[p])-m)%notPrime[p]==0){ //
pow返回double和notPrime[p]为int。%n运算符位于double和int上
将pow的结果显式强制转换为int
还有这个-
printf( "%d \n", ¬Prime[p]) ;
^ this is not needed
不要传递int的地址
编辑-
在获取输入a和b后,可以将数组声明为-
因为i
printf( "%d \n", ¬Prime[p]) ;
^ this is not needed
int notPrime[a] = {0}; // or size b as you desire