Overview
In this lab, you will write a program that will find several traits of a collection of NN numbers. Get the value of NN before scanning each value in the collection of NN numbers. You can assume NN will be an integer, but you must check that it is greater than or equal to 0 (0≤N0≤N). If the value of NN provided by the user is outside of the expected range, the program should print an error and prompt the user again for NN. It should continue to do this until the user enters a number that is valid.
Once you have the value of NN, your program must loop that many times, calling a get_input() function to get the next value in the collection from the user. With these values, you are to calculate and display the following information:
Smallest Number
Largest Number
Average
Range of values (e.g. how many between smallest and largest?)
standard deviation =√SN−a2=SN-a2, where SS is the sum of squares of the values in the collection, and aa is the average.
Analysis
To write this program (or any program) it helps to first identify what the inputs and outputs of the program should be. It also helps to identify any supporting data required by the formulas, for example, values required by the formula but not explicitly output to the user.
In this case, the program is asking for several number as input, and produces as output several traits of that particular collection of numbers.
Inputs
N=N= An integer value, this is how many numbers are in the number collection. Should be greater than or equal to zero.
List of numbers = NN different numbers, input one after the other. These should be real numbers (data type double or float).
Supporting Data
sum = The sum of all of the numbers in the list so far
sum_squares = The sum of the squares of all the numbers in the list so far
Outputs
lowest = The lowest number in the list
highest = The highest number in the list
average = The average of all the numbers
range = The difference between the highest and lowest number. Should be positive or 0.
standard deviation = The standard deviation, as calculated by the above formula.
Algorithm Design
Pseudocode is a way to write an algorithm in a language agnostic sort of way. It is up to you, the programmer, to translate these generic instructions into C statements. Remember to look at the examples on the class website if you get stuck on exactly what syntax to use.
function get_n():
set N = -1
loop: do {
Prompt user for a value of N
if N < 0:
print error message
else
break // ends loop
end if
} while ( N < 0 )
return N
end function
function main():
set N = get_N();
set lowest = DBL_MAX, highest = -DBL_MAX;
loop from 0 -> N:
current = get_input()
if current < lowest:
set lowest = current
if current > highest:
set highest = current
set sum = sum + current
set sum_squares = sum_squares + current^2
end loop
set range = highest - lowest
if n == 0:
set average = 0, std_dev = 0;
else:
set average = sum / n
set std_dev = square_root( ( sum_squares / n ) - average^2 )
end if
print out all results
end function
Implementation Hints
This should be added with your other include statements:
#include
Then, during your loop, save the current input value into highest if it is greater than the current value of highest, and save it into lowest if the value is less than the current value in lowest.
You may want to re-use your get_input() function from Lab 2. You'll call it repeatedly from inside a loop, to get all of the values in the collection.
double get_input(void)
{
/* your code here */
}
You should write a second function, get_n(), that takes in an integer instead of a real number. You will count to this number using a loop, and you should only ever use integers for that purpose. This function should use a do...while loop to prompt the user to input a value that is greater than or equal to 0, and continue prompting the user until they enter such a value.
Use a for loop to count from 0 to NN.
Calculate the values of sum and sum_squares inside the for loop, as running totals.
Keep track of the lowest and highest values as you see them during the for loop as well.
After the for loop is complete, you have enough information to calculate the rest of the required data.
In order to use the sqrt and pow functions, you will need to include the math standard library. This should be added with your other include statement:
#include
In addition, when you compile your program, you should include the -lm flag, so that the math library will be included in the build.
To find the highest and lowest number, set the variable highest to the lowest possible value for a double, and the variable lowest to the highest possible double value. If you include the standard library , you can use the pre-defined constant DBL_MAX for this purpose.
double highest = -DBL_MAX, lowest = DBL_MAX;
You will need to check for the case when NN is zero, and handle that gracefully (e.g. no divide by zero errors, just return 0 instead).
Attachments
No attachments uploaded
Tags
Clarifications
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'cramshark'; // required: replace example with your forum shortname
var disqus_identifier = '/production/1661';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function () {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
Please enable JavaScript to view the comments powered by Disqus.