In: Computer Science
A series of numerical values have been obtained from a temperature sensor operating in a refrigeration unit. You have been tasked to use the C programming language to develop a program which will query the data series to compute a statistical summary. Upload your C source file to answer the question. The following conditions apply:
Your program will read data via the standard input stream, and generate a report to the standard output stream.
The data will be supplied in a plain text file.
Numeric values are represented as decimal-format integers, separated by white space.
Values of interest occupy the range {-10,…,30}.
Due to sampling errors, the series also contains outliers – integers which fall outside the range of interest.
Your program must parse the input file and count the number of occurrences of each value in the range of interest, ignoring any value outside that range.
The lowest occurrence count, and all values for which the occurrence count is equal to the lowest count.
The second-lowest occurrence count, and all values for which the occurrence count is equal to the second-lowest count.
A complete listing showing the number of occurrences of each value in the range of interest.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define MAX_VAL 30
#define MIN_VAL -10
// Declare and initialise any additional global variables here, as
required.
void process(int x) {
// Process one value here.
}
void post_process() {
// Compute derived results here.
}
void print() {
// Print results here
}
int main(void) {
int x;
while (1 == scanf("%d", &x)) {
process(x);
}
post_process();
print();
return 0;
}
-6 23 29 15 32 39 -13 12 24 -7 33 32 40 22 35 -2 20 25 27 36 -18 27 -7 35 -11 25 -16 24 8 10 31 10 18 3 31 2 4 -2 40 -18 18 -1 -8 33 21 -19 -8 21 14 -14 23 32 -8 12 -18 30 -2 -4 37 29 -18 37 8 24 32 10 32 -12 6 14 28 30 13 -13 30 -11 22 20 32 22 16 36 29 -8 13 -19 13 -6 -8 37 1 -10 -14 -14 34 -9 -1 21 -13 5 -8 24 -4 31 3 15 38 -8 -13 11 5 21 25 -13 6 22 40 -11 13 -16 30 -18 -12 28 11 -6 10 12 -10 -14 40 20 -2 -5 -16 -17 4 -12 10 24 -12 27 15 1 37 1 6 -2 14 20
cat sample.txt | ./example
your program will ideally produce this output:
The lowest count is 0, at -3, 0, 7, 9, 17, 19, 26
The second-lowest count is 1, at -9, -5, 2, 16
The complete histogram is:
-10 -> 2
-9 -> 1
-8 -> 7
-7 -> 2
-6 -> 3
-5 -> 1
-4 -> 2
-3 -> 0
-2 -> 5
-1 -> 2
0 -> 0
1 -> 3
2 -> 1
3 -> 2
4 -> 2
5 -> 2
6 -> 3
7 -> 0
8 -> 2
9 -> 0
10 -> 5
11 -> 2
12 -> 3
13 -> 4
14 -> 3
15 -> 3
16 -> 1
17 -> 0
18 -> 2
19 -> 0
20 -> 4
21 -> 4
22 -> 4
23 -> 2
24 -> 5
25 -> 3
26 -> 0
27 -> 3
28 -> 2
29 -> 3
30 -> 4
Complete code in C:-
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Defining macros
#define MAX_VAL 30
#define MIN_VAL -10
// Defingin global variables
// count is an array of size 41, it stores occurence of each number
in range
// range is [-10, 30] so i am adding 10 in every number to make it
non negative.
int count[41];
// 'Min' for getting minimum occurence
int Min = 100000;
// 'secondMin' is for getting second minimum occurence.
int secondMin = 100000;
// Processing a number, incrementing its count.
void process(int x) {
if(x >= MIN_VAL && x <= MAX_VAL) {
count[x+10]++;
}
}
// Getting minimum and second minimum occurence value
void post_process() {
for(int i = 0; i < 41; i++) {
if(count[i] < Min) {
secondMin =
Min;
Min =
count[i];
}
}
}
// Printing data
void print() {
printf("The lowest count is %d, at ", Min);
for(int i = 0; i < 41; i++) {
if(count[i] == Min) {
printf("%d, ",
i-10);
}
}
printf("\n");
printf("The second-lowest count is %d, at ",
secondMin);
for(int i = 0; i < 41; i++) {
if(count[i] == secondMin) {
printf("%d, ",
i-10);
}
}
printf("\n");
printf("The complete histogram is:\n");
for(int i = 0; i < 41; i++) {
printf("%d -> %d\n", i-10,
count[i]);
}
}
int main(void) {
// Variable 'x' will be used to read a number from
text file.
int x;
// Initializing 'count[]' with all zeros.
for(int i = 0; i < 41; i++) {
count[i] = 0;
}
// Reading input number and processing them using
process() function
while(scanf("%d", &x) == 1) {
process(x);
}
// Calling 'post_process()' function
post_process();
// Calling 'print()' function.
print();
return 0;
}
Screenshot of output:-