Explenation of these codes ? #include <stdio.h> #include <stdbool.h> float neare
ID: 3864193 • Letter: E
Question
Explenation of these codes ?
#include <stdio.h>
#include <stdbool.h>
float nearestNumber(float *sensorReading, float value);
bool isWithinReadings(float value, float max, float min);
bool isActualReading(float *sensorReading, float value);
bool withinLowerQuart(float lowerQuartile, float min, float value);
bool withinUpperQuart(float upperQuartile, float max, float value);
main()
{
int i, j, k, n, uinput; //counters for the arrays
float a; //temporary value for sorting
float value, res_nn;
float number[10];
bool res, res_iwr, res_wlq, res_wuq;
uinput = 0;
n = 100; // n is the size of the sensorReading array
k = 0; //initiate variable to zero
float sensorReading[] = { 19,40.5,18.3,17.9,32.5,30.7,36.2,20.6,38,26.4,
22.6,18.8,6.1,44.4,20.1,21.8,18.5,12.6,18.1,40.3,
34.1,35.5,45.6,42.1,17.7,2.9,3.5,36.5,36.4,27.1,
43.4,5.6,33.9,39.1,0.8,31.4,14.9,26.6,45.5,30.1,
19.5,47.5,47.9,44.8,0.6,35.6,5.1,38.8,48.5,24.6,
32.3,50,48.4,6.8,18,29.1,37.4,24.8,44.5,19.6,
9,10.6,46.3,34.5,21.6,20.3,28,10,10.8,16.4,
45.4,25,34.6,31.8,1.1,40,18.2,32.6,11,7.5,
17.1,39.8,40.4,40.8,45.1,32,5.5,45.7,9.4,22.9,
43.2,7.3,23.7,8.6,4.1,24.2,18.9,14.2,31.6,7.0 };
float firsthalf[50], secondhalf[50]; //two arrays to calculate quartiles
float median, upperQuartile, lowerQuartile, max, min;
//sort array in ascending order from smallest to biggest value
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (sensorReading[i] > sensorReading[j])
{
a = sensorReading[i];
sensorReading[i] = sensorReading[j];
sensorReading[j] = a;
} //end if
} //end j loop
} //end i loop
//display array after sorting
printf("The numbers are sorted in ascending order ");
/*for (i = 0; i < n; ++i)
printf("%d - %.1f ", i, sensorReading[i]);
printf("number of readings: %d ", count); // to check that number of readings is 100 */
median = (sensorReading[n / 2] + sensorReading[n / 2 + 1]) / 2.0; //calculate median
//median = a[n / 2 + 1];
printf("Median = %.1f ", median); // display median
max = sensorReading[n-1]; // max value is the last number of sorted array
min = sensorReading[0]; // min value is the first number of sorted array
printf("Max = %.1f ", max); //display max
printf("min = %.1f ", min); //display min
//CALCULATING QUARTILES
//check this link to understand the calculation:
//en.wikipedia.org/wiki/Quartile
//split main array into FIRST half. size of array = 50
for (i = 0; i < n / 2; ++i)
firsthalf[i] = sensorReading[i];
/*printf("First half "); //pirnt First half - for checking
for (i = 0; i < n/2; ++i)
printf("%d - %.1f ", i, firsthalf[i]);*/
lowerQuartile = (firsthalf[25] + firsthalf[26]) / 2.0; //calculate lower quartile
printf("lower Quartile: %.1f ", lowerQuartile); //display lower quartile
//split main array into second half. size of array = 50
for (i = n / 2 ; i < n; ++i)
{
secondhalf[k] = sensorReading[i];
k++;
}
/*printf("Second half "); //pirnt Second half - for checking
for (k = 0 ; k < n/2; ++k)
printf("%d - %.1f ", k, secondhalf[k]);*/
upperQuartile = (secondhalf[25] + secondhalf[26]) / 2.0; //calculate upper quartile
printf("Upper Quartile: %.1f ", upperQuartile); //print upper quartile
while (uinput != 100)
{
printf(" ");
printf("--MENU-- ");
printf("Enter 1 if you are looking for one value ");
printf("Enter 2 if you are looking for 10 values ");
printf("Enter 100 to EXIT program ");
scanf("%d", &uinput);
if (uinput == 1)
{
printf("Enter the value: ");
scanf("%f", &value);
printf("The value entered is %.2f ", value);
res = isActualReading(sensorReading, value);
if (res == true)
{
res_wlq = withinLowerQuart(lowerQuartile, min, value);
res_wuq = withinUpperQuart(upperQuartile, max, value);
}
if (res != true)
{
res_iwr = isWithinReadings(value, max, min);
if (res_iwr == true) {
res_nn = nearestNumber(sensorReading, value);
printf("Nearest lower Number is: %f ", res_nn);
res_wlq = withinLowerQuart(lowerQuartile, min, value);
res_wuq = withinUpperQuart(upperQuartile, max, value);
}
}
//uinput = 1;
//printf("input is %d ", uinput);
}// end if equals 1
if (uinput == 2)
{
for (i = 0; i < 10; ++i)
scanf_s("%f", &number[i]);
for (i = 0; i < 10; ++i)
{
printf("Search results for %.2f ", number[i]);
res = isActualReading(sensorReading, number[i]);
if (res == true)
{
res_wlq = withinLowerQuart(lowerQuartile, min, number[i]);
res_wuq = withinUpperQuart(upperQuartile, max, number[i]);
}
if (res != true)
{
res_iwr = isWithinReadings(number[i], max, min);
if (res_iwr == true) {
res_nn = nearestNumber(sensorReading, number[i]);
printf("Nearest lower Number is: %.2f ", res_nn);
res_wlq = withinLowerQuart(lowerQuartile, min, number[i]);
res_wuq = withinUpperQuart(upperQuartile, max, number[i]);
}
}
printf("--------------------------------------- ");
printf(" ");
}//end loop
}//end if equals 2
if ((uinput != 1) && (uinput != 2) && (uinput != 100)) {
printf("input is %d ", uinput);
printf("ERROR: The value entered is not 1, 2 or 100 ");
}
//system("pause"); //make console screen pause to see results
}
}
float nearestNumber(float *sensorReading, float value)
{
int i;
for (i = 0; i < 100; i++) {
if (sensorReading[i] > value)
return sensorReading[i-1];
}
}
bool isActualReading(float *sensorReading, float value)
{
int i;
for (i = 0; i < 100; i++) {
if (sensorReading[i] == value)
{
printf("Match Found! The number is one of the sensor readings ");
return true;
}//end if
} //end for
printf("NOT Found! The number is NOT a sensor readings ");
return false;
}
bool isWithinReadings(float value, float max, float min)
{
if (value > max) {
printf("Value is not in the range. It is above range of data ");
return false;
}
if (value < min) {
printf("Value is not in the range. It is BELOW range of data ");
return false;
}
else
{
printf("Value is in the range. ");
return true;
}
}
bool withinLowerQuart(float lowerQuartile, float min, float value)
{
if (value >= min && value <= lowerQuartile)
{
printf("Value is within Lower Quartile ");
return true;
}
else {
printf("Value is NOT within Lower Quartile ");
return false;
}
}
bool withinUpperQuart(float upperQuartile, float max, float value)
{
if (value <= max && value >= upperQuartile)
{
printf("Value is within Upper Quartile ");
return true;
}
else {
printf("Value is NOT within Upper Quartile ");
return false;
}
}
Explanation / Answer
Basically what this program does is, it contains an array of sensor readings of length 100. First, it sorts the array in ascending order and then finds median, max, min, lowerquartile and upperquartile values. After finding all of these values, it asks for user input.
If the user inputs 1, it gets a single number from the user and then finds whether this value is an actual reading or it falls within the range of the sensor readings, if so whether in lower or upper quartile.
If the user inputs 2, it gets 10 values from the user and does the same what it does when the user inputs 1, but for all 10 values.
If the user inputs 100, the program exits.
If some other values than 1, 2, or 100 is entered, it keeps asking the user to input until one fo these is entered.
PROGRAM CODE:
//Header file declaration
//stdio - for standard input and output
#include <stdio.h>
// stdbool - for boolean variable
#include <stdbool.h>
//declaration of function protoypes that are used in this program
float nearestNumber(float *sensorReading, float value);
bool isWithinReadings(float value, float max, float min);
bool isActualReading(float *sensorReading, float value);
bool withinLowerQuart(float lowerQuartile, float min, float value);
bool withinUpperQuart(float upperQuartile, float max, float value);
//main function - processing the sensor readings to fund out the max, min , lowerQuartile and upperQuartile values
// also the arrays is sorted in ascending order
main()
{
//these are just values to store intermediate results.
int i, j, k, n, uinput; //counters for the arrays
float a; //temporary value for sorting
float value, res_nn;
float number[10];
bool res, res_iwr, res_wlq, res_wuq;
uinput = 0;
n = 100; // n is the size of the sensorReading array
k = 0; //initiate variable to zero
//this is the sensor reading array
float sensorReading[] = { 19,40.5,18.3,17.9,32.5,30.7,36.2,20.6,38,26.4,
22.6,18.8,6.1,44.4,20.1,21.8,18.5,12.6,18.1,40.3,
34.1,35.5,45.6,42.1,17.7,2.9,3.5,36.5,36.4,27.1,
43.4,5.6,33.9,39.1,0.8,31.4,14.9,26.6,45.5,30.1,
19.5,47.5,47.9,44.8,0.6,35.6,5.1,38.8,48.5,24.6,
32.3,50,48.4,6.8,18,29.1,37.4,24.8,44.5,19.6,
9,10.6,46.3,34.5,21.6,20.3,28,10,10.8,16.4,
45.4,25,34.6,31.8,1.1,40,18.2,32.6,11,7.5,
17.1,39.8,40.4,40.8,45.1,32,5.5,45.7,9.4,22.9,
43.2,7.3,23.7,8.6,4.1,24.2,18.9,14.2,31.6,7.0 };
float firsthalf[50], secondhalf[50]; //two arrays to calculate quartiles
float median, upperQuartile, lowerQuartile, max, min;
//sort array in ascending order from smallest to biggest value
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (sensorReading[i] > sensorReading[j])
{
a = sensorReading[i];
sensorReading[i] = sensorReading[j];
sensorReading[j] = a;
} //end if
} //end j loop
} //end i loop
//display array after sorting
printf("The numbers are sorted in ascending order ");
/*for (i = 0; i < n; ++i)
printf("%d - %.1f ", i, sensorReading[i]);
printf("number of readings: %d ", count); // to check that number of readings is 100 */
//When the number of elements in even, the median is calculated by adding the two middle elements and dividing the sum by 2
median = (sensorReading[n / 2] + sensorReading[n / 2 + 1]) / 2.0; //calculate median
//median = a[n / 2 + 1];
printf("Median = %.1f ", median); // display median
max = sensorReading[n-1]; // max value is the last number of sorted array
min = sensorReading[0]; // min value is the first number of sorted array
printf("Max = %.1f ", max); //display max
printf("min = %.1f ", min); //display min
//CALCULATING QUARTILES
//check this link to understand the calculation:
//en.wikipedia.org/wiki/Quartile
//split main array into FIRST half. size of array = 50
for (i = 0; i < n / 2; ++i)
firsthalf[i] = sensorReading[i];
/*printf("First half "); //pirnt First half - for checking
for (i = 0; i < n/2; ++i)
printf("%d - %.1f ", i, firsthalf[i]);*/
lowerQuartile = (firsthalf[25] + firsthalf[26]) / 2.0; //calculate lower quartile
printf("lower Quartile: %.1f ", lowerQuartile); //display lower quartile
//split main array into second half. size of array = 50
for (i = n / 2 ; i < n; ++i)
{
secondhalf[k] = sensorReading[i];
k++;
}
/*printf("Second half "); //pirnt Second half - for checking
for (k = 0 ; k < n/2; ++k)
printf("%d - %.1f ", k, secondhalf[k]);*/
upperQuartile = (secondhalf[25] + secondhalf[26]) / 2.0; //calculate upper quartile
printf("Upper Quartile: %.1f ", upperQuartile); //print upper quartile
while (uinput != 100)
{
printf(" ");
printf("--MENU-- ");
printf("Enter 1 if you are looking for one value ");
printf("Enter 2 if you are looking for 10 values ");
printf("Enter 100 to EXIT program ");
scanf("%d", &uinput);
if (uinput == 1)
{
printf("Enter the value: ");
scanf("%f", &value);
printf("The value entered is %.2f ", value);
res = isActualReading(sensorReading, value);
if (res == true)
{
res_wlq = withinLowerQuart(lowerQuartile, min, value);
res_wuq = withinUpperQuart(upperQuartile, max, value);
}
if (res != true)
{
res_iwr = isWithinReadings(value, max, min);
if (res_iwr == true) {
res_nn = nearestNumber(sensorReading, value);
printf("Nearest lower Number is: %f ", res_nn);
res_wlq = withinLowerQuart(lowerQuartile, min, value);
res_wuq = withinUpperQuart(upperQuartile, max, value);
}
}
//uinput = 1;
//printf("input is %d ", uinput);
}// end if equals 1
if (uinput == 2)
{
for (i = 0; i < 10; ++i)
scanf_s("%f", &number[i]);
for (i = 0; i < 10; ++i)
{
printf("Search results for %.2f ", number[i]);
res = isActualReading(sensorReading, number[i]);
if (res == true)
{
res_wlq = withinLowerQuart(lowerQuartile, min, number[i]);
res_wuq = withinUpperQuart(upperQuartile, max, number[i]);
}
if (res != true)
{
res_iwr = isWithinReadings(number[i], max, min);
if (res_iwr == true) {
res_nn = nearestNumber(sensorReading, number[i]);
printf("Nearest lower Number is: %.2f ", res_nn);
res_wlq = withinLowerQuart(lowerQuartile, min, number[i]);
res_wuq = withinUpperQuart(upperQuartile, max, number[i]);
}
}
printf("--------------------------------------- ");
printf(" ");
}//end loop
}//end if equals 2
if ((uinput != 1) && (uinput != 2) && (uinput != 100)) {
printf("input is %d ", uinput);
printf("ERROR: The value entered is not 1, 2 or 100 ");
}
//system("pause"); //make console screen pause to see results
}
}
//finding out the nearest least value from the readings arrays to the provided value
float nearestNumber(float *sensorReading, float value)
{
int i;
for (i = 0; i < 100; i++) {
if (sensorReading[i] > value)
return sensorReading[i-1];
}
}
//indicates whether the provided value exists in the reading array
bool isActualReading(float *sensorReading, float value)
{
int i;
for (i = 0; i < 100; i++) {
if (sensorReading[i] == value)
{
printf("Match Found! The number is one of the sensor readings ");
return true;
}//end if
} //end for
printf("NOT Found! The number is NOT a sensor readings ");
return false;
}
//indicates whether the provided value is within the range of the readings
//max and min represent the range of the readings
bool isWithinReadings(float value, float max, float min)
{
if (value > max) {
printf("Value is not in the range. It is above range of data ");
return false;
}
if (value < min) {
printf("Value is not in the range. It is BELOW range of data ");
return false;
}
else
{
printf("Value is in the range. ");
return true;
}
}
//checking whether the provided value occurs within the range of min and lowerQuartile
//quartile is one fourth of the whole quantity that is being valued.
// in this case quartile is one fourth of all the sensor readings.
bool withinLowerQuart(float lowerQuartile, float min, float value)
{
if (value >= min && value <= lowerQuartile)
{
printf("Value is within Lower Quartile ");
return true;
}
else {
printf("Value is NOT within Lower Quartile ");
return false;
}
}
//This function indicates whether the provided value occurs between the upperQuartile and max value
bool withinUpperQuart(float upperQuartile, float max, float value)
{
if (value <= max && value >= upperQuartile)
{
printf("Value is within Upper Quartile ");
return true;
}
else {
printf("Value is NOT within Upper Quartile ");
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.