4 a Write a program that will find the smallest largest and

4. a. Write a program that will find the smallest, largest, and average values in a collection of N numbers. Get the value of N before scanning each value in the collection of N numbers Modify your program to compute and display both the range of values in the data collection and the standard deviation of the data collection. To compute the standard deviation, accumulate the sum of the squares of the data (sum_squares) in the main loop. After loop exit, use the formula b. aresaverage standard deviation sum squares

Solution

// C code read from file and compute average

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int max(int array[], int size)
{
    int i;
    int m = array[0];
    for (i = 1; i < size; i++)
    {
           if(array[i] > m)
            m = array[i];
    }
    return m;
}

int min(int array[], int size)
{
    int i;
    int m = array[0];
    for (i = 1; i < size; i++)
    {
           if(array[i] < m)
            m = array[i];
    }
    return m;
}

double avg(int array[], int size)
{
    int i;
    double sum = 0;
    for (i = 0; i < size; i++)
    {
           sum = sum + array[i];
    }
    return sum/size;
}

double sd(int array[], int size)
{
    int i;
    double average = avg(array,size);
    double dev = 0;
    for (i = 0; i < size; i++)
    {
        dev = dev + array[i]*array[i];
    }

    dev = dev/size - (average*average);
    return sqrt(dev);
}


int main()
{
    int i = 0;
    FILE *myFile;
    myFile = fopen(\"input.txt\", \"r\");

    //read file into array
    int array[200];

    if (myFile == NULL)
    {
        printf(\"Error Reading File\ \");
        exit (0);
    }

    while (fscanf(myFile, \"%d\", &array[i]) == 1)
    {
      i++;
    }
  
    int size = i;
    fclose(myFile);

    printf(\"Max: %d\ \",max(array,size) );
    printf(\"Min: %d\ \", min(array,size));
    printf(\"Average: %lf\ \", avg(array,size));
    printf(\"Standard Deviation: %lf\ \",sd(array,size) );

    return 0;
}

/*

input.txt
180
71
19
13
152
1
190
89
166
48
35
64
59
156
9
53
72
183
167
83
23
159
83
76
84
156
38
78
183
83
200
114
153
171
127
56
171
116
145
136
116
131
151
126
87
159
130
158
142
48
193
116
6
75
191
89
183
29
166
165
63
118
79
167
88
5
22
58
72
118
145
139
49
95
65
135
53
146
44
146
194
36
61
199
63
52
88
45
32
5
161
46
122
191
12
161
147
185
170
171
103
114
109
103
8
125
37
13
71
32
110
64
68
171
14
82
174
101
78
157
58
38
2
179
181
165
92
127
149
61
97
3
175
158
57
134
34
93
98
56
125
8
71
144
130
85
177
103
185
54
59
42
43
12
173
23
176
64
150
76
76
198
31
2
107
87
136
141
132
185
196
8
144
67
103
73
151
79
175
87
132
185
81
174
196
53
149
123
68
50
199
143
47
181
145
154
67
32
46
198


output:
Max: 200
Min: 1
Average: 103.815000
Standard Deviation: 57.260202

*/

 4. a. Write a program that will find the smallest, largest, and average values in a collection of N numbers. Get the value of N before scanning each value in t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site