How do i do the following with this program.... //You can use any array of unsor
ID: 3640676 • Letter: H
Question
How do i do the following with this program....
//You can use any array of unsorted values
In this lab assignment, instead of
of calling "double_it" to double the values (grades) in the array, call
"Arrayutil.sort" to sort those values. (Output the sorted values to the file.)
Additionally, on the screen, display the following values:
- low grade
- high grade
- the "spread" (that is, the difference between high and low)
- median grade
import java.util.*;
//////////////////////////////////////////////////////////////
//
// This module contains a method that carries out the
// bubble sort on an array "a" of integers, where "n"
// is the number of data items (integers) currently in
// the array.
//
// Assumption: n > 1
//
//
//
//////////////////////////////////////////////////////////////
public class Arrayutil
{
public static void sort(int[] a, int n)
boolean exchange_made;
int num_items_to_visit;
int last_marker;
int i;
int temp;
// if the number of items is 0 or 1, then there's
// nothing to do; otherwise, do the sorting
if(n > 1)
{
// set the number of values (elmts) to visit on the first pass.
// Note that it is actually one less than the total #
// of data values since the comparison of the next-to-last
// and last values occurs while visiting the next-to-last
num_items_to_visit = n - 1;
do
{
// no exchange has occurred yet on the current pass.
// (We haven't scanned any values yet!)
// Indicate this by lowering the "exchange_made" flag
exchange_made = false;
// the index of the last item to visit on this pass is
// one less than the # to visit
last_marker = num_items_to_visit - 1;
// scan the values
for(i = 0; i <= last_marker; i++)
if(a[i] > a[i+1])
{
//swap them
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
// indicate that a swap has been made
exchange_made = true;
}
// number of items to visit is now one less
num_items_to_visit--;
} while( exchange_made && (num_items_to_visit > 0) );
}
} // end sort
} // end Arrayutil
Explanation / Answer
package string; import java.io.BufferedWriter; import java.io.FileWriter; import java.util.*; ////////////////////////////////////////////////////////////// // //This module contains a method that carries out the //bubble sort on an array "a" of integers, where "n" //is the number of data items (integers) currently in //the array. // //Assumption: n > 1 // // // ////////////////////////////////////////////////////////////// public class Arrayutil { public static void sort(int[] a, int n) { boolean exchange_made; int num_items_to_visit; int last_marker; int i; int temp; // if the number of items is 0 or 1, then there's // nothing to do; otherwise, do the sorting if (n > 1) { // set the number of values (elmts) to visit on the first pass. // Note that it is actually one less than the total # // of data values since the comparison of the next-to-last // and last values occurs while visiting the next-to-last num_items_to_visit = n - 1; do { // no exchange has occurred yet on the current pass. // (We haven't scanned any values yet!) // Indicate this by lowering the "exchange_made" flag exchange_made = false; // the index of the last item to visit on this pass is // one less than the # to visit last_marker = num_items_to_visit - 1; // scan the values for (i = 0; i a[i + 1]) { // swap them temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; // indicate that a swap has been made exchange_made = true; } // number of items to visit is now one less num_items_to_visit--; } while (exchange_made && (num_items_to_visit > 0)); } } // end sort /** * Writes the sorted array into the grade.txt file * @param a * @param outputFileName */ public static void writeData(int[] a,String outputFileName) { try { // Create file FileWriter fstream = new FileWriter(outputFileName); BufferedWriter out = new BufferedWriter(fstream); int k = 0; while(kRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.