在我的代碼中,程式不允許輸入負數,程式會停止讀取,然后計算最大值、最小值和平均值。那是我的代碼
#include <stdio.h>
int main(void) {
int age[10] = {0}; // initalized an array
printf("Please enter ages: \n"); // allow user to enter numbers
for (int i = 0 ;i < 10; i ) {
scanf("%d",&age[i]);
if (age[i] < 0) { // if it is negative number, it is should stop reading
break;
}
else if (age[i] >= 0) {
continue;
}
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
int length = sizeof(age) / sizeof(age[0]);
for (int j = 0; j < length; j ) {
if (maximum < age[j]) {
maximum = age[j];
}
else if (minimum > age[j]) {
minimum = age[j];
}
average = age[j];
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}
請輸入年齡:5 -1 預期結果:max:5;min:5,average:5;
實際結果:最大值:5;最小值:-1,平均值:0.4;
這是我遇到的一個問題,代碼不應該接受任何負值。
謝謝你們。但如果我添加 age[i] = 0; 然后打破;平均值將等于 0.5。
uj5u.com熱心網友回復:
- 你不需要陣列。
- 您不需要回圈變數和長度。
- 更適合
? :用于更新最小值/最大值。 - 你不需要兩個回圈
- 需要查看 的
int回傳值scanf(),表示掃描成功的專案數,應該是1。我將把它留給你/OP 添加(提示:用 -loop 替換for-loopwhile以避免length再次添加單獨的變數)。
int main(void)
{
printf("Please enter ages: \n");
int minimum = INT_MAX;
int maximum = 0;
int sum = 0;
int count = 0;
for (count = 0; count < 10; count )
{
int age;
scanf("%d", &age);
if (age < 0)
{
break;
}
sum = age;
minimum = (age < minimum) ? age : minimum;
maximum = (age > maximum) ? age : maximum;
}
if (count > 0)
{
printf("Min: %d\n", minimum);
printf("Max: %d\n", maximum);
printf("Avg: %.1f\n", (float)sum / count);
}
else
{
printf("You didn't enter (valid) age(s).\n");
}
return 0;
}
uj5u.com熱心網友回復:
您的方法過于復雜和錯誤。
你要這個:
...
int length = 0; // declare length here and initialize to 0
for (int i = 0; i < sizeof(age) / sizeof(age[0]); i ) {
scanf("%d", &age[i]);
if (age[i] < 0) // if it is negative number, it is should stop reading
break;
length ; // one more valid number
}
// now length contains the number of numbers entered
// the rest of your code seems correct
您可能還需要處理沒有輸入數字的特殊情況,例如:唯一輸入的是-1. 當沒有數字時,計算平均值或最大/最小數字是沒有意義的。
uj5u.com熱心網友回復:
一個可能的解決方案可能是:
(更正寫在注釋代碼中)
#include <stdio.h>
int main(void){
int arraySize = 10;
int age[arraySize]; //initialize not required
//the number of existing values inside the array (effective length)
int length = 0;
printf("Please enter ages: \n"); // allow user to enter numbers
for(int i=0; i<arraySize; i ){
scanf("%d",&age[i]);
// if it is negative number, it is should stop reading
if(age[i]<0){ break; }
//the else-if is not required
//but, if the compiler goes here,
//it means that the value is acceptable, so
length ;
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
for(int j=0; j<length; j ){
if(maximum<age[j]){ maximum = age[j]; }
else if(minimum>age[j]) { minimum = age[j]; }
average = age[j];
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}
uj5u.com熱心網友回復:
OP 的主要問題是第二個回圈迭代 10 次而不是i次(輸入非負數的次數。
為了好玩,讓我們嘗試一個非浮點解決方案,因為它確實是一個整數問題。
不需要用于存盤值的陣列。
#include <limits.h>
#include <stdio.h>
int main(void) {
// Keep track of 4 things
int min = INT_MAX; // Set min to the max int value.
int max = INT_MIN;
long long sum = 0; // Use wide type to cope with sum of extreme ages.
int count = 0;
#define INPUT_N 10
printf("Please enter ages: \n");
for (count = 0; count < INPUT_N; count ) {
int age;
if (scanf("%d", &age) != 1) {
fprintf(stderr, "Missing numeric input.");
return EXIT_FAILURE;
}
if (age < 0) {
break;
}
if (age < min) min = age;
if (age > max) max = age;
sum = age;
}
if (count == 0) {
fprintf(stderr, "No input.");
return EXIT_FAILURE;
}
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
// Could use FP and
// printf("Average: %.1f\n", 1.0 *sum / count);
// But for fun, how about a non-FP approach?
#define SCALE 10
#define SCALE_LOG 1
sum *= SCALE; // Scale by 10 since we want 1 decimal place.
// Perform a rounded divide by `count`
long long average_scaled = (sum count/2) / count;
// Print the whole and fraction parts
printf("Average: %lld.%.*lld\n",
average_scaled / SCALE, SCALE_LOG, average_scaled % SCALE);
return 0;
}
uj5u.com熱心網友回復:
首先,你必須記錄你輸入了多少個正數。那么 的值length將是正確的。
其次,對于第二個for回圈,j必須小于正年齡數。因此,您不會age[j]在average.
您可以簡單地修改第二個 for 回圈。
#include <stdio.h>
int main(void) {
int age[10] = {0}; // initalized an array
printf("Please enter ages: \n"); // allow user to enter numbers
int length = 0;
for (int i = 0 ;i < 10; i ) {
scanf("%d",&age[i]);
if (age[i] < 0) { // if it is negative number, it is should stop reading
break;
}
else if (age[i] >= 0) {
length ;
continue;
}
}
int maximum = age[0];
int minimum = age[0];
float average = 0.0;
for (int j = 0; j < length; j ) {
if (maximum < age[j]) {
maximum = age[j];
}
else if (minimum > age[j]) {
minimum = age[j];
}
if ( age[j] > 0.0 )
{
average = age[j];
}
}
average = average / length;
printf("%d\n", maximum);
printf("%d\n", minimum);
printf("%.1f\n", average);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473148.html
標籤:C
