2015-10-07 17 views
0

所以我分配如下:如何增加陣列塊,我想操縱

「編寫一個程序,從名爲input.txt輸入文本文件號碼列表讀取對於每個讀取數量,你的程序應該確定並打印出這個數字是否充足。「

input.txt中= enter image description here

我的代碼下來,我想我終於想通了如何發現並總結所有的除數,以檢查是否有數量豐富與否,但這個問題我有是怎麼我是否會去下一個數組塊來檢查下一個數字?現在它正在檢查「15」這是第一個要檢查的號碼(告訴程序需要檢查多少個號碼)但是我怎樣才能檢查下一個號碼等等?

謝謝!

#include <stdio.h> 
#define SIZE 100 

int main(){ 

FILE *ifp; 
int cases; 
int i; 
int j; 
int counter = 1; 
int dividen =1; 
int listArray[SIZE]; 
int sum = 0; 

ifp = fopen("input.txt", "r"); 
fscanf(ifp,"%d", &cases); 

for(i=0;i<cases;i++){ 
fscanf(ifp,"%d",&listArray[i]); 
printf("%d\n", listArray[i]);} 


while(listArray[0] > counter){ 
if(listArray[0] % dividen == 0){ 
sum = sum+dividen; 
dividen++; 
counter++;} 
else{ 
dividen++; 
counter++;} 
} 

if(listArray[0] > sum) 
     printf("\n%d is not an abundant number\n",listArray[0]); 

system("pause"); 
return 0; 
} 

回答

0

移動,檢查豐成相同的循環,你存入listArray代碼,或寫另一個循環認爲的listArray每個元素的作品之後。

很好的解決這樣的問題,一般是這樣的:

static const int TO_READ = 3; // For example. 
FILE* file = open_input_file(); 
variable foo, bar, baz; 
while (TO_READ == fscanf(file, format, &foo, &bar, &baz)) { 
    do_stuff_with(foo, bar, baz); 
}