473,387 Members | 3,801 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,387 software developers and data experts.

need help in sorting array(java)!!!

THanks~
this is the example and my quote are below
Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal.

Example 1:
Input:
1
2
3
4
5

Output:
5
4
3
2
1
1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 2 smaller, 0 equal
3 - 2 larger, 2 smaller, 0 equal
4 - 1 larger, 3 smaller, 0 equal
5 - 0 larger, 4 smaller, 0 equal



and my qupte are as follow but i am lose in how to
count how many int are greater/less/equals then the currenty index int in a array. everytime the max value changes. Not sure how to make the max constant after it find the max int number in the array.

public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);

int[] numbers = new int[5];

for(int index = 0 ; index < numbers.length; index ++)
{
System.out.print("enter number"+(index + 1) + " : ");

numbers[index] = scan.nextInt();
}


for (int j=0; j <= (numbers.length -1) ;j++)

int small = 0;
int greater = 0;
int count= 0;


for (int n=(numbers.length -1); n>=0 ; n--)

if (numbers[j] > numbers[n])
{
greater++;
}
else if (numbers[j] < numbers[n])
{
small++;
}
else if (numbers[j] == numbers[n])
{
count++;
}

System.out.println(+values[j]+": " + greater+ "greater: "
+small+ "smaller: " +count+ "equal" );
Mar 31 '07 #1
2 1775
r035198x
13,262 8TB
THanks~
this is the example and my quote are below
Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal.

Example 1:
Input:
1
2
3
4
5

Output:
5
4
3
2
1
1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 2 smaller, 0 equal
3 - 2 larger, 2 smaller, 0 equal
4 - 1 larger, 3 smaller, 0 equal
5 - 0 larger, 4 smaller, 0 equal



and my qupte are as follow but i am lose in how to
count how many int are greater/less/equals then the currenty index int in a array. everytime the max value changes. Not sure how to make the max constant after it find the max int number in the array.

public static void main( String[] args )
{
Scanner scan = new Scanner(System.in);

int[] numbers = new int[5];

for(int index = 0 ; index < numbers.length; index ++)
{
System.out.print("enter number"+(index + 1) + " : ");

numbers[index] = scan.nextInt();
}


for (int j=0; j <= (numbers.length -1) ;j++)

int small = 0;
int greater = 0;
int count= 0;


for (int n=(numbers.length -1); n>=0 ; n--)

if (numbers[j] > numbers[n])
{
greater++;
}
else if (numbers[j] < numbers[n])
{
small++;
}
else if (numbers[j] == numbers[n])
{
count++;
}

System.out.println(+values[j]+": " + greater+ "greater: "
+small+ "smaller: " +count+ "equal" );
1.) Please use code tags when posting code.
2.) Your example seems to be contradicting your problem statement
"The program will then output the values in order with a statement on how many of the remaining values are larger, smaller, or equal"

So isn't it supposed to be

1 - 4 larger, 0 smaller, 0 equal
2 - 3 larger, 0 smaller, 0 equal
3 - 2 larger, 0 smaller, 0 equal
4 - 1 larger, 0 smaller, 0 equal
5 - 0 larger, 0 smaller, 0 equal
Mar 31 '07 #2
i am really confused about the same/smaller/ larger counter
since i am a beginner in java programming

so what errors did i made in this program?
and how can i correct it

thanks~
Mar 31 '07 #3

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

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: Ben Fidge | last post by:
Hi What are the options for sorting a StringCollection? I'm quite surprised that StringCollection doesn't have any sorting capability built in! Thanks Ben
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
3
by: Curtis | last post by:
I am trying to do a simple sort on a list view.With the listview control the first time I sort on the control it sorts the items but anytime after that it fails to sort. I'm sure this is a simple...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.