#include <stdio.h>
int main(void){
int inputNumber;
int counter=0;
int totalValue;
int arr[counter];
int avg;
puts("Please enter any number of positive whole numbers you would like to be averaged. Enter ' -1 ' when you are finished for your result.\n");
while(scanf("%d\n", &arr[counter])){
if(arr[counter] = -1){
break;
}
if(arr[counter] > 0){
totalValue += arr[counter];
++counter;
}
else if(arr[counter]<=0){
puts("Please enter a positive number.");
}
else{
}
}
avg = totalValue/counter;
printf("The average of your entered values is: %d", avg);
return 0;
}
我尝试过很多东西试图阻止它,虽然这可能来自缺乏知识,除了创建一个巨大的数组之外,还有什么方法可以做到这一点吗?
我尝试使用带有calloc()的动态数组,但我遇到了同样的错误。我不确定此方法中还有哪些可用选项。
代码应该取平均值&#34; n&#34;用户输入的值。
答案 0 :(得分:0)
您不需要阵列。
快速&amp;你想做的很脏但更容易
int number = 0;
int counter = 0;
int total = 0;
while (number != -1)
{
total += number;
++counter;
scanf("%d", &number);
}
printf("average = %d\n", total / (counter - 1) );