Question

In: Computer Science

A series of numerical values have been obtained from a temperature sensor operating in a refrigeration...

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.

  • It should produce a statistical summary containing:
    • 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.

  • You are advised to use the code skeleton below as the starting point for your solution.

    #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;
    }

  • The program should process any data file correctly, but to help in your development, sample input data file sample.txt is provided below:

    -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

  • When compiled as program example and executed with the following command:

    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

Solutions

Expert Solution

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:-


Related Solutions

When calibrating a temperature sensor using a precise reference instrument, the following relationship is obtained between...
When calibrating a temperature sensor using a precise reference instrument, the following relationship is obtained between reference temperature T (degrees C) and sensor value Y (mV).   T (C) Y (mV) 100      33.14269 110      42.69237 120      53.37345 130      65.13426 140      78.14743 150      92.02897 160     107.12636 170     123.26571 180     140.52200 190     158.85228 200     178.16609 Estimate the parameters b0, b1, b2 for the square regression curve y = b0 + b1 T + b 2 T 2
the following data have been obtained from the accounting records of filo corporation for the year...
the following data have been obtained from the accounting records of filo corporation for the year ended 31 deceber 2014. ACCOUNT: sales 860 000, purchaces of raw material 170 000, direct labour 220 000, manufacturing overhead 210 000, admistrative expenses 120 000,selling expenses 170 000, raw material inventory begining 10 000, raw material inventory ending 50 000, work in progress begining 80 000,work in progress ending 60 000, finished good inventory begining 110 000, finished good inventory ending 100 000....
A geyser is a hot spring in which water occasionally boils, sending a tall column of water and steam into the air. A temperature sensor is used to monitor the temperature near the geyser. The data from the sensor is sent to a microprocessor.
A geyser is a hot spring in which water occasionally boils, sending a tall column of water and steam into the air. A temperature sensor is used to monitor the temperature near the geyser. The data from the sensor is sent to a microprocessor Explain why the data from the sensor has to be changed before it is read by the microprocessor. Describe three advantages of using sensors and microprocessors to monitor the temperature in geysers rather than using manual...
A random sample from a given population gives the following numerical values: x1, y2, · ·...
A random sample from a given population gives the following numerical values: x1, y2, · · · , xn: (a) What is meant by the expression “random sample”? (b) Prove that ∑n j=1 (xi − x¯) = 0. (c) Indicate the type of measures and state one advantage and disadvantage of the in the following table: Measure Type Advantage Disadvantage Mean Median Mode Range
After graduating from University you obtained a job and have been working there for three years....
After graduating from University you obtained a job and have been working there for three years. You recently obtain a promotion and a bonus of $20,000 that you decided to use to buy a house. The cost of the property is $100,000 and a down payment of 20% is required. There are $1000 of closing costs (added to the loan) and no points. The mortgage loan term is 30 years and the interest rate is fixed at 3% per year...
During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first...
During lab 4, we have seen numerical implementation of Fourier Series for periodic signals. As first part of this assignment, you need to write a Matlab function that would take an array representing a single period of a signal (x), corresponding time array (t), and return the Fourier Series coefficients (Ck) in exponential form. The function should also be able to take two (2) optional input arguments: number of Fourier coefficients (Nk) and plot option (p). Use the template ‘fourier_series_exp.m’...
Supose you have a table with the next values that represent the change of temperature in...
Supose you have a table with the next values that represent the change of temperature in 10 hours, based on the data of the table respond the next questions Hour Temperature (K) 9:00 287.5 10:00 289.3 11:00 290.75 12:00 292.8 13:00 294.5 14:00 296.37 15:00 298.15 16:00 300.00 17:00 301.8 18:00 303.5 Considering that the measurements can only be taken at those hours and there can be small errors in them, if you were asked to know what was the...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 11 On average the observed y deviate from the predicted y by, a 10.73 b 10.04 c 9.53 d 8.76 12 Sum of squares total (SST) is, a 1096.3 b 1178.8 c 1296.7 d 1361.5...
Below are the values for two variables x and y obtained from a sample of size...
Below are the values for two variables x and y obtained from a sample of size 5. We want to build a regression equation based the sample data. ŷ = b₀ + b₁x y x 16 5 21 10 8 6 28 12 53 14 1 The sum product of x and y is, a 1416 b 1451 c 1466 d 1481 2 The value in the numerator of the formula to compute the slope of the regression equation is,...
Considering the nature of the Charpy impact test can the impact energy values obtained from it...
Considering the nature of the Charpy impact test can the impact energy values obtained from it be used directly in mechanical design? Explain your answer. ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT