Question

In: Computer Science

Section 6.9 of your textbook ("Debugging") lists three possibilities to consider if a function is not...

Section 6.9 of your textbook ("Debugging") lists three possibilities to consider if a function is not working.

Breaking a large program into smaller functions creates natural check points for debugging. If a function is not working, there are three possibilities to consider:
• There is something wrong with the arguments the function is getting; a precondition is violated.
• There is something wrong with the function; a postcondition is violated.
• There is something wrong with the return value or the way it is being used.

1. Describe each possibility in your own words.

2. Define "precondition" and "postcondition" as part of your description.

3. Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it.

Solutions

Expert Solution

Solution:-

Precondition is something when a function works when the precondition is true. But when the condition is failed, no guarantee is at all. Due to Precondition violation and if precondition is not defined properly bug gets introduce into the code.

Example:

def centigrade(a):

return 5*(a-32)/9

Now if you provide

centigrade(32)

You will get an error and that is precondition violated.

Postcondition violation:

The post condition statement indicates what shall be true when the function finishes the work. A programmer should ensure precondition is valid and postcondition remain true at program's end. It depends when precondition is not valid.

def centigrade (a):

res = 5*(a-32)/9

return res

if res > 0:

print("condition passed")

else:

exit

Violation in the return value: This is another programming failure when the value of a function is wrongly returned. This could lead to failure.

def centigrade (a):

res = a + 10

return result

Here the variable name is returned wrongly and thus leads to program failure.

Thanking you.


Related Solutions

1. Consider the loop from Section 8.3 of your textbook. prefixes = 'JKLMNOPQ' suffix = 'ack'...
1. Consider the loop from Section 8.3 of your textbook. prefixes = 'JKLMNOPQ' suffix = 'ack' for letter in prefixes: print(letter + suffix) Put this code into a Python script and run it. Notice that it prints the names "Oack" and "Qack". Modify the program so that it prints "Ouack" and "Quack" but leaves the other names the same. Include the modified Python code and the output in your submission. 2. Give at least three examples that show different features...
Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all...
Revise the substitutionEncrypt function from section 3.5 (p. 102) of the textbook to (1) remove all spaces from the plaintext message before it is encrypted and (2) generate the substitution cipher key from a password. (substitutionEncrypt will call genKeyFromPass to do this.) The password should be a parameter, psw, which will replace key as a parameter in the function header. Write function substitutionDecrypt, which will have two parameters, cipherText, a string, a message encrypted by substitutionEncrypt, and psw, the password...
After reading about the three main financial statements in your textbook, consider how reading these statements...
After reading about the three main financial statements in your textbook, consider how reading these statements could help you in your job. No matter what your position in the company is, you should be able to find some relevance between the information you can glean from the statements and information that would be useful to you in your work. Give at least three examples and explain how you can use the information to improve your effectiveness and/or efficiency in your...
Figure 8-16 on page 254 in your textbook lists traditional, evolving, and emerging technologies used to...
Figure 8-16 on page 254 in your textbook lists traditional, evolving, and emerging technologies used to capture and enter data into a system. List 10 actual examples with descriptions of innovative methods to capture data. To get you started look at law enforcement, agriculture, GPS, hand held devices, manufacturing, RFID, barcoding among other numerous fields. You can also think about devices/methods that are actually used, are unexpected, or cutting edge. In your description make sure you describe how this method...
Figure 8-17 on page 261 in your textbook lists traditional, evolving, and emerging technologies used to...
Figure 8-17 on page 261 in your textbook lists traditional, evolving, and emerging technologies used to capture and enter data into a system. List 10 actual examples with descriptions of innovative methods to capture data. To get you started look at law enforcement, agriculture, GPS, hand held devices, manufacturing, RFID, barcoding among other numerous fields. You can also think about devices/methods that are actually used, are unexpected, or cutting edge. In your description make sure you describe how this method...
Figure 8-16 on page 254 in your textbook lists traditional, evolving, and emerging technologies used to...
Figure 8-16 on page 254 in your textbook lists traditional, evolving, and emerging technologies used to capture and enter data into a system. List 10 actual examples with descriptions of innovative methods to capture data. To get you started look at law enforcement, agriculture, GPS, hand held devices, manufacturing, RFID, barcoding among other numerous fields. You can also think about devices/methods that are actually used, are unexpected, or cutting edge. In your description make sure you describe how this method...
Your Smith textbook lists several methods of removing lesions. Select one method of removal and locate...
Your Smith textbook lists several methods of removing lesions. Select one method of removal and locate a more specific description of that type of removal. (For example, Removal of skin tags. Skin tags can be removed by cutting, cryosurgery, and electrosurgery etc. You would then go into the details of these different methods.). Make sure to include your sources. Respond to two classmates with a scenario for the skin lesion that they selected and the applicable CPT codes. This should...
From information in the “Data section” of your textbook, calculate the standard Gibbs free energy and...
From information in the “Data section” of your textbook, calculate the standard Gibbs free energy and the equilibrium constant at (a) 25°C and (b) 50°C for the reaction CH4(g) + 3 Cl2(g) <--> CHCl3(l) + 3 HCl (g). Assume that the reactionenthalpy isindependent of temperature. ΔG°f (CHCl3, l) =-­71.8 kJ/mol, ΔH°f(CHCl3, l) =‐132.2 kJ/mol Which extra DATA can i provide from the textbook. What values should I look for?
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
In C programming Generalize the to binary() function of Listing 9.8 (from your textbook) to a...
In C programming Generalize the to binary() function of Listing 9.8 (from your textbook) to a to base n(number, base) function that takes a second argument in the range 2–10. It then should convert (and print) the number that is its first argument to the number base given by the second argument. For example, to base n(129,8) would display 201, the base-8 equivalent of 129. Test the function in a complete program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT