210 likes | 595 Views
Quadratic Equation Program. Remember the quadratic equation program that from several weeks ago. it takes the coefficients A, B, and C of a quadratic equation Ax 2 +Bx +C = 0 and computes the real roots using the quadratic equation
E N D
Quadratic Equation Program • Remember the quadratic equation program that from several weeks ago. • it takes the coefficients A, B, and C of a quadratic equation Ax2 +Bx +C = 0 and computes the real roots using the quadratic equation • We want to convert this program into a function that can easily be used in any program that needs to compute the roots of a quadratic equation. • the function computes and returns the roots given the coefficients A, B, and C • Problem: there may be zero, one, or two roots and a function can return at most one value using the return mechanism. B2 – 4AC x = -B ± 2A Computer Science I - Martin Hardwick
Returning Multiple Values From a Function • There are two ways to return multiple values from a function: • global variables • reference parameters • The preferred approach is to use reference parameters. • Global Variables – special variables that are available for all functions to use. • hence, one function can put a value into a global variable and another function can retrieve that value from the global variable • Reference Parameters – special type of parameter that lets a called function change the value of the corresponding argument variable in the calling function. Computer Science I - Martin Hardwick
#include <iostream> using namespace std; // global variables int a,b; void func1 () // called first { a = 10; // global a b = 2; // global b . . . } void func2() // called last { int b,c; c = a; // global a cout << c; b = 1; // local b . . . } A global variable is a variable declared at the beginning of a program outside all functions. remember, variables declared inside a function are local to that function All functions can use a global. unless a function has a local variable or parameter with the same name as a global variable in this case, the global variable is hidden from the function Global variables make programs hard to read and understand. therefore, don’t use them Global Variables Computer Science I - Martin Hardwick
Parameter Passing In Functions • In all the programs we have seen so far, the arguments in a function call provide data needed by the called function • they pass data into the function • There are times when a function wants to use its parameters to pass data back to the calling program. • examples are when functions must return more than one value or must modify the arguments passed to them • We need two types of parameter passing to handle these two situations. int main ( ) { . . . f(a, b, c); . . . } int main ( ) { . . . f(a, b, c); . . . } by value void f(&x, &y, &z) { . . . } void f(x, y, z) { . . . } by reference Computer Science I - Martin Hardwick
Passing Parameters By Value • Pass parameters by value when you want to pass a value into the function only. • the argument in the function call is evaluated and its value used to initialize the corresponding parameter in the function • if the function changes the value of the parameter, this change is not reflected in the corresponding argument void main ( ) { . . . f(a, b+1, c*a); . . . } void f(x, y, z) { . . . } . . . y = 2; . . . return a x 10 10 y b -2 -1 2 c 5 z 50 Computer Science I - Martin Hardwick
Passing Parameters By Reference • Pass parameters by reference when you want the function to be able to change the value of a parameter and have this change reflected in the corresponding argument in the function call. • the argument must be a variable and the function gets direct access to this variable void main ( ) { . . . f(a, b, c); . . . } void f(&x, &y, &z) { . . . } . . . x = 2; . . . return a x 10 2 y b -2 c 5 z Computer Science I - Martin Hardwick
#include <iostream> #include <cmath> Goal: create a function named quadSolve that solves for the real roots of a quadratic equation using the quadratic formula. The function will return the number of real roots (i.e., zero, one, or two) using the return mechanism. The function will have two reference parameters named x1 and x2 that it will use to pass the roots back to the calling function. The function will have three value parameters that will be used to pass the coefficients of the quadratic equation to the function. We will need the sqrt function from the <cmath> library. Solution Of A Quadratic Equation Computer Science I - Martin Hardwick
int quadSolve(double A, double B, double C, double &x1, double &x2) //PURPOSE: compute the roots of a // quadratic equation //PRECONDITIONS: A, B, and C are // the coefficients of the equation //POSTCONDITIONS: returns the // number of roots, using x1 and x2 // to return the roots, as needed { double disc, sqrtOfDisc; // Compute the discriminant disc = B*B - 4*A*C; To use the quadratic formula, this function must have the three coefficients A, B and C. thus, three parameters are needed for them since the function will not change these parameters, they can be passed by value Two additional parameters are needed to pass back the two solutions. they must be passed by reference this is indicated by the ampersand in front of the parameter name Function quadSolve (1) Computer Science I - Martin Hardwick
// Compute solutions if (disc < 0) { // no solutions exist return 0; } else { // solutions exist sqrtOfDisc = sqrt(disc); x1 = (-B + sqrtOfDisc)/(2 * A); if (disc == 0) {// one solution return 1; } else {// two solutions x2 = (-B - sqrtOfDisc)/(2*A); return 2; } } } The number of real roots is determined by the value of the discriminator. The first root is assigned to reference parameter x1, if the root exists. the corresponding argument in the calling function receives this value The second root is assigned to reference parameter x2, if the root exists. the corresponding argument in the calling function receives this value Function quadSolve (2) Computer Science I - Martin Hardwick
int main () //PURPOSE: solve a quadratic equation //PRECONDITIONS: none //POSTCONDITIONS: rets 0 if success { double A, B, C, x1, x2; int numSols; // Get the quadratic equation cout << "Enter first coefficient: "; cin >> A; cout << "Enter second coefficient: "; cin >> B; cout << "Enter third coefficient: "; cin >> C; // Solve the quadratic equation numSols = quadSolve(A, B, C, x1, x2); The main function provides the basic control flow. Note the call to function quadSolve to compute the roots. the value it returns is assigned to variable numSols this value is used later to control formatting the display of the roots variables x1 and x2 have not been given values when passed to function quadSolve the function puts values into them Function main (1) Computer Science I - Martin Hardwick
// Display the solution(s) switch (numSols) { case 0: cout << "No real solutions." << endl; break; case 1: cout << "One real solution: " << x1 << endl; break; case 2: cout << "Two real solutions: " << x1 << " and " << x2 << endl; } return 0; } The value that function quadSolve returns is used in the switch statement to identify how to display the solutions. Function quadSolve can be used in any program that needs to solve quadratic equations. it is easier and less error prone to copy entire functions from one program to another than to cut and paste code segments Function Main (2) Computer Science I - Martin Hardwick
Running The Program Quadratic Equation: x2 - 5x + 6 = 0 Computer Science I - Martin Hardwick
#include <iostream> using namespace std; // Global Variables int a; int b; int func1(int c, int &d) { int a; a = 6; c = b + d + a; d = c; return b; } int func2(int c, int &f) { c = f * a; f = c * a; return c; } int main() { int g; a = 2; b = 5; g = func1(a, b); cout << a << " " << b << " " << g << endl; a = func2(b, g); cout << a << " " << b << " " << g << endl; a = func1(a, a); cout << a << " " << b << " " << g << endl; return 0; } What Is the Output Of This Program? Computer Science I - Martin Hardwick
Tracing The Program Execution • To figure out what the output of the program is, draw a picture similar to the one below, and then use it to hand execute the program. • each pink box represents a variable (local, global or parameter) Global Variables a b func1 func2 main c c g d f a Computer Science I - Martin Hardwick
Program Output • If you can reproduce this output by hand, then you understand global variables and parameter passing. • If you can’t, please ask questions, or try running the problem using your compiler. Computer Science I - Martin Hardwick
Pass by Reference and Arrays • Because of legacy concerns about time and space, arrays are always pass by reference. // a is pass by reference void mod_array (int a[], int limit, int incr) { for (int i = 0; i < limit; i++) { a[i] = a[i] + incr; } } Computer Science I - Martin Hardwick
Shadowing • You can define a variable with the same name every time you start a new code block using the “{“ character. • The new variable will shadow (hide) the old one until your code leaves that block. • The code below will output “10” followed by “0” void function () { int counter = 0; { int counter = 10; cout << counter; } cout << counter; } Computer Science I - Martin Hardwick