Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the error in the following generic code? Note: when run this program doe

ID: 3819007 • Letter: W

Question

 What is the error in the following generic code? Note: when run this program does work and execute properly. Therefore a logic error is present.    #Declare some variables   Declare Test1 as float   Declare Test2 as float   Declare Average as integer    #Get the 2 numbers   write "Enter in your first test score: "   input Test1   write "Enter in your second test score: "   input Test2    #Calculate the Average   Average = (Test1 + Test2)/2    #Display the result   write "Your Average is: " + Average 
    
The Average variable was declared as the wrong data type. A local variable is misplaced. The variable Test1 was misspelled. The variable Test2 is of the wrong datatype The Average variable was declared as the wrong data type.

Explanation / Answer

Test1 and Test2 both are floats. But Average is declared an integer. On some compilers it may lead to error, but for sure in all compilers if executed, will loose the precision of the result.

For example if Test1 is stored a value 4.5, and Test2 is stored a value 5.5, then the average will be: (4.5 + 5.5) / 2 = 10 / 2 = 5.0, and therefore, only the integral part i.e., 5 will be stored in Average. This seems fine with this example.

Now, coming to another example.

Test is stored a value 0.5, and Test2 is stored a value 0.4, then the average will be: (0.5 + 0.4) / 2 = 0.9 / 2 = 0.45, but when stored in an integer variable Average, the floating point will be truncated, and therefore, will store 0 into the variable.

So, the answer to what is wrong is:

The Average variable was declared as the wrong data type.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote