Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A blank program

Status
Not open for further replies.

storygeek

Technical User
Jun 25, 2004
5
US
Situation: I typed down a code that's supposed to calculate statistics of the population with given values. There's no errors but when I run it I get a blank empty screen. I typed in the values but it still wouldn't work. Vertically and horizonally. And Horizonally split up with commas. Anybody's help would be greatly appreciated.
 
Where is your (minimal) code? Can we answer to absolutely abstract questions?..
I have a car. I can't use it. Whay's a problem? Help me, please...
 
#include <stdio.h>
#include <math.h>
main()
{
float x,/*current value input*/
max,
min,
sum,
mean,
sum_of_squares,
variance;
int count;

if (scanf("%f", &x) == EOF ) /* EMPTY? */
printf( "0 data items read\n");
else {
max = min = sum = x;
count = 1;
sum_of_squares =x * x;
while (scanf("%f", &x) != EOF)
{
count += 1;
if (x > max) /*new high?*/
max = x;
if (x < min) /* new low?*/
min = x;
sum += x;
sum_of_squares += x * x;
}
printf( "%d data items read\n", count);
printf("maximum value read = %f\n", max);
printf("minimum value read = %f\n", min);
printf( "sum of all values read = %f\n", sum);
mean = sum / count;
printf ("Mean = %f\n", mean);
variance = sum_of_squares / count - mean * mean;
printf("variance = %f\n", variance );
printf( "standard deviation = %f\n",
sqrt (variance));
}
}
I'm showing the whole program since I can't trace where the problem is.
 
Hi !!!

Please see this extract of 'man scanf()'.

The value EOF is returned if an input failure occurs before
any conversion such as an end-of-file occurs. If an error or
end-of-file occurs after con-version has begun, the number of
conversions which were successfully completed is returned.


Since your function never encounters a failure before conversion it does not reach EOF. remember: An empty input is not an input failure, in that case scanf would not return EOF. To Return EOF please press control-D, if you are using *nix Environment. If you are using DOS/Windows, please excuse.

Regards,
SwapSawe.
 
SwapSawe is right. On Windows press Ctrl-Z to terminate console stdin.
It seems more better using (scanf(...) != 1) stop condition. Initialize all variables then use scan loop:
Code:
float x,
      mean = 0,
...
      ;
int   count;
if (scanf(...) != 1)
{
   /* fail: no values or errors */
   exit(1);
}
minval = maxval = x;
for (count = 1; scanf(...) == 1; ++count)
{
  /* increase sum vars etc */
}
if (count > 1)
{
   /* calculate and print */
}
else /* only 1 value report */
Avoid using min and max as var names: there are library functions/macros with that names...
As far as I know a true estimation of variance is
Code:
D = (1/(n-1))*sum((xk - M)^2);
Consult your math stat book...

Use doubles instead of floats (correct scanf format spec to %lf): float arith is slower than double on most of CPU and printf args automatically converted in doubles (what for in that case?).

Add else before if (x < minval): no need to check this cond when 1st if cond was true...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top