Question

##4. What will the following program display? ##def main(): ## x = 1 ## y =...

##4. What will the following program display?

##def main():

## x = 1

## y = 3.4

## print(x, y) ## first printing

## change_us(x, y)

## print(x, y) ##second printing

##

##def change_us(a, b):

## a = 0

## b = 0

## print(a, b)

##

##main()

##

##Yes, yes, main() displays

##1 3.4

##0 0

##1 3.4

## The question is: why x and y are still the same while the second printing of

(x,y)?

## It seems that both x and y went through change_us() subprogram and change_us()

## makes zeros from its arguments?

##Explain, please.

##5. Look at the following function definition:

##def my_function(a, b, c):

## return a + (b - c)

##a. Write a statement that calls this function and uses keyword arguments

##to pass 2 into b and 3 into c.

##b. What will be printed when the function call my_function(3.5) executes?

##6. Write a statement that generates a random number in the range of 1 through 100

and

##assigns it to a variable named rand.

##Solution:

##import random

##rand = random.randint(1, 100)

##6.1 further exension.

## a) Write a function RANDO(n) which returns a raw of n(independent)

##random numbers from 1 to 100. Use random.seed(10).

##Let n is always to be integer and > 1 (don't check that)

## hint:

##def random_raw(n):

## import random

## random.seed(10)

## for m in .......:

## print(random.randint(1, 100), ....)

## b) Call your function for n=3

##7. The following statement calls a function named half, which returns a value

##that is half that of the argument. (Assume the number variable references a float

value.)

##Write code for the function.

##result = half(number)

##Solution:

##def half(value):

## return value / 2.0

##7.1 Further extension. Write a program value_part() which returns the

##pth part (as float) of the value (value and p are integer).

##Let get value and p as input. Format the result as .2f.

##Hint:

##def value_part():

## value = int(input('put the value: '))

## p = ...(...('put the p: '))

## return ...(.../..., '.2f')

Homework Answers

Answer #1

4

x and y are still the same while the second printing of (x,Y) because both x and y are declared inside the main method and

we are printing x and y inside the main only .and when we call change_us method then the value of a=x=1 and b=y=3.4

but in change_us method we modify a and b that a=0 b=0 so print(a,b) after change of a and b will print 0 0

here no modification is done regarding to the variable x and y so it will remain same as previous value .

so the output

1 3.4

0 0

1 3.4

5.

x=my_function(1,2,3)

here value of a=1,b=2 and c=3 the return value is stored in x

b) compilation error

6

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? import random #function definition #check for even and return 0 if even def isEven(number): if(number%2==0): return 0 #return 1 if odd else: return 1 #count variables even =0 odd = 0 c = 0 #loop iterates for 100 times for i in range(100): #generate random number n = random.randint(0,1000) #function call val = isEven(n) #check value in val and increment if(val==0):...
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If X^2+ y^2 <= 1, then M=M+1 End the loop Print M/N (This is the probability) Print M/N (this is approximately = Py
Which of the following strings P is a Python program. (b) P = “def f(x): x...
Which of the following strings P is a Python program. (b) P = “def f(x): x = 'am I a Python program?'” (d) P = “def f(x,y,z): return y” (e) P = “def f(x): return y” For each of the following Python programs P and input strings I, give the output P(I), (f) P = “def f(x): return str(len(x+x+'x'))”, I = “GAGAT” (g) P = “def f(x): return str(len(x))”, I=P (h) P = “def f(x): return str(1/int(x))”, I = “0”
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
1. Let f (x, y) = xy((x^2-y^2)/(x^2+y^2)) if, (x, y) 6= (0, 0), 0, if (x,...
1. Let f (x, y) = xy((x^2-y^2)/(x^2+y^2)) if, (x, y) 6= (0, 0), 0, if (x, y) = (0, 0) (it's written as a piecewise function) (a) Compute ∂f/∂y (0, 0) and ∂f/∂x (0, 0). (b) Compute ∂f/∂y (x, 0) for all x, and ∂f/∂x (0, y) for all y (c) Use part (a) and (b) to compute ∂^2f/∂y∂x (0, 0) and ∂^2f/ ∂x∂y (0, 0), then verify that: ∂^2f/∂y∂x (0, 0) does not equal ∂^f/ ∂x∂y (0, 0)
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT