473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Array input

2
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
Sep 24 '17 #1
3 2088
chaarmann
785 Expert 512MB
Please show us what you have written so far and where you are stuck.
Sep 25 '17 #2
Bekir
2
Expand|Select|Wrap|Line Numbers
  1. package dan1;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Nizovi6 {
  6.     public static void main(String [] args){
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = scanner.nextInt();
  9.         int [] index = new int [n];
  10.         int [] score = new int [n];
  11.         for(int i = 0;i < n;i++){
  12.             index[i] = scanner.nextInt();
  13.             score[i] = scanner.nextInt();
  14.         }
  15.         for(int i = 0;i<n;i++){
  16.             for(int j =0;j < n; j++){
  17.                 if(score[i] > score[j]){
  18.                     int t = score[j];
  19.                     score[j] = score[i];
  20.                     score[i] = t;
  21.                 }
  22.             }
  23.         }
  24.         for(int i = 0; i <n;i++){
  25.             System.out.print(index[i] + " "+  score[i]);
  26.             System.out.println();
  27.         }
  28.     }
  29. }
  30.  
  31.  
My output

2
Input 1 2
Input 3 4
Output 1 4
Output 3 2
Sep 25 '17 #3
chaarmann
785 Expert 512MB
You need to swap the indexes also when you swap the scores in your sorting.
So add following lines after line 20:
Expand|Select|Wrap|Line Numbers
  1. int t = score[j];
  2. score[j] = score[i];
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. public class Item {
  2.   private int index;
  3.   private int score;
  4.   public Item(int index, int score) {
  5.     this.index = index;
  6.     this.score = score;
  7.   }
  8.   public void print() {
  9.     System.out.print(index + " " + score);
  10.   }
  11.   public int getScore() {
  12.     return score;
  13.   }
  14. }
Now you only have a single array of "items" instead 2 independent arrays that hold your data.
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 26 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Csaba | last post by:
I'd like to have the cursor in the login form's username input text field when users load the login page. It works fine with the following implementation: <body...
1
by: Srilatha | last post by:
Hi, I want to read a file as a Byte Array Input Stream in VB.Net. How can I declare a Byte Array Input Stream?
8
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and...
2
by: shardul21 | last post by:
hi i am new to java programming.can anyone tell me about how to take array input from user in java program?. also why java is difficult regarding inputs from keyboard(user)?
6
by: pc | last post by:
I was wondering if there is a more gracious way to write the below code: //$input is an array .I just need its 0th and 2nd key-not the 1st.I want to avoid using temp $temp=array_keys($input);...
36
by: laredotornado | last post by:
Hi, I'm using PHP 5. I have an array of strings. What is the simplest way to remove the elements that are empty, i.e. where the expression "empty($elt)" returns true? Thanks, - Dave
2
by: lociteck | last post by:
Hello, How can i give an error depending on the array input? This is my code using System; class MainClass { public static void Main (string args) {
0
by: Kelsier | last post by:
I have a potentially large number of checkboxes that I need to get input from. I have a table in a SQL database that has a list of names that I need to pull out, give 4 checkbox inputs for each of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.