# Sorting an array of doubles in descending order not working

• (3 Pages)
• 1
• 2
• 3

## 30 Replies - 3341 Views - Last Post: 03 November 2012 - 01:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=298238&amp;s=8a552bd5a89936ac1e213e764e8aa41b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

# Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:30 PM

Ok, you all will be proud of me! I remembered all that I learned last week and managed to get it to total the array values, I even got it to sort them, however I need them sorted in the opposite direction largest to smallest because the next step I must take is to take the 6 largest scores and average them. Im not exactly sure how that will work but I'll get there. First things first .... I need to know how to make the array do what I want when I'm using doubles. I will post code and errors!
```import java.util.*;
import javax.swing.*;
import java.io.*;
{
public static void main(String args[])throws Exception

{
// Declare input file to be opened.

String studentName;
String scoreString;
int counter;
double totalScore;
final int MAX_SCORE = 9;

double scoresArray[] = new double[MAX_SCORE];
scoresArray = new double[9];

while ((studentName = br.readLine()) != null)//Beginning of outer loop.
{
double score = scoresArray[0];
totalScore = 0;
System.out.println (studentName);
for (counter = 0; counter<MAX_SCORE; counter++)
{
scoresArray[counter] = Double.parseDouble(scoreString);
totalScore = scoresArray[counter] + totalScore ;   // Accumulate a total of all scores.
}
int loopIndex;
double scores;
boolean flag = true;
int temp;

while (flag)
{
flag = false;
for (scores = 1; scores < MAX_SCORE-1 ;scores++)
{
if (scoresArray[scores]<scoresArray[scores+1])
{
temp = scoresArray[loopIndex];
scoresArray[loopIndex] = scoresArray[indexLoop-1];
scoresArray[loopIndex+1] = temp;
flag = true;
}
}
System.out.print ("Scores: "+ scoresArray[loopIndex]);

System.out.printf("Total Score: %.2f" , totalScore);
}
}
}
}

```

ERRORS:

StudentGrades.java:43: error: possible loss of precision
if (scoresArray[scores]<scoresArray[scores+1])
^
required: int
found: double
StudentGrades.java:43: error: possible loss of precision
if (scoresArray[scores]<scoresArray[scores+1])
^
required: int
found: double
StudentGrades.java:45: error: possible loss of precision
temp = scoresArray[loopIndex];
^
required: int
found: double
StudentGrades.java:46: error: cannot find symbol
scoresArray[loopIndex] = scoresArray[indexLoop-1];
^
symbol: variable indexLoop
4 errors
I had originally had the "scores" as loopIndex and it didn't like that either! So can someone tell me what's up? I see that it thinks it wants an integer, because I'm assuming that the brackets are the count... but when I used int loopIndex it wasn't happy either!

Is This A Good Question/Topic? 0

## Replies To: Sorting an array of doubles in descending order not working

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:42 PM

I think you meant to set up your for loop using an index other than the variable scores. Perhaps that's what you meant for the variable loopIndex.

You misspelled loopIndex at line 46.

Any why not make the variable temp a double rather than in int since you're assigning a double value to it?

You'll likely find additional errors, but fixing those above should get you a little further down the road.

This post has been edited by GregBrannon: 01 November 2012 - 02:42 PM

### #3 N1G3L2

• New D.I.C Head

Reputation: 4
• Posts: 28
• Joined: 25-October 11

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:45 PM

On line 24 you create double score = scoresArray[0]; but it never seems to be used so you should be able to get rid of that.
On line 35, loopIndex needs to be initialized, just set it to 0 there. Also on line it looks like you accidentally typed indexLoop instead.
On line 38, temp should be a double and oline 43, you might need to cast to int or change some other variables to doubles depending on what you want to do.

But fixing those errors should at least get it compiled =)

This post has been edited by N1G3L2: 01 November 2012 - 02:45 PM

### #4 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:50 PM

Oh yeah, and a for loop that iterates an array usually looks like:

for ( index = 0 ; index < array.length ; index++ )

array.length returns the length of the array as an array property (not a method). Notice that the index stops at less than the array length, because the max index of an array is one less than the total number of items (length) of the array.

### #5 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:54 PM

Yes, I realized that I did that... instead of changing them all back to loopIndex before I posted. Done and done... but still in essence the same 4 errors.

### #6 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:55 PM

Post your updated code.

### #7 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:59 PM

It is the name of the array.length correct?

I will. As soon as I make sure of one other thing. The loopIndex must be an integer correct because it's what is keeping count! Then it compiles and yes I have other problems....which I think involve rearranging things... I'm glad you are on today...

### #8 RozenKristal

• D.I.C Regular

Reputation: 45
• Posts: 381
• Joined: 29-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 02:59 PM

array_name.length == numbers of slots in the array

### #9 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 03:00 PM

```import java.util.*;
import javax.swing.*;
import java.io.*;
{
public static void main(String args[])throws Exception

{
// Declare input file to be opened.

String studentName;
String scoreString;
int counter;
double totalScore;
final int MAX_SCORE = 9;

double scoresArray[] = new double[MAX_SCORE];
scoresArray = new double[9];

while ((studentName = br.readLine()) != null)//Beginning of outer loop.
{

totalScore = 0;
System.out.println (studentName);
for (counter = 0; counter<MAX_SCORE; counter++)
{
scoresArray[counter] = Double.parseDouble(scoreString);
totalScore = scoresArray[counter] + totalScore ;   // Accumulate a total of all scores.
}
int loopIndex;
boolean flag = true;
double temp;

while (flag)
{
flag = false;
for (loopIndex = 1; loopIndex < scoresArray.length-1 ;loopIndex++)
{
if (scoresArray[loopIndex]<scoresArray[loopIndex+1])
{
temp = scoresArray[loopIndex];
scoresArray[loopIndex] = scoresArray[loopIndex-1];
scoresArray[loopIndex+1] = temp;
flag = true;
}
}
System.out.print ("Scores: "+ scoresArray[loopIndex]);

System.out.printf("Total Score: %.2f" , totalScore);
}
}
}
}

```

This post has been edited by ValeriePurvis: 01 November 2012 - 03:04 PM

### #10 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 03:03 PM

So it looks like you've cleared the major syntax and compiler errors. There may still be undiscovered run time or logic errors. What are you seeing?

### #11 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 03:06 PM

This is the output.

James Frank
Scores: 70.0Total Score: 715.00Scores: 70.0Total Score: 715.00Scores: 70.0Total Score: 715.00
----jGRASP wedge2: exit code for process is 0.

It keeps printing the 1 score and the total. but not even 9 times. which is odd... I've got something inside a brace it shouldn't be!

### #12 RozenKristal

• D.I.C Regular

Reputation: 45
• Posts: 381
• Joined: 29-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 03:06 PM

Isnt loopIndex suppose to start at 0? Since array index start from 0 instead of 1?

### #13 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 03:12 PM

Actually it would, except in this program it is actually that the numbers or scores start in position 1 of the array. position 0 is the name which is already taken care of in the outer while loop! So I need to start at 1, and there are 9 total scores. Am I still thinking correctly?

### #14 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 01 November 2012 - 09:12 PM

Ok, I made progress. I can now output the scores in ascending order and it calcs the proper total. This is all great! Now my dilemma is, and I think I know the answer but I am not sure how to approach it exactly yet....

1. I have the scores, I need to take the top 6 scores and put them where I can add them, and average them. Then I need to use my Parallel Array and give output as to what the letter grade would be. Creating a Report whose output is the Name, the percent grade and the letter grade.

2. I am thinking that I need 2 or 3 methods. Method 1 (): Reads the file and sorts the array; Method 2 (): Pulls the top 6 scores, adds them, averages them (total/MAX_POINTS) max points will be set to equal 600. Then Passes the Information to the 3. Grade Reports () Method. I need to gather and output the Name, The Grade Percent and the Letter Grade. The letter Grade I will get from comparing my Average with the Parallel array.

3. I don't know how to pull just the top 6 scores! And I'm not entirely sure about the set and get instructions which I believe I need to use to pass information between one method and another.

Would anyone like to guide me through this one? I am learning, and I'm feeling pretty good about being able to begin this one without a lot of brain cramp already! I have read all my chapters, I have looked at many threads and snippets of code to piece together a little of what I think I need to know. Now I just need help sewing it together.

My Code so far is such:
```import java.util.*;
import javax.swing.*;
import java.io.*;
{
public static void main(String args[])throws Exception

{
// Declare input file to be opened.

String studentName;
String scoreString;
int counter;
double totalScore;
final int MAX_SCORE = 9;

double scoresArray[] = new double[MAX_SCORE];

while ((studentName = br.readLine()) != null)//Beginning of outer loop.
{

totalScore = 0;
System.out.println (studentName);
for (counter = 0; counter<MAX_SCORE; counter++)
{
scoresArray[counter] = Double.parseDouble(scoreString);
totalScore = scoresArray[counter] + totalScore ;   // Accumulate a total of all scores.
}
int loopIndex = 0;
Arrays.sort(scoresArray);
int scoresIndex = 0;

for (loopIndex = 0; loopIndex < scoresArray.length ;loopIndex++)
{
System.out.print ("Scores: "+ scoresArray[loopIndex]);
}
System.out.printf("Total Score: %.2f" , totalScore);
}

}
}

```

My Current Output, which is not necessary other than so I can see what it's actually doing is:

James Frank
Scores: 70.0Scores: 72.0Scores: 75.0Scores: 79.0Scores: 80.0Scores: 80.0Scores: 82.0Scores: 87.0Scores: 90.0Total Score: 715.00

This post has been edited by ValeriePurvis: 02 November 2012 - 05:59 PM

### #15 ValeriePurvis

Reputation: 5
• Posts: 78
• Joined: 21-September 12

## Re: Sorting an array of doubles in descending order not working

Posted 02 November 2012 - 07:48 PM

Made more progress towards the ultimate goal... Will post code tomorrow. I think Im going to need a little help with passing the arguments to the new method...