2016-02-20 110 views
-2

C初學者在這裏,我有一個程序,我正在處理,不幸的是,該程序要麼不正確地讀取數組,要麼不正確地顯示內容。數組內容顯示不正確

這裏就是我有

int main() 
{ 
    int size;  // the number of elements 

    printf("Enter size of the array: \n"); 
    scanf("%d", &size); 

    double *array = malloc(size*sizeof(int)); //memory allocated using malloc 
    if (array == NULL) 
    { 
     printf("Error! memory not allocated."); 
     exit(0); 
    } 
    printf("Enter elements of array: \n"); 
    for (int i = 0; i<size; ++i) 
    { 
     scanf("%d", &array[i]); 
    } 

    printf("Results:"); 

    double min = array[0]; 
    printf("\nmin = %.3lf ", min); 
    double max = array[size - 1]; 
    printf("\nmax = %.3lf", max); 

,這裏是我的輸出

enter image description here

+0

你分配一個整數數組並將其分配給一個雙指針。 –

回答

0

陣列=的malloc(大小的sizeof(int)的);

應該是:

double *array = malloc(size * sizeof(double)); 

的scanf( 「%d」,&陣列[I]);

這裏不對控制線,應該是%lf雙,而不是%d

+0

是不是這樣,同樣的事情還在發生 – Frankjoww

+0

哇,真的很簡單,謝謝 – Frankjoww