Array input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bekir
    New Member
    • Sep 2017
    • 2

    Array input

    Hello,I have question about arrays.I know how to input array with for loop but i have to learn how to input array like this:
    n - number of elements
    we've got index and score of player and i have to sort it from the biggest.I did all good but i stuck on one thing.
    Ex: n=3
    index score
    1 20
    2 30
    3 10
    it has to output
    2 30
    1 20
    3 10
    but my program's output is:
    1 30
    2 20
    3 10
    I need code that make this index follow my score.
    Thank you
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Please show us what you have written so far and where you are stuck.

    Comment

    • Bekir
      New Member
      • Sep 2017
      • 2

      #3
      Code:
      package dan1;
      
      import java.util.Scanner;
      
      public class Nizovi6 {
      	public static void main(String [] args){
      		Scanner scanner = new Scanner(System.in);
      		int n = scanner.nextInt();
      		int [] index = new int [n];
      		int [] score = new int [n];
      		for(int i = 0;i < n;i++){
      			index[i] = scanner.nextInt();
      			score[i] = scanner.nextInt();
      		}
      		for(int i = 0;i<n;i++){
      			for(int j =0;j < n; j++){
      				if(score[i] > score[j]){
      					int t = score[j];
      					score[j] = score[i];
      					score[i] = t;
      				}
      			}
      		}
      		for(int i = 0; i <n;i++){
      			System.out.print(index[i] + " "+  score[i]);
      			System.out.println();
      		}
      	}
      }
      My output

      2
      Input 1 2
      Input 3 4
      Output 1 4
      Output 3 2
      Last edited by Frinavale; Sep 25 '17, 01:48 PM.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        You need to swap the indexes also when you swap the scores in your sorting.
        So add following lines after line 20:
        Code:
        int t = score[j];
        score[j] = score[i];
        score[i] = t;
        By the way, professionals use objects to keep the data together. What if someone needs to add a name for each item? If you do it as a third array: Does he really think of modifying the code everywhere where the array content is swapped? Like you sorted the score but forgot to sort the corresponding index? So better is that:
        Code:
        public class Item {
          private int index;
          private int score;
          public Item(int index, int score) {
            this.index = index;
            this.score = score;
          }
          public void print() {
            System.out.print(index + " " + score);
          }
          public int getScore() {
            return score;
          }
        }
        Now you only have a single array of "items" instead 2 independent arrays that hold your data.
        Code:
        Item [] data= new Item[n];
        It's up to you for exercise to use it in your program.
        If you have problems doing so, come back to me.

        Comment

        Working...