1 / 17

Finding the Minimum Value

First, recall the code for finding a sum of the values read in: int g, sum = 0; for (int i = 0; i < n; i++) { cin >> g; sum += g; }. Finding the Minimum Value.

jael
Download Presentation

Finding the Minimum Value

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. First, recall the code for finding a sum of the values read in: int g, sum = 0; for (int i = 0; i < n; i++) { cin >> g; sum += g; } Finding the Minimum Value

  2. Finding the minimum value is similar. Instead of a variable to store the current sum, we need a variable to store the current minimum value: int g, m = ?; for (int i = 0; i < n; i++) { cin >> g; if(g < m) m = g; } Finding the Minimum Value (cont'd)

  3. The only question left is: What should m be initialized to? A common mistake is to set m to be 0, just like the sum is. But suppose we are reading in grades between 0 and 100: Input: 100 70 80 30 90 m: 0 0 0 0 0 0 What should m be initialized to?

  4. Instead of initializing m to 0, it should be set to the maximum value that could be read in (if known). In this example, m would be set to 100: Input: 100 70 80 30 90 m: 100 100 70 70 30 30 Correct Code

  5. If the maximum value isn't known, and m is an int, we could use the constant MAXINT, which is the largest value that an int variable can have. An alternative is to read the first value before the loop, and use that to start off m: int g, m; cin >> g; m = g; for (int i = 1; i < n; i++) { cin >> g; if (g < m) m = g; } An Alternative

  6. Instead of using the if-statement, we could use the min function from the previous class: for(int i=0; i < n; i++) { cin >> g; m = min(m,g); } This sets the new value of m to be the minimum of the old value of m and g. Other Code

  7. A function can be declared without being defined at the same time. That is, we can say what the name of the function is, give its arguments and types, and the return type, without specifying the actual code for the function. This is called a function declaration or function prototype. int min(int a, int b); Note the semicolon at the end, rather than a body. Function Declaration

  8. The declaration of a function must occur in the program before the function is called. But the definition of the function can occur later in the code. Even if you have a separate declaration, you must still define the function somewhere. The function definition should have the same header as the function declaration, but include code. Function Definition

  9. int min(int a, int b); // function declaration int main(void) { // main program uses min int x = 5, y = 7; cout << min(x,y) << endl; } int min(int a, int b) { // function definition return (a < b ? a : b); } Example

  10. Parameters, sometimes called formal parameters, are the variables that occur in function declaration. Arguments, sometimes called actual arguments, are the values that used in the call to the function. Parameters must be variables, because they are names of data objects. Arguments may be expressions (for now). Parameters vs. Arguments

  11. Each argument is evaluated. For each parameter, a new data object is created and is label by the parameter. The value of the corresponding argument is the initial value of the data object. The body of the function is executed, and the return value (if any) is saved. When the function is done, the data objects for the parameters are destroyed. When you call a function

  12. Arguments correspond to the parameters by their order. That is, the first argument corresponds to the first parameter argument to the second parameter, and so forth. Don't mix them up. The type of the argument should match that of the parameter (or be able to be automatically converted to that type, i.e., int -> double). Pitfalls

  13. Functions may call other functions (but remember, a function must be declared before it can be used). Functions may have zero arguments. Functions may have the return type void. In this case, they are not used as part of an expression, but the function call is followed by a semicolon, to make it a statement. main is a function. More on Functions

  14. Function foo may call function goo, and function goo may call function foo as well. A function may call itself. This is called recursion and is tricky. If your function calls itself (at this point), it's probably a mistake and you didn't mean to do it. Functions calling Functions

  15. Functions may have local variables. You may declare a variable inside of a function. The data object created exists only while the function is running and disappears when the function is done. The label of the data object (that variable) may be the same name as one in the main program (or the calling function), but stands for a different data object. This can be confusing for novice programmers. Local Variables

  16. int min(int a, int b); int main(void) { int x = 5; y = 7, c; c = min(x,y); cout << c << endl; } int min(int a, int b) { int c = (a < b ? a : b); return c; } Example

  17. Write a function, getMin, with one int parameter, n, that reads in n grades (between 0 and 100), and returns the lowest grade read in. The function should call the min function. The return value should be of type int. Exercise

More Related