[英]C - how to display the numbers in an array that are lower than the average value of the numbers in the array
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i=0;
int ray[20];
float sum=0.00, average;
for (i=0; i<20; i++)
{
printf("Enter integer #%d",i+1);
scanf ("%d", &ray[i]);
sum=(sum+ray[i]);
}
average=(sum/20);
printf("Average = %.2f", average);
if (ray[i] < average)
{
printf("The followiing values are less than the average: %d", ray[i]);
}
system("pause");
}
代碼可以正常運行,並給出輸入整數的正確平均值,但小於平均值的值顯示為-858993460
您正在嘗試打印ray[i]
,但是i
當前是20
,它在數組的索引之外。 您是否要在該if語句周圍復制for循環?
您的代碼中的這一部分:
for (i=0; i<20; i++)
{
printf("Enter integer #%d",i+1);
scanf ("%d", &ray[i]);
sum=(sum+ray[i]);
}
//Now i = 20,that is why it left loop
average=(sum/20);
printf("Average = %.2f", average);
if (ray[i] < average) //You are saying if ray[20]<average
{
printf("The following values are less than the average: %d", ray[i]);
}
現在ray[20]
不在范圍內,因為有20
元素並且數組索引從0
開始到19
,因此ray[20]
超出了訪問范圍。 我強烈建議您看一下這個問題,以更好地理解超出范圍的陣列有多危險? 。
其次,您要在ray[i] < average
情況下打印所有ray[i]
,因此應運行如下循環
printf("The following values are less than the average:");
for(i = 0; i<20; i++) // i starts from 0 and increase with run of loop
// and loop stops when i>19 so you are not
// accessing forbidden area.
{
if (ray[i] < average){ //check if ray[i] is less than average
printf("%d\n", ray[i])
}
}
這一切使:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i=0;
int ray[20];
float sum=0.00, average;
for (i=0; i<20; i++)
{
printf("Enter integer #%d",i+1);
scanf ("%d", &ray[i]);
sum=(sum+ray[i]);
}
average=(sum/20);
printf("Average = %.2f", average);
printf("The following values are less than the average:");
for(i = 0; i<20; i++) // i starts from 0 and increase with run of loop
// and loop stops when i>19 so you are not
// accessing forbidden area.
{
if (ray[i] < average){ //check if ray[i] is less than average
printf("%d\n", ray[i]);
}
}
system("pause");
}
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i=0;
int ray[20];
float sum=0.00, average;
for (i=0; i<20; i++)
{
printf("Enter integer #%d: ",i+1);
scanf ("%d", &ray[i]);
sum=(sum+ray[i]);
average=(sum/20);
if (ray[i] < average)
{
printf("The followiing values are less than the average: %d", ray[i]);
}
}
printf("Average = %.2f", average);
system("pause");
}
在每個低於當時平均值的值之后,這給出了“以下整數低於平均值”部分,但是我需要它在最后一起顯示低於平均值的值。
正如其他人已經指出的那樣,您的索引超出范圍。 發生這種情況是因為您已經迭代for (i=0; i < 20; i++)
,這意味着一旦離開了for語句,您將處於i == 20
。 您的數組分配有20個位置,因此您可以從索引0到19進行訪問。之所以給出笨拙的值,是因為您正在訪問“垃圾箱”,換句話說,就是具有無法預測(或幾乎)值的無效位置。
好的,一旦您正確設置了該索引,您仍然需要一種算法來顯示低於平均值的數字。 您不能只將if語句復制到循環中,因為只有在遍歷所有值之后才知道真實的平均值(這樣做很好)。
因此,您想要的是另一個遍歷整個數組以及其中的if語句的循環(嗯,還有其他方法可以執行此操作而無需再次運行所有值,例如對數組進行排序 )
所以,這是我會建議您的算法
#include <stdio.h>
#include <stdlib.h>
#define array_size 20
void main()
{
int i;
int ray[array_size];
int sum=0;
float average;
for (i=0; i<array_size; i++)
{
printf("Enter integer #%d: ",i+1);
scanf ("%d", &ray[i]);
sum += ray[i];
}
average=(sum/(float)array_size);
printf("Average = %.2f\n", average);
printf("The following values are less than the average: ");
for (i = 0; i < array_size; i++)
{
if (ray[i] < average)
{
printf("%d ", ray[i]);
}
}
system("pause");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.