COSC 1336 - Ch. 5 MPLab

Ace your homework & exams now with Quizwiz!

Other cards to look at...

Chapter 5b --> https://quizlet.com/102343526/chapter-5b-flash-cards/ Python ch 5 --> https://quizlet.com/165811975/python-ch-5-flash-cards/ CMPT 140CodeLab 2 --> https://quizlet.com/161914978/cmpt-140-codelab-2-flash-cards/

5.7..#51147 Assume that to_the_power_of is a function that expects two int parameters and returns the value of the first parameter raised to the power of the second parameter. Write a statement that calls to_the_power_of to compute the value of cube_side raised to the power of 3 and that associates this value with cube_volume. Instructor Notes: Write a statement that calls the function to_the_power_of, passing the parameters cube_side and 3, assigning the result to a variable cube_volume. KJ

cube_volume = to_the_power_of(cube_side,3)

5.8..#51140 Write the definition of a function add which receives two parameters containing integer values and returns their sum.

def add(int1,int2): >>> result=int1+int2 >>> return result

5.8..#51156 Write the definition of a function add, that receives two int parameters and returns their sum.

def add(int1,int2): >>> return int1+int2 **Remember to tab (>>>) or space

5.8.. #51152 Write the definition of a function half which receives a variable containing an integer as a parameter and returns another variable containing an integer, whose value is closest to half that of the parameter. (Use integer division!) Instructor Notes: Write a definition for a function half which receives one parameter and returns half the parameter's value. Use regular division and convert the result to an int before returning it. KJ

def half(value): >>> return int(value/2)

5.8.. #51164 Define a function called isEven that takes a parameter containing an integer value and returns True if the parameter is even, and False otherwise.

def isEven(int): >>> if int%2==0: >>> >>> return True >>> else: >>> >>> return False

5.8.. #51157 Define a function called isPositive that takes a parameter containing an integer value and returns True if the parameter is positive or False if the parameter is negative or 0.

def isPositive(int): >>> if int>0: >>> >>> return True >>> else: >>> >>> return False

5.8.. #51159 Define a function called isSenior that takes a parameter containing an integer value and returns True if the parameter is greater than or equal to 65, and False otherwise.

def isSenior(int): >>> if int>=65: >>> >>> return True >>> else: >>> >>> return False

5.8.. #51174 Define a function called min that takes two parameters containing integer values and returns the smaller integer of the two. If they have equal value, return either one.

def min(int1,int2): >>> if int1<int2: >>> >>> return int1 >>> elif int2<int1: >>> >>> return int2 >>> else: >>> >>> return int1 or int2

5.8..#51151 Write the definition of a function oneLess which receives a parameter containing an integer value and returns an integer whose value is one less than the value of the parameter.

def oneLess(int): >>> return int-1

5.8..#51142 Write the definition of a function oneMore which recieves a parameter containing an integer value and returns an integer that is one more than the value of the parameter. Instructor Notes: Write the definition of a function oneMore which receives one parameter (name of your choice) and returns a number that is one more than the value of the parameter. KJ

def oneMore(int): >>> return int+1

5.8.. #51141 [Functions >> functions and if statements] Write the definition of a function powerTo which receives two parameters, a double and an integer. If the second parameter is positive, the function returns the value of the first parameter raised to the power of the second. Otherwise, the function returns 0. Instructor Notes: Write the definition of a function powerTo which receives two parameters (names of your choice). If the second parameter is >= 0, return the value of the first parameter raised to the power of the second parameter. Otherwise, return 0.

def powerTo(adouble,aint): >>> if aint>=0: >>> >>> return adouble**aint >>> else: >>> >>> return 0

5.2 .#51136 Write a definition of the function printDottedLine, which has no parameters and doesn't return anything. The function prints to standard output a single line consisting of 5 periods (".").

def printDottedLine(): >>> print(".....") **Remember to tab (>>>) or space

5.8..#51154 Define a function called signOf that takes a parameter containing an integer value and returns a 1 if the parameter is positive, 0 if the parameter is 0, and -1 if the parameter is negative.

def signOf(int): >>> if int>0: >>> >>> return 1 >>> elif int==0: >>> >>> return 0 >>> else: >>> >>> return -1

5.8..#51139 Write the definition of a function square which receives a parameter containing an integer value and returns the square of the value of the parameter. Instructor Notes: Write the definition of a function named square which receives one parameter and returns the value of the square of the parameter. You may call the parameter anything you like.

def square(int): >>> return int**2 **Remember to tab (>>>) or space

5.8..#51155 Write the definition of a function twice, that receives an int parameter and returns an int that is twice the value of the parameter. Instructor Notes: Write the definition for a function named twice. The function should receive a parameter and return the value that is twice the value of the parameter. You may call the parameter anything you like. KJ

def twice(int): >>> return 2*int **Remember to tab (>>>) or space

5.8..#51016 Write the definition of a function typing_speed, that receives two parameters. The first is the number of words that a person has typed (an int greater than or equal to zero) in a particular time interval. The second is the length of the time interval in seconds (an int greater than zero). The function returns the typing speed of that person in words per minute (a float). Instructor Notes: Write the definition of a function named typing_speed that receives two parameters, a variable that is the number of words typed and a second variable that is the length of time in seconds that typing occurred (variable names of your choice). The function should return the typing speed in words per minute. KJ

def typing_speed(num_words,time_interval): >>> num_words>=0 >>> time_interval>0 >>> result=float(60*num_words/time_interval) >>> return result **Remember to tab (>>>) or space

5.7..#51146 Given that add, a function that expects two int parameters and returns their sum, and given that two variables, euro_sales and asia_sales, have already been defined: Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.

eurasia_sales =add(euro_sales,asia_sales)

5.5..#51144 Assume that print_error_description is a function that expects one int parameter and returns no value. Write a statement that invokes the function print_error_description, passing it the value 14. print_error_description(14)

print_error_description(14)

5.5..#51145 Given print_larger, a function that expects two parameters and returns no value and given two variables, sales1 and sales2, that have already been defined, write a statement that calls print_larger, passing it sales1 and sales2.

print_larger(sales1, sales2);

5.2.#51143 Assume that print_todays_date is a function that uses no parameters and returns no value. Write a statement that calls (invokes) this function.

print_todays_date()

5.5..#51074 Write the code to call a function whose name is send_number. There is one argument for this function, which is an int. Send 5 as an argument to the function.

send_number(5)

5.2.#51073 Write the code to call the function named send_signal. There are no parameters for this function.

send_signal()

5.5..#51076 Write the code to call a function named send_two and that expects two parameters: a float and an int. Invoke this function with 15.955 and 133 as arguments.

send_two(15.955,133)

5.5..#51075 Write the code to call a function named send_variable and that expects a single int parameter. Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable.

send_variable(x);


Related study sets

Chapter 5- what is real estate ?

View Set

General Science Module #15 Study Guide

View Set

Email Etiquette/ Writing Emails.

View Set

R U A REAL FAN OF TYLER THE CREATOR?

View Set

Chapter exam six health insurance policy provisions

View Set

Case 8: Tinker v. Des Moines (1969)

View Set