Note that there are some explanatory texts on larger screens.

plurals
  1. POBubble sort. Do I need double or int?
    primarykey
    data
    text
    <p>OK So I am making a bubble sorting program to make sure arrays are in order and I have the first class compiled fine now but the second one keeps giving me problems. Right now it says it is the wrong type so I changed the variable dif to a int instead of a double but then it says possible loss of precision. Here is the code for the first file.</p> <pre><code>public class BubbleSort { public static void sort(int[] a, int numberUsed) { int index; for ( int i =0; i&lt;numberUsed; i++) { for (index = 0; index&lt;a.length - 1; index++) { if (a[index]&gt; a[index + 1]) { interchange(index, index + 1, a); } //end of if () } //end of for () } //end of for () } private static void interchange(int i, int j, int[] a) { int temp1; temp1 = a[i]; a[i] = a[j]; a[j] = temp1; } } </code></pre> <p>and this is the second file that is giving me the problem</p> <pre><code>import java.util.Scanner; public class GolfScores { public static double[] diff = new double[5]; public static void printOutArray(double[] a) { System.out.println("Sorted array values:"); for (int i=0; i&lt;a.length; i++) { System.out.printf("%2.2f",a[i]); } //end of for loop System.out.println(); double Handicap= (diff[0])/1*0.96; System.out.printf("Handicap: %2.2f",Handicap); System.out.println(); } public static void main(String[]args) { //construct and declare three arrays Scanner keyboard = new Scanner(System.in); double[] rating = new double[5]; double[] slope = new double[5]; double[] score = new double[5]; int numberUsed = 0; int index = 0; //Print out directions for the user System.out.println("Calculate handicap for 5 games of golf. This program takes Scores, \nCourse Rating and Slope rating for 5 games. \n\nUsing those figures, the program calculates the differential\nfor each round entered using this formula:(Score - Course Rating) x113 / Slope Rating. \n\nThen uses the lowest differential to calculate handicap"); System.out.println(); //A do while loop that runs until index is great than 5 do { System.out.print("Enter golf scores for game " +(index+1)+ ": "); score[index]= keyboard.nextDouble(); System.out.print("Course rating for game "+(index+1)+ ": "); rating[index] = keyboard.nextDouble(); System.out.print("Course slope for game "+(index+1)+": "); slope[index] = keyboard.nextDouble(); index++; //index is the number of array indexed variables used so far } while((index&lt;5)); //this formula for all 5 arrays (Score- Course Rating) x 113 / Slope Rating diff[0]=((score[0]- rating[0]) * 113 / slope [0]); diff[1]=((score[1]-rating[1])*113/slope[1]); diff[2]=((score[2]-rating[2])*113/slope[2]); diff[3]=((score[3]-rating[3])*113/slope[3]); diff[4]=((score[4]-rating[4])*113/slope[4]); BubbleSort.sort(diff, diff.length);//passes value of diff array to BubbleSort.sort printOutArray(diff);//prints out diff array } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload