Ask Computer Engineering Expert

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 =vSN-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).

Compile & Test

Compile your program using this gcc command. c99 is a shortcut for running gcc -std=c99, which uses the C99 standard instead of the default C89 standard. -lm includes the math libraries.

$ c99 lab3.c -lm -o program3

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M93074857

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As