我知道我在這里問的是非常基本的問題,但是誰能給我一個提示,如何正確地回圈遍歷在另一個函式中初始化的陣列?我試著用谷歌搜索,找到了大量的視頻,但我還沒能把它弄好。有人可以幫我找出我的代碼中缺少什么嗎?我是一個苦苦掙扎的初學者。在此先感謝您的時間。
我的代碼(不起作用):
#include <stdio.h>
#define ARR_RANGE 1000000
#define GREATEST_NUMBER 1000000
void sieve(int eratosthenes[]);
int main()
{
int eratosthenes[ARR_RANGE];
int n = 999;
int c;
for(i = 2; i <= arrLen; i)
{
if(eratosthenes[i]!= -1)
{
int c = 0;
while(n % i == 0)
{
n /= i;
c;
}
if(c >= 2)
{
printf("%d^%d x ", i, c);
}
else if(c == 1)
{
printf("%d x ", i);
}
}
else
continue;
}
return 0;
}
void sieve(int eratosthenes[])
{
for(int i = 1; i < GREATEST_NUMBER; i)
{
eratosthenes[i] = i;
}
for(int i = 2; i*i < GREATEST_NUMBER; i)
{
if(eratosthenes[i] != -1)
{
for(int j = 2*i; j < GREATEST_NUMBER ; j = i)
eratosthenes[j] = -1;
}
}
int arrLen = sizeof eratosthenes / sizeof eratosthenes[0];
}
uj5u.com熱心網友回復:
在main不可見函式的呼叫中sieve,因此陣列沒有傳遞到函式中,為此您必須在main sieve(eratosthenes);(Passage by reference)中寫入
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/454306.html
