Question

The following incorrect code is supposed to find the integral average of the positive numbers and...

The following incorrect code is supposed to find the integral average of the positive numbers and the integral average of the negative numbers in the array a with nElems elements.

public void avgPosAvgNeg (int [ ] a, int nElems)

{

     int j;

     int sumPos = 0; int sumNeg = 0; int nPos = 0; int nNeg = 0;

     for (j = 0; j < nElems; j++)

     {

          if (a[j] > 0)

          {

               sumPos = sumPos + a[j];

               nPos++;

          }

          else if (a[j] < 0)

          {

               sumNeg = sumNeg - a[j];

               nNeg++;

          }

     }

          System.out.println(" Average of positive numbers: “ + sumPos/nPos);

          System.out.println(" Average of negative numbers: “ + sumNeg/nNeg);

}

Go through the four phases in the debugging process. [a] In the stabilization phase, list the minimal test cases that produce the error. [b] In the localization phase, identify the section of the code that causes the error. [c] In the correction phase, show the correct code that fixed the error. [d] In the verification phase, what test cases would you use to make sure the error is fixed, and no other errors were introduced?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

[a]. Stabilization phase

Here after completing the entire code , when it is executed,

  1. The output for positive integers is giving correct values, but in case of fractional calculation the decimal parts were omitted from output. So the desired output was not gained for calculating the average of positive integers for all cases.
  2. For the average of negative integers, firstly, the negative sign before the magnitude is not shown, so the result is treated as wrong and it also omit the decimal part in case of fractional average.

                Here we took no of elements=6

                Elements are:

                5 -8 -9 6 2 -2

                Result is shown as: Average of positive numbers:   4 ( decimal part missing)

                                                    Average of negative numbers: 6 (decimal and negative sign before digit are missing)

The following error is in the output

In this way , for no of elements=8

                Elements are:

                4 -2 -3 -5 6 3 2 -1

                Result is shown as: Average of positive numbers: 3 ( decimal part missing)

                                                    Average of negative numbers: 2 (decimal part and negative sign before digit are missing)

[b].localization phase

public void avgPosAvgNeg (int [ ] a, int nElems)

{

     int j;

     int sumPos = 0; int sumNeg = 0; int nPos = 0; int nNeg = 0; // Error1

     for (j = 0; j < nElems; j++)

     {

          if (a[j] > 0)

          {

               sumPos = sumPos + a[j];

               nPos++;

          }

          else if (a[j] < 0)

          {

               sumNeg = sumNeg - a[j];      // Error2

               nNeg++;

          }

     }

          System.out.println(" Average of positive numbers: " + sumPos/nPos);      // Error3

          System.out.println(" Average of negative numbers: " + sumNeg/nNeg); //Error4

}

Error1:   in place of   int sumPos = 0; int sumNeg = 0;

                It will be double sumPos = 0; double sumNeg = 0;

                ( double will help to calculate decimal value to generate final average)

Error2:     in place of sumNeg = sumNeg - a[j];    

                  It will be    sumNeg = sumNeg + a[j];   

                  ( because in first case as a[j] is –ve so sumNeg will generate positive value always. Because minus of minus always gives positive value. So the code will be positive as per second case)

Error3 and Error4:                     

                  Average of positive numbers: " + sumPos/nPos

                  Average of negative numbers: " + sumNeg/nNeg

Will be

                  Average of positive numbers: " + Double.valueOf(sumPos/nPos)

                  Average of negative numbers: " + Double.valueOf(sumNeg/nNeg)

(Conversion in double the average will give perfect result, if it has decimal part)

[c]. Correction phase

import java.util.Scanner;

class avg

{

public void avgPosAvgNeg (int [ ] a, int nElems)

{              

     int j;

    double sumPos = 0; double sumNeg = 0; int nPos = 0; int nNeg = 0;

     for (j = 0; j < nElems; j++)

     {

          if (a[j] > 0)

          {

               sumPos = sumPos + a[j];

               nPos++;

          }

          else if (a[j] < 0)

          {

               sumNeg = sumNeg + a[j];

               nNeg++;

          }

     }

          System.out.println(" Average of positive numbers: " + Double.valueOf(sumPos/nPos));

          System.out.println(" Average of negative numbers: " + Double.valueOf(sumNeg/nNeg));

}

  }

class inte

{

   public static void main(String args[])

   {

       int n,i;

       int a[]=new int[100];

       avg av = new avg();

       Scanner ob = new Scanner(System.in);

       System.out.println("how many elements");

       n = Integer.parseInt(ob.nextLine());

       System.out.println("enter elements:");

       for(i=0;i<n;i++)

       {

          a[i]=Integer.parseInt(ob.nextLine());

       }

         

       av.avgPosAvgNeg ( a, n);

   }

}

[d]. Verification phase

  1. Here for first case we took

No of elements=6

5 -8 -9 6 2 -2

Output gives: for positive numbers, the average is : 4.333333333333333

                           for negative numbers , the average is : -6.333333333333333

  1. Here for second case we took

No of elements=8

4 -2 -3 -5 6 3 2 -1

Output gives: for positive numbers, the average is : 3.75

                           for negative numbers , the average is : -2.75

Outputs for both the cases are absolutely right with respect to sign convention and appropriate result.

Add a comment
Know the answer?
Add Answer to:
The following incorrect code is supposed to find the integral average of the positive numbers and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT