Top Banner
QUIZ Lesson 4 Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise y is unchanged.
45

QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Sep 20, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ Lesson 4

Exercise 4:

Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise y is unchanged.

Page 2: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ Lesson 4

Exercise 4:

Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Exercise 5: Now do the same using a conditional operator

… ? … : …

Page 3: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ Lesson 4

What is the difference between relational operators and logical operators?

Give 3 examples of each.

Page 4: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ Lesson 4

Page 5: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ Lesson 4

• Is -42 true or false?

• What is a more economical way to write if (a != 0) ?

Page 6: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Lesson 5

Functions

Page 7: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise
Page 8: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Anatomy of a function

Function body

Function headerLocal variable

(recognized only inside the function)

Page 9: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ: Write a function that returns the absolute value of its float argument

Hint: use an if statement or a conditional operator

Page 10: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

A function may have multiple

return statements

Page 11: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

… but any call to a function always returns only one value!

//the little function who tried

unsigned multiple(unsigned){

return 42;

return 43;

return 44;

}

When the first return is encountered during program execution, the function exits, and the value from that return is returned.

Page 12: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

… actually a void function does not return any value!

void no_return (int x){

printf("x is %d\n", x);

}

Do not confuse a value printedby a function with a value returned by a function!

Page 13: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Parameters vs. argumentsSometimes

called formal parameters

Sometimes called actual parameters

Page 14: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise
Page 15: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise
Page 16: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Conclusion: local variables within a function are distinct from any global variables having the same name, and from any other local variables with the same name within other functions.

Page 17: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

Write the declaration (prototype) for a function ssquares that returns the sum of the squares of its two double arguments.

Then write the definition.

Page 18: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

Give an example of how the previous function would be used in the main program.

Page 19: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

To do in notebook for next time:

• Read and take notes pp.92-108

• Answer end-of-chapter quizzes 1 – 6

• Answer end-of-chapter exercise 1

Page 20: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

Write a function prod with two double arguments, that returns their product, as a float.

Page 21: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

Write a function ssquares with two integer arguments, that returns the sum of the squares of the arguments.

Show how the function is called in the main program.

Page 22: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

What is the difference between formal and actual parameters (arguments)?

Explain using the example of the previous function ssquares.

Page 23: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

What does this function return?

Page 24: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZTrue or false? Any function should:

a) return one value.

b) return at least one value.

c) return at most one value.

d) have at least one return statement

e) have at most one return statement

Page 25: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Why functions?

Page 26: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Two ways to write our functions

If the function definition is placed before main, we don’t

need a protoype!

Page 27: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Recursive functions

Page 28: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

How a recursive function works

1On the way “down”, five instances of the function are left “pending”, because their return value is not yet known.

Page 29: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Because the results increase very fast, the most appropriate data type for factorial is unsigned long long

2.4 quintillion = 2.4∙1018

Page 30: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Of course, the range of any data type will be exceeded sooner or later …

14.1 quintillion = 14.1∙1018

Correct value is 51.0∙1018

EOL2

Page 31: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ: Explain why we have two ways to write our C functions

Page 32: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

Define a recursive function.

Page 33: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

QUIZ

What are the two parts of any recursive function?

Page 34: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Sum of numbers from 1 to n

0

Page 35: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Sum of numbers from 1 to n

Page 36: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

More practice with recursive functions

Draw the sequence of recursive calls, as we did in prev. examples. What is printed?What would be a better name for foo?

Page 37: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

More practice with recursive functions

What is printed?

Page 38: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

More practice with recursive functions

What is printed?

4

Page 39: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

More practice with recursive functions

What is printed?

Page 40: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

More practice with recursive functions

What is printed?

Page 41: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Inline functions

int fun(int a, int b) {

return (a-b) + (a * b);

}

inline int fun(int a, int b) {

return (a-b) + (a * b);

}

Generally, the compiler literally copies the code of the inline function into the body of

any function that calls it (e.g. main)

Page 42: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Inline functions

int fun(int a, int b) {

return (a-b) + (a * b);

}

inline int fun(int a, int b) {

return (a-b) + (a * b);

}

Trade-off between speed and memory!

Page 43: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

MS Visual Studio 2012 only allows the keyword inlinein C++ programs. In C, we have to use _inline instead

Page 44: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

To do in notebook for next time:

• Read and take notes pp.109-115

• Answer end-of-chapter quizzes 7 – 10

• Answer end-of-chapter exercises 3, 4, 5

Page 45: QUIZ Lesson 4 - Tarleton State University...QUIZ Lesson 4 Exercise 4: Write an ifstatement that assigns the value of x to the variable y if x is in between 1 and 20 (inclusive), otherwise

Homework for Chapter 5, due Wed, Sep.28:• Exercises 7, 8, 11

• Hint for 11: Multiplication is repeated addition!

• Not from text: Write a function that takes as arguments the radius and height of a cone and returns its volume. Test function with r = 2.5 and h = 3.5.

Every time a function is required, you have to also write the main function, and call your function at least once!

Capture a screenshot of both code and output.

Give instructor one printout with the entire homework.