Humza
Humza

Reputation: 13

How to correlate array's indexes in java

I have a slight problem in correlating the indexes of two arrays, and I can't seem to figure it out. For example, I have an

ID array{365, 222, 306, 203, 113, 208,213}

and

Scores array{265,262,257,256,253,246,246}

They match perfectly: ID[0] matches with scores[0],

The task: is to read 21 pairs of numbers(ID number and score respectively) into two separate arrays. Write out the numbers paired and ranked by score from high to low with column headings.

So far I have been able to successfully print the scores in descending order, however am unable to successfully correlate them with the correct ID number.

Any help is appreciated.

This is my code so far:

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class Program408a 
{
    public static void main(String[]args)throws Exception
    {
        Scanner input = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
        Scanner input1 = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
        System.out.println("ID\tScore");
        int count = 0;
        while(input.hasNextInt())
        {
            count++;
            input.nextLine();
        }   
        int[] iD = new int [count];
        int[] scores = new int[count];
        for(int i = 0; i < count; i++ )
        {
            iD[i] = input1.nextInt();
            scores[i] = input1.nextInt();
        }
        int[] iDCopy = new int[count];
        int[] scoresCopy = new int[count];
        System.arraycopy(iD, 0, iDCopy, 0, iD.length);
        System.arraycopy(scores, 0, scoresCopy, 0, scores.length);
        System.out.println(Arrays.toString(iD));        
        System.out.println(Arrays.toString(scores));                
        int max = scores[0];                
        int index = 0;      
        for(int i = 0; i < count; i++)
        {
            if(max <= scores[i])
            {
                max = scores[i];    
                index = i;
            }               
        }                   
        for (int i = 0; i < count; i++)
        {
            for (int x = i + 1; x < count; x++)
            {
                if (scoresCopy[i] < scoresCopy[x])
                {
                    int holder = scoresCopy[i];
                    scoresCopy[i] = scoresCopy[x];
                    scoresCopy[x] = holder;  
               }
            }
        }
        System.out.println(Arrays.toString(iDCopy));
        System.out.println(Arrays.toString(scoresCopy));            
    }       
}

Upvotes: 1

Views: 107

Answers (2)

Deepak Kumar
Deepak Kumar

Reputation: 69

You should swap the index of iDCopy while swapping the index of scoresCopy.

Make this change in your code

for (int i = 0; i < count; i++)
{
    for (int x = i + 1; x < count; x++)
    {
        if (scoresCopy[i] < scoresCopy[x])
        {
            int holder = scoresCopy[i];
            scoresCopy[i] = scoresCopy[x];
            scoresCopy[x] = holder;

            holder = iDCopy[i];
            iDCopy[i] = iDCopy[x];
            iDCopy[x] = holder;
        }
    }
}

The new code should look like this

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;
public class Program408a 
{
    public static void main(String[]args)throws Exception
    {
        Scanner input = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
        Scanner input1 = new Scanner(new File("C:\\Users\\humza\\Documents\\Eclipse Projects\\Projects\\src\\prg408a.dat"));
        System.out.println("ID\tScore");
        int count = 0;
        while(input.hasNextInt())
        {
            count++;
            input.nextLine();
        }   
        int[] iD = new int [count];
        int[] scores = new int[count];
        for(int i = 0; i < count; i++ )
        {
            iD[i] = input1.nextInt();
            scores[i] = input1.nextInt();
        }
        int[] iDCopy = new int[count];
        int[] scoresCopy = new int[count];
        System.arraycopy(iD, 0, iDCopy, 0, iD.length);
        System.arraycopy(scores, 0, scoresCopy, 0, scores.length);
        System.out.println(Arrays.toString(iD));        
        System.out.println(Arrays.toString(scores));                
        int max = scores[0];                
        int index = 0;      
        for(int i = 0; i < count; i++)
        {
            if(max <= scores[i])
            {
                max = scores[i];    
                index = i;
            }               
        }                   
        for (int i = 0; i < count; i++)
        {
            for (int x = i + 1; x < count; x++)
            {
                if (scoresCopy[i] < scoresCopy[x])
                {
                    int holder = scoresCopy[i];
                    scoresCopy[i] = scoresCopy[x];
                    scoresCopy[x] = holder;  

                    holder = iDCopy[i];
                    iDCopy[i] = iDCopy[x];
                    iDCopy[x] = holder;
               }
            }
        }
        System.out.println(Arrays.toString(iDCopy));
        System.out.println(Arrays.toString(scoresCopy));            
    }       
}

Hope this solve your problem.

Upvotes: 0

Henry
Henry

Reputation: 43758

When you do the sorting, just make the same changes to the iD array:

...
int holder = scoresCopy[i];
int iDholder = iDCopy[i];
scoresCopy[i] = scoresCopy[x];
iDCopy[i] = iDCopy[x];
scoresCopy[x] = holder;
iDCopy[x] = iDholder;  
...

And by the way, using two separate arrays is the FORTRAN way of doing things. In a modern programming language you would group the two numbers into one structure/record/class and then use just one array with this class as elements.

Upvotes: 2

Related Questions