該程序的目的是檢測指定數組中的最大數目。但是目前它只能檢測到數組的最後一位數字。如果其他if被刪除或註釋掉,那麼它可以正確檢測除最後一位數字之外的所有數字。else if語句阻止程序檢測最大索引
#include <stdio.h>
int main(){
int c,i,max;
printf("Enter the size of the array.");
scanf("%d",&c);
int array[c];
printf("Enter the integers to fill the array.\n");
for(i=0;i<c;i++){
scanf("%d",&array[i]);
}//end for
int last_element = array[c-1];
for(i=0;i<c;i++){
printf("%d",array[i]);
if(array[i-1] > array[i]) //This if statement detects greatest
{ //index of array for all but last index
max = array[i-1];
}//end if
else if(last_element > array[i-1]) //This else if detects greatest
{ //index of array in last index
max = last_element; //This statement is always eval
}//end if //uating true.
}//end for
printf("\n%d",max);
return 0;
}//end main
'int數組並[c];'不是由C++標準所允許'C'必須在編譯時 – CoryKramer
已知C不是C++不是C!當您使用C功能時,我刪除了垃圾郵件標籤。並提供更多信息。我們不是一個調試服務。 3年後,你應該知道[問]。 – Olaf
IDE中集成了一個名爲「調試」的漂亮功能,使用它 –