[VIEWED 3828
TIMES]
|
SAVE! for ease of future access.
|
|
|
devilwithin999
Please log in to subscribe to devilwithin999's postings.
Posted on 04-28-08 8:33
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hey guys, plz help me with my program.
Plz make this program and submit it, I would be very grateful.
Thanks in advance.
Sort the array in ascending order before counting using JAVA Programming
It should ask user for numbers using array and then arrange in ascending order and count the reapeated number.
Suppose user inputs: 1 4 5 4 4 1
The output should be similar to the following sample output:
Number Count
1 2
4 3
5 1
. .
. .
. .
Thanks
|
|
|
|
ganatantranepal
Please log in to subscribe to ganatantranepal's postings.
Posted on 04-28-08 9:25
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hey mate, Problem solved !!!!!! /* FOR SAJHA MATE */ package sajhabro; public class Sort { public static void main(String[] args) { int[] a = new int[] { 1, 4, 5, 4, 4, 1 }; int temp = 0, count = 1; //SORTS UNSORTED ARRAY for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } System.out.println("Number\t\tCount"); //PRINTS SORTED ARRAY AND COUNTER for (int i = 0; i < a.length; i++) { for (int j = i + 1; j < a.length; j++) { if (a[i] == a[j] && a[j] != '*') { a[j] = '*'; count++; } } if (a[i] != '*') System.out.println(a[i] + "\t\t" + count); count = 1; } } }
Last edited: 28-Apr-08 09:47 PM
Last edited: 28-Apr-08 09:49 PM
|
|
|
techGuy
Please log in to subscribe to techGuy's postings.
Posted on 04-28-08 9:53
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
ganatantranepal has done it pretty nicely, mine looks messsy, this will take 10 input from user
import java.lang.*; import java.io.*;
public class jayNepal { public static void main(String[] args) throws Exception { // Read from console BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
int[] a=new int[10]; for(int i=0;i<10;i++) { System.out.println("Enter value:"); String input = stdin.readLine(); // from console input example above. a[i] = Integer.parseInt( input ); }
//Sort for(int i=0;i<10;i++) for(int j=i;j<10;j++) { if(a[i]>a[j]) { int temp=a[i]; a[i]=a[j]; a[j]=temp; } } //Count System.out.println("Number\tCount"); System.out.print(a[0]+"\t"); int count=1; for(int i=1;i<10;i++) { if(a[i]==a[i-1]) { count++;} else { System.out.print(count +"\n" + a[i]+"\t"); count=1; } } System.out.print(count);
} }
|
|
|
devilwithin999
Please log in to subscribe to devilwithin999's postings.
Posted on 04-29-08 1:52
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
Hey dude suike Why are you jealous? Is it because you didnot get an idea to get help like this and got poor gpa?? sorry for ur gpa.... anyways thanks techguy and ganatantranepal.. i will never forget ur help.. jai nepal
|
|
|
khai_k_khai_k
Please log in to subscribe to khai_k_khai_k's postings.
Posted on 04-29-08 4:34
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
hi DevilWithin999, I got a linear time (O(n)) solution but it might take a lot of space in worst case. public class Sort { public static void main(String[] args) { int[] inputArray = new int[]{1, 4, 5, 4, 4, 1}; //Calculate the maximum length required for bucket array int max = inputArray.length; for(int i:inputArray){ if(i>max){ max = i; } } int bucketArray[] = new int[max]; for(int i =0;i<inputArray.length;i++){ bucketArray[inputArray[i]]++; } for(int i = 0;i<bucketArray.length;i++){ if(bucketArray[i]!=0){ System.out.println("Number : " + i + " Count: " + bucketArray[i]); } } } } Happy Programming.
|
|
|
khai_k_khai_k
Please log in to subscribe to khai_k_khai_k's postings.
Posted on 04-29-08 4:35
PM
Reply
[Subscribe]
|
Login in to Rate this Post:
0
?
|
|
and the output is: Number : 1 Count: 2 Number : 4 Count: 3 Number : 5 Count: 1
|
|