Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>Analysis</h3> <p>There is a wealth of problems in these few lines:</p> <pre><code>pFile = fopen("argv[1]", "r"); fscanf(pFile, "%lf", &amp;N); while(!feof(pFile)) { fscanf(pFile, "%d", &amp;num); </code></pre> <ol> <li>You probably wanted to open the file designated by <code>argv[1]</code>, not a file called <code>argv[1]</code>. Remove the quotes.</li> <li>You didn't check that you were passed an argument.</li> <li>You don't check that <code>fopen()</code> succeeded, so you probably crashed in your first <code>fscanf()</code>.</li> <li>You don't check that <code>fscanf()</code> succeeded. It is also odd to read what should probably be an integer value into a <code>double</code>, but the notation used will work.</li> <li>You should not use <code>feof()</code> like that, especially if …</li> <li>You don't check that the second <code>fscanf()</code> succeeded, and it won't work properly (but might not report the problem) because …</li> <li>You are trying to read integers (<code>%d</code>) into a <code>double</code>.</li> </ol> <p>So, you should have written:</p> <pre><code>if (argc &lt;= 1) …report error and exit…(or use pFile = stdin)… FILE *pFile = fopen(argv[1], "r"); if (pFile == 0) …report error and exit… if (fscanf(pFile, "%lf", &amp;N) == 1) { for (int i = 0; i &lt; N; i++) { if (fscanf(pFile, "%lF", &amp;num) != 1) …report error, close file, and exit… …as before…more or less…subject to fixing any as yet undiagnosed errors… } } fclose(pFile); </code></pre> <p>Incidentally, you forgot to set <code>sum_sqs</code> to zero before you started adding to it, so you won't know what value you got. Also, if all the numbers are negative, you'll report that the maximum is 0; if all the numbers are positive, you'll report that the minimum is 0. Fixing that is a tad fiddly, but you could use <code>if (i == 0 || num &lt; min) min = num;</code> etc.</p> <hr> <h3>Linking Problem</h3> <p>Your linking problem (undefined reference to <code>sqrt()</code>) indicates that you are running on a system where you need to link the maths library; that is usually <code>-lm</code> on the end of the linking command line.</p> <hr> <h3>Synthesis</h3> <pre><code>#include &lt;stdio.h&gt; #include &lt;math.h&gt; int main(int argc, char*argv[]) { double average, num = 0, min = 0, max = 0, sum = 0, N, std_dev, sum_sqs = 0.0; if (argc &lt;= 1) { fprintf(stderr, "Usage: %s file\n", argv[0]); return 1; } FILE *pFile = fopen(argv[1], "r"); if (pFile == 0) { fprintf(stderr, "%s: failed to open file %s\n", argv[0], argv[1]); return 1; } if (fscanf(pFile, "%lf", &amp;N) == 1) { for (int i = 0; i &lt; N; i++) { if (fscanf(pFile, "%lF", &amp;num) != 1) { fprintf(stderr, "%s: failed to read number\n", argv[0]); return 1; } if (num &lt; min || i == 0) min = num; if (num &gt; max || i == 0) max = num; sum += num; sum_sqs += (num*num); } } fclose(pFile); average = sum/N; std_dev = sqrt((sum_sqs/N)-(average*average)); printf("Smallest: %7.2lf\n", min); printf("Largest: %7.2lf\n", max); printf("Average: %7.2lf\n", average); printf("Standard deviation: %7.3lf\n", std_dev); return(0); } </code></pre> <h3>Results</h3> <p>Given data file <code>data</code>:</p> <pre><code>5 4.34 23.4 18.92 -78.3 17.9 </code></pre> <p>The result of running the program is:</p> <pre><code>Smallest: -78.30 Largest: 23.40 Average: -2.75 Standard deviation: 38.309 </code></pre> <p>Those values mostly look plausible; my calculation of the standard deviation came to 42.83 (using a different tool altogether). The difference is between the sample standard deviation and the population standard deviation (a factor of √1.25) — so your value is OK as you calculated it.</p> <pre><code># Count = 5 # Sum(x1) = -1.374000e+01 # Sum(x2) = 7.375662e+03 # Mean = -2.748000e+00 # Std Dev = 4.283078e+01 # Variance = 1.834476e+03 # Min = -7.830000e+01 # Max = 2.340000e+01 </code></pre> <p>So, the code works for me. What result are you getting?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload