某处有错误,我找不到。代码有效,但结果错误。我想修复,但我不知道它在哪里。请帮助我

时间:2019-03-25 22:47:41

标签: c

某处有错误,我找不到。代码有效,但结果错误。我想修复,但我不知道它在哪里。请帮助我

#include <stdio.h>
int main() {
    int elemansayisi;   //size
    printf("Eleman sayisini giriniz : ");
    scanf("%d", &elemansayisi); //size
    printf("\n");
    int dizi[elemansayisi], i, j; //array
    int enBuyuk = dizi[0]; //first biggest element 
    for (i = 0; i < elemansayisi; i++) {
        printf("Dizinin  elemanlanrini giriniz : "); //getting array from user
        scanf("%d", &dizi[i]);
    }
    printf("\n");
    for (i = 0; i < elemansayisi; i++) {

        printf("dizinin %d. elemani: %d\n", i, dizi[i]);  //print array 
        printf("\n");

    }

    for (j = 0; j <= elemansayisi; j++) {

        if (enBuyuk <= dizi[j])  //find biggest element in array

            enBuyuk = dizi[j];

    }

    printf("En buyuk deger : %d", enBuyuk);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这会将enBuyuk设置为未定义的值!

int enBuyuk = dizi[0]; //first biggest element 

相反,将这一行移到for循环的上方。


for (j = 0; j <= elemansayisi; j++) {  

这应严格小于(删除=符号)

int enBuyuk = dizi[0]; // Move line here
for (j = 0; j < elemansayisi; j++) {