Note that there are some explanatory texts on larger screens.

plurals
  1. POCode in C to find largest and smallest numbers from text file
    primarykey
    data
    text
    <p>I am attempting to write a program that takes a user's inputted text file and returns the largest number, smallest number, average of the numbers and the standard deviation of the numbers. The text file that we input is formatted as such (with the first number being "N", or the total number of numbers, and the second row the list of all the numbers):</p> <p>5 </p> <p>4.34 23.4 18.92 -78.3 17.9</p> <p>So far, this is my code</p> <pre><code>int main(int argc, char*argv[]) { double average, num = 0, min = 0, max = 0, sum = 0, N, std_dev, sum_sqs; FILE * pFile; pFile = fopen("argv[1]", "r"); fscanf(pFile, "%lf", &amp;N); while(!feof(pFile)) { fscanf(pFile, "%d", &amp;num); if (num &lt; min) min = num; if (num &gt; max) max = num; sum += num; sum_sqs += (num*num); } average = sum/N; std_dev = sqrt((sum_sqs/N)-(average*average)); printf("Smallest: %.2lf\n", min); printf("Largest: %.2lf\n", max); printf("Average: %.2lf\n)", average); printf("Standard deviation: %.3lf\n", std_dev); return(0); } </code></pre> <p>Currently, the compiler does not let me get past an error about an undefined reference to sqrt and i cannot figure out what's wrong. Thank you in advance to everybody who takes the time to respond! I really appreciate any help, I am still getting the hang of C. If my code is not doing what I intend it to do please dont hesitate to correct me!</p> <p>Updated the messy part to below. Still not sure what exactly im doing with the rest though haha pFile = fopen(argv[1], "r"); fscanf(pFile, "%lf", &amp;N);</p> <pre><code> if (fscanf(pFile, "%lf", &amp;N) == 1) { for (int i = 0; i &lt; N; i++) { if (fscanf(pFile, "%lf", &amp;num) == 1) if (num &lt; min) min = num; if (num &gt; max) max = num; sum += num; sum_sqs += (num*num); } average = sum/N; std_dev = sqrt((sum_sqs/N)-(average*average)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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