8 Replies - 779 Views - Last Post: 12 November 2009 - 09:19 PM Rate Topic: -----

#1 shaylah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 09

sort troubles

Post icon  Posted 12 November 2009 - 11:51 AM

I need help with my selection sort method. This is due tonight and I am really stuck. Okay here is my code. It is sorting it, but its doing it least to greatest and it needs to go greatest to least. Thanks in advance.

//Shaylah Mutschler, CS 240, Ondrasek, CJ, project 6, November 12, 2009

package mutschler_project6;

/**
 *
 * @author Shaylah
 */
import  java.util.Scanner;
import java.io.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
        File inFile = new File("scores.txt");
        Scanner input = new Scanner(inFile);
        
        //declare and crreate arrays
        String[] nameArray = new String[17];
        double[] labArray = new double[17];
        double[] quizArray = new double[17];
        double[] projectArray = new double[17];
        double[] examArray = new double[17];
        double[] finalExamArray = new double[17];
        double[] courseAverageArray = new double[17];
        String[] courseGradeArray = new String[17];

        getData(input, nameArray, labArray, quizArray, projectArray, examArray, finalExamArray, courseAverageArray, courseGradeArray);

        
       
            
        for (int i = 0; i < 17; i++)
        {
            double lab = labArray[i] * .15;
            double quiz = quizArray[i] * .1;
            double project = projectArray[i] * .25;
            double exam = examArray[i] * .3;
            double finalExam = finalExamArray[i] * .2;
            courseAverageArray[i] = (lab + quiz + project + exam + finalExam);
            double courseAverage = courseAverageArray[i];
            
            //fill courseGradeArray
            if (courseAverage >= 90)
            {
                courseGradeArray[i] = "A";
            }
            else if (courseAverage < 90 && courseAverage >= 80)
            {
                courseGradeArray[i] = "B";
            }
            else if (courseAverage < 80 && courseAverage >= 70)
            {
                courseGradeArray[i] = "C";
            }
            else if(courseAverage < 70 && courseAverage >= 60)
            {
                courseGradeArray[i] = "D";
            }
            else if (courseAverage < 60)
            {
                courseGradeArray[i] = "F";
            }
        }

        //sort using selection sort
        sortArrays(nameArray, labArray, quizArray, projectArray, examArray, finalExamArray, courseAverageArray, courseGradeArray);

        System.out.println("Name                        Lab Avg         Quiz Avg    Project Avg       Exam Avg      Final Exam    Course Avg     Course Grade");
        System.out.println("");
        for (int i = 0; i < 17; i++)
        {
            System.out.printf("%-25s", nameArray[i]);
            System.out.printf("%10.1f", labArray[i]);
            System.out.printf("%15.1f", quizArray[i]);
            System.out.printf("%15.1f", projectArray[i]);
            System.out.printf("%15.1f", examArray[i]);
            System.out.printf("%15.1f", finalExamArray[i]);
            System.out.printf("%15.1f", courseAverageArray[i]);
            System.out.printf("%15.1s", courseGradeArray[i]);
            System.out.println();
        }

    }

    
     
  
     
    public static void getData(Scanner input, String[] nameArray, double[] labArray, double[] quizArray, double[] projectArray, double[] examArray, double[] finalExamArray, double[] courseAverageArray, String[] courseGradeArray)
    {
        //get data using for loop
        //calculate percent points and store in arrays
        for (int i = 0; input.hasNext(); i++)
        {
            String name = input.nextLine();
            double lab1 = input.nextDouble();
            double lab2 = input.nextDouble();
            double lab3 = input.nextDouble();
            double lab4 = input.nextDouble();
            double lab5 = input.nextDouble();
            double lab6 = input.nextDouble();
            double lab7 = input.nextDouble();
            double lab8 = input.nextDouble();
            double lab9 = input.nextDouble();
            double lab10 = input.nextDouble();
            double lab11 = input.nextDouble();
            double lab12 = input.nextDouble();
            double lab13 = input.nextDouble();
            double lab14 = input.nextDouble();
            double lab15 = input.nextDouble();
            input.nextLine();
            double quiz1 = input.nextDouble();
            double quiz2 = input.nextDouble();
            double quiz3 = input.nextDouble();
            double quiz4 = input.nextDouble();
            double quiz5 = input.nextDouble();
            double quiz6 = input.nextDouble();
            double quiz7 = input.nextDouble();
            double quiz8 = input.nextDouble();
            double quiz9 = input.nextDouble();
            double quiz10 = input.nextDouble();
            double quiz11 = input.nextDouble();
            double quiz12 = input.nextDouble();
            input.nextLine();
            double project1 = input.nextDouble();
            double project2 = input.nextDouble();
            double project3 = input.nextDouble();
            double project4 = input.nextDouble();
            double project5 = input.nextDouble();
            double project6 = input.nextDouble();
            input.nextLine();
            double exam1 = input.nextDouble();
            double exam2 = input.nextDouble();
            input.nextLine();
            double finalExam1 = input.nextDouble();
            input.nextLine();
            double labPercent = (lab1 + lab2 + lab3 + lab4 + lab5 + lab6 + lab7 + lab8 + lab9 + lab10 + lab11 + lab12 + lab13 + lab14 + lab15)/(1.5);
            double quizPercent = (quiz1 + quiz2 + quiz3 + quiz4 + quiz5 + quiz6 + quiz7 + quiz8 + quiz9 + quiz10 + quiz11 + quiz12)/(.60);
            double projectPercent = (project1 + project2 + project3 + project4 + project5 + project6)/(1.50);
            double examPercent = (exam1 + exam2)/(2.00);
            double finalExam2 = finalExam1/1.00;
            //store in arrays
            nameArray[i] = name;
            labArray[i] = labPercent;
            quizArray[i] = quizPercent;
            projectArray[i] = projectPercent;
            examArray[i] = examPercent;
            finalExamArray[i] = finalExam2;
         }
    }


    public static void sortArrays(String[] nameArray, double[] labArray, double[] quizArray, double[] projectArray, double[] examArray, double[] finalExamArray, double[] courseAverageArray, String[] courseGradeArray)
    {
        for (int i = courseAverageArray.length - 1; i >= 1; i--)
        {
            //find the maximum in array
            double currentMax = courseAverageArray[0];
            int currentMaxIndex = 0;

            for (int j = 1; j <= i; j++)
            {
                if (currentMax < courseAverageArray[j])
                {
                    currentMax = courseAverageArray[j];
                    currentMaxIndex = j;
                }
            }
            if (currentMaxIndex != i)
            {
                //move all parallel arrays
                courseAverageArray[currentMaxIndex] = courseAverageArray[i];
                courseAverageArray[i] = currentMax;
                String temp1 = nameArray[currentMaxIndex];
                nameArray[currentMaxIndex] = nameArray[i];
                nameArray[i] = temp1;
                double temp2 = labArray[currentMaxIndex];
                labArray[currentMaxIndex] =labArray[i];
                labArray[i] = temp2;
                double temp3 = quizArray[currentMaxIndex];
                quizArray[currentMaxIndex] = quizArray[i];
                quizArray[i] = temp3;
                double temp4 = projectArray[currentMaxIndex];
                projectArray[currentMaxIndex] = projectArray[i];
                projectArray[i] = temp4;
                double temp5 = examArray[currentMaxIndex];
                examArray[currentMaxIndex] = examArray[i];
                examArray[i] = temp5;
                double temp6 = finalExamArray[currentMaxIndex];
                finalExamArray[currentMaxIndex] = finalExamArray[i];
                finalExamArray[i] = temp6;
                String temp7 = courseGradeArray[currentMaxIndex];
                courseGradeArray[currentMaxIndex] = courseGradeArray[i];
                courseGradeArray[i] = temp7;
                
                
            }
        }
    }

}



((ps. NeoTifa is a hottie ;D jk I'm posting this for Larrys sister. She was too lazy to make her own account. For shame Shayrah!!! :/ ))

This post has been edited by shaylah: 12 November 2009 - 11:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: sort troubles

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: sort troubles

Posted 12 November 2009 - 12:13 PM

to flip over selection sort, just flip over the condition so that on each iteration you are finding the greatest element and swapping it with the element in the beginning of the array..i have a snippet for this but it sorts in ascending order, just change the condition that checks for the smallest element and make it check for the largest element by just flipping the sign around
Was This Post Helpful? 0
  • +
  • -

#3 shaylah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-November 09

Re: sort troubles

Posted 12 November 2009 - 12:24 PM

View Postmostyfriedman, on 12 Nov, 2009 - 11:13 AM, said:

to flip over selection sort, just flip over the condition so that on each iteration you are finding the greatest element and swapping it with the element in the beginning of the array..i have a snippet for this but it sorts in ascending order, just change the condition that checks for the smallest element and make it check for the largest element by just flipping the sign around


I am not exactly sure on how to go about making the largest go to the beginning instead of end of the array. I have tried several things and none of them have worked so far.
Was This Post Helpful? 0
  • +
  • -

#4 Dophert  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 60
  • Joined: 19-October 09

Re: sort troubles

Posted 12 November 2009 - 01:05 PM

		String[] nameArray = new String[17];
		double[] labArray = new double[17];
		double[] quizArray = new double[17];
		double[] projectArray = new double[17];
		double[] examArray = new double[17];
		double[] finalExamArray = new double[17];
		double[] courseAverageArray = new double[17];
		String[] courseGradeArray = new String[17];



This is an example of where you can best use OOP, for Java is an OOP language.

public class Student {

	public String name, courseGrade;
	public double lab, quiz, project, exam,
			finalExam, courseAverage;

}



Sorting this wouldn't be such a big deal, as you don't have to deal with huge amounts of arrays, just one class and checking on 1 local variable inside that class.

This post has been edited by Dophert: 12 November 2009 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: sort troubles

Posted 12 November 2009 - 04:13 PM

 public static void sortArrays(double[] courseAverageArray)
 {
	int largest; //variable to hold the index of the largest element
	int temp; //a temporary variable to use when swapping the elements
	for(int i = 0; i < courseAverageArray.length - 1; i++)
	{
		largest = i;
		for(int j = i+1; j < courseAverageArray.length; j++)//loop to find the largest element
		{
			if(courseAverageArray[j] > courseAverageArray[largest])
				largest = j;	
		}
		temp = courseAverageArray[i];
		courseAverageArray[i] = courseAverageArray[largest];
		courseAverageArray[largest] = temp;
		}
 }



alright this should sort the courseAverageArray in descending order.. all you need to do is swap the elements of the rest of the arrays..btw making these arrays as instance variables would be much much better, just to clean up the code a little
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2636
  • View blog
  • Posts: 15,661
  • Joined: 24-September 08

Re: sort troubles

Posted 12 November 2009 - 07:21 PM

Is that going from largest to smallest? It's supposed to be from A - F. I didn't have the time to figure it out for her, and I was hoping pbl would've been on and helped her by now... Maybe I should call her and tell her you posted an answer. And Dophert, we appreciate your enthusiasm, but you're not helping at all. That was just a generalization... -___-
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: sort troubles

Posted 12 November 2009 - 07:28 PM

View PostNeoTifa, on 12 Nov, 2009 - 06:21 PM, said:

Is that going from largest to smallest? It's supposed to be from A - F. I didn't have the time to figure it out for her, and I was hoping pbl would've been on and helped her by now... Maybe I should call her and tell her you posted an answer.


she said that she wanted it to sort from highest to lowest and my code does that.. oh and I'm like way better than pbl :P jk
Was This Post Helpful? 0
  • +
  • -

#8 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2636
  • View blog
  • Posts: 15,661
  • Joined: 24-September 08

Re: sort troubles

Posted 12 November 2009 - 09:01 PM

She didn't say that, I did ;D And thanks. It means a lot. ((thats Tish daughter that you said you were gonna kidnap btw :P))
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: sort troubles

Posted 12 November 2009 - 09:19 PM

alright i have no idea who's using who's account
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1