Lab 2 – Simple Functions

Assignment 1. A). Write a Complete C++ Program that will receive from the user coefficients a, b and c of a quadratic equation. Assume that given coefficients will ...
85KB taille 0 téléchargements 37 vues
Lab 3 – Functions Topics to be covered: o Basic function structure and syntax o Function calls Assignment 1 A) Write a Complete C++ Program that will receive from the user coefficients a, b and c of a quadratic equation. Assume that given coefficients will produce two real solutions for the quadratic equation. Your program should contain 3 functions: main, Calculate_First_Root and Calculate_Second_Root. The main function should: • prompt for and read the 3 coefficients • pass the coefficients to a function Calculate_First_Root (parameter list) which calculates and returns the value of x1 to the main • pass the coefficients to a function Calculate_Second_Root (parameter list) which calculates and returns the value of x2 to the main • display coefficients and both solutions to the screen functionType Calculate_First_Root (parameter list) - parameter list: a, b, c - return value: x1 - return type: floating point functionType Calculate_Second_Root (parameter list) - parameter list: a, b, c - return value: x2 - return type: floating point Optional for part A: Think of creating an additional function that is called Calculate_ Discriminant. This function would receive coefficients a, b and c, calculate the discriminant and return it to the calling function. Both Calculate_First_Root and Calculate_Second_Root functions would call it as a first step in their calculations. B) Write in 4-5 sentences an Explanation (must be typed) of how you would change your approach if the assumption that the given coefficients will produce two real solutions was no longer valid. Describe how your pseudocode would change if you had to take into account all of the possible cases.

Assignment 2 For the following problem provide a Defining Diagram, Pseudocode and a Complete C++ Program:

The probability density of the Normal distribution with mean μ and standard deviation σ

is an example of a Gaussian function (also known as a bell curve). A common example of this distribution is grades in class.

You require a program that will receive the mean and the standard deviation from the user. Your program should then request 4 points (values for x) from the user and display in a tabular format the x and the corresponding f(x) values. (Hint: points that are symmetric relative to the mean value should produce the same f(x) since the curve is symmetric around the mean value. You can use this information to test your program.) All calculations must be done in a separate function. All results should be displayed in main. Think of how you can write one function and reuse it. Read Chapter 3, page 113-124 to learn how to format the output.