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

Can anyone explain how you got the answer without just running the program? I wa

ID: 3869355 • Letter: C

Question

Can anyone explain how you got the answer without just running the program? I want to know how you get the answe step by step because I don't really understand the concept of swapping. Please either write on the paper or just write in chegg as specific as you can, Thank you

public class Q4
{
public static void main( String [] args )
{
    int j, k, m, tmp;
    int [] a = new int [4];

    for ( m = 0 ; m < a.length ; ++m )
      a[m] = a.length - m;

    for ( m = 0 ; m < a.length ; ++m )
       for ( j = a.length - 1 ; m < j ; --j )
   
         if ( a[j-1] > a[j] )
         {
           tmp    = a[j-1];
           a[j-1] = a[j];
           a[j]   = tmp;

           for( k = 0 ; k < a.length ; ++k )
             if ( k == m || k == j )
               System.out.print(a[k]);
             else
               System.out.print("*");
          System.out.println();
        }
}
}

Explanation / Answer

explaination:

for ( m = 0 ; m < a.length ; ++m )
      a[m] = a.length - m; // a[0] = 4, a[1] = 3, a[2] = 2, a[3] = 1

array = 4 3 2 1

so output:

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