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

The code below sorts an array in numerical descending order. Insert a section of

ID: 3744056 • Letter: T

Question

The code below sorts an array in numerical descending order. Insert a section of code that prints out the array in ascending order below the original.

ZZZZZZZZZZZZZZZZZZZZZZZZZZ

#include <stdio.h>
int main (void){

int set[10] = {12, 5, 7, 6, 2, 18, 5, 2, 9, 3};

int i;
int j;
int swap;

// Bubble Sort, order output //

for (i = 0;i < 9; i++){
for (j =0;j < 9;j++){
  
if (set[j] < set [j+1]){
swap= set[j];
set [j] = set[j+1];
set [j+1] = swap;
  
}
}
}

int k;
for (k = 0; k < 9; k++){
printf("%d, ",set[k] );
}
return 0;

}

Explanation / Answer

include <stdio.h>
int main (void){

   int set[10] = {12, 5, 7, 6, 2, 18, 5, 2, 9, 3};

   int i;
   int j;
   int swap;

   // Bubble Sort, order output //

   for (i = 0;i < 9; i++){
       for (j =0;j < 9;j++){
      
           if (set[j] < set [j+1]){
               swap= set[j];
               set [j] = set[j+1];
               set [j+1] = swap;
           }
       }
   }

   int k;
   for (k = 0; k < 9; k++){
       printf("%d, ",set[k] );
   }

   // Bubble Sort, Ascending order output //  

   for (i = 0;i < 9; i++){
       for (j =0;j < 9;j++){
      
           if (set[j] > set [j+1]){
               swap= set[j];
               set [j] = set[j+1];
               set [j+1] = swap;
           }
       }
   }

   int h;
   for (h = 0; h < 9; h++){
       printf("%d, ",set[h] );
   }

   return 0;
}

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