Home>

In a program that uses a while statement to find a value below the average value, I want to take the average of the values ​​up to that point when a value below 0 is entered. How should I set it up? (Does not include 0 or less)
I have no idea, so I would appreciate it if you could tell me.
For the time being, when I thought about it as a trial, it became as follows.
To be honest, I think that what i am doing is completely different, but please point out that as well.
* I don't know how to use the while statement and break.

include define NUM 10000

int main (void)
{
int Num = 0;
double test [NUM];
double dnumSum = 0;
double dnumAve = 0;
int i = 1;
int j;

printf ("Enter the number of data. \ N");
scanf ("% d",&Num);

while (Num<= 0)
{
printf ("There is an error in the input. Please enter it again. \ N");
scanf ("% d",&Num);
}

while (test [i]<0) {

printf ("Enter the value of the data. \ N");
scanf ("% lf",&test [i]);
i ++;
break;

dnumSum + = test [i];

}

dnumAve = (double) dnumSum/Num;

for (j = 0;j if (test [j]<= dnumAve) {
printf ("Data below the mean is% f. \ N", test [j]);

}
}

return 0;
}

  • Answer # 1

    How to use while (expression) {} ,,
    ->Repeat inside {} and execute while the expression is true.

    while (test [i]<0) {

    test [i]While it is true, it is executed, but since the value of the first test [i] is not set, it is unknown whether it is the result. Maybe,NumI just want to repeat it
    ->while (iIs not it.
    In addition, it should be notediShould the default setting of be 0? (It's 1)

    Since the input is less than 0, would break look like this?
    if (test [i]<= 0) break;(i ++Will be after this)

    Also, to find the average

    dnumAve = (double) dnumSum/Num;

    Is invalid if the input is 0 or less,
    dnumAve = (double) dnumSum/i;Is not it?

    I just noticed it.
    However, this doesn't take into account the case where the first input is less than or equal to 0, but for now.