Hello I wrote a function that continuously accepts integer inputs from the user until the user enters -999 (sentinel value). The function also identifies the highest and lowest value entered, the average of inputs. I already wrote a code for it but I can't seem to get the correct average value for it and need help revising it.
Here's my code:
#include <stdio.h>
int getMinMaxAve ( int *min, int *max, float *ave)
{
int nVal;
int nMin = 0;
int nMax = 0;
int nResult = 1;
int nCount = 0;
while (nVal != -999)
{
printf ("Value:");
scanf ("%d", &nVal);
if (nVal !=-999)
{
if (nVal > nMax)
nMax = nVal;
if (nVal < nMin)
nMin = nVal;
nResult += nVal;
nCount += 1;
}
}
*min = nMin;
*max = nMax;
*ave = nResult / nCount;
}
int main ()
{
int nMin;
int nMax;
int nResult;
float fAve;
nResult = getMinMaxAve (&nMin, &nMax, &fAve);
if (nMin == -999)
{
printf ("Min: NA
");
printf ("Max: NA
");
printf ("Average: NA
");
}
else
{
printf ("Min: %d
", nMin);
printf ("Max: %d
", nMax);
printf ("Average: %f
", fAve);
}
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…