これは、10アレイの最小要素と最大要素を検出する簡単なプログラムです。なぜ私がセグメンテーションフォールト(コアダンプ)エラーを受け取っているのか分かりません。C - この簡単なプログラムでセグメンテーションフォールト(コアダンプ)エラーが発生する
#include <stdio.h>
int main(void) {
int i, j, min, array[10], max, n;
//This loop get user input for the elements of the array
for(i = 0; i < 10; i++) {
printf("Enter element number %d:", i);
scanf("%d", &n);
array[i] = n;
}
min = array[0];
max = array[0];
//This loop finds the smallest element of the array
for(j = 0; j < 10; j++) {
if(min > array[j]) {
min = array[j];
}
}
//This loop finds the largest element of the array
for(j = 9; j >= 0; j++) {
if(max < array[j]) {
max = array[j];
}
}
printf("smallest value is: %d", min);
printf("largest value is: %d", max);
return 0;
}
あなたの答えはあなたのエラーであると言われましたので、私は別のことを尋ねます:二次的な時間ではなく、最小値と最大値を見つけるのに線形時間を使う方法を見つけてみてください。 –
@MeikVtuneここには2次的なものは何もありません... 2つではなく1つの走査を意味しますか? – Quentin
@Quentin Yes :) –