我做了一個數組程序,它讀入10個數字,並找到這些數字的最小值,最大值和平均值。如何讓數組知道是否有非數字輸入或它到達輸入的結尾?
現在,我希望程序在遇到非數字輸入或到達輸入末尾時輸出「沒有足夠的數字」。例如3 65 2 4個招呼 - > 「有沒有足夠的數字」
這裏是我到目前爲止的代碼:
#include <stdio.h>
#define LENGTH 10
int main (void) {
int array [LENGTH];
int i, max, min, sum;
float average;
printf("Enter vector of 10 numbers: ");
// inputting the numbers into an array
for(i = 0; i < LENGTH; i++) {
scanf("%d", &array [i]);
}
// Finding the minimum value
min = array [0];
for(i = 1; i < LENGTH; i++) {
if(min > array [i]) {
min = array [i];
}
}
printf("Minimum vector is %d\n", min);
// Finding the maximum value
max = array [0];
for(i = 1; i < LENGTH; i++) {
if(max < array [i]) {
max = array [i];
}
}
printf("Maximum vector value is %d\n", max);
// Finding the average of all values
sum = 0;
average = 0;
for(i = 0; i < LENGTH; i++){
sum = sum + array [i];
average = sum/LENGTH;
}
printf("Average vector value is %.1f\n", average); // %.1f prints the number with 1 decimal point
return 0;
}
[This'scanf'(and family)reference](http://en.cppreference.com/w/c/io/fscanf)可能派上用場。檢查函數*返回*。 –