I am a beginner in program coding and could really use some help with the Debugg
ID: 3905577 • Letter: I
Question
I am a beginner in program coding and could really use some help with the Debugging Exercise: Thank you!!
1. Part of the following pseudocode is incompatible with the Java, Python, C, and C++ languages. Identify the problem. How would you fix the problem if you were to translate this pseudocode into one of the aforementioned languages?
Module checkEquality(Integer num1, Integer num2)
If num1 = num2 Then
Display "The values are equal."
Else
Display "The values are NOT equal."
End If
End Module
2. The intended purpose of the following module is to set the temp parameter to the value 32.0 if it is not already equal to 32.0. This will not work as the programmer expects, however. Find the problem.
Module resetTemperature(Real Ref temp)
If NOT temp == 32.0 Then
Set temp = 32.0
End If
End Module
3. The intended purpose of the following module is to determine whether the value parameter is within a specified range. The module will not work, however. Find the problem.
Explanation / Answer
1. In the following statement:
'If num1 = num2 Then' , we use num1=num2 to check for equality of num1 and num2. However = is the assignment operator and will assign the value of num2 to num1. For checking equality, we should use ==.
Thus the modified pseudocode is as follows:
Module checkEquality(Integer num1, Integer num2)
If num1 == num2 Then
Display "The values are equal."
Else
Display "The values are NOT equal."
End If
End Module
2. Here since we are making changes to the value of temp which is a parameter passed to the function, we need to use pass by reference i.e. pass a reference of temp to the function. Here we do that by passing Real Ref temp. Now while updating the value of temp, we need to update the memory location pointer by temp as temp is just a reference to the actual memory location. So we need to write: 'set *temp=32.0' instead of 'set temp=32.0'
Thus the modified pseudocode is as follows:
Module resetTemperature(Real Ref temp)
If NOT temp == 32.0 Then
Set *temp = 32.0
End If
End Module
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.