[Show all top banners]

devilwithin999
Replies to this thread:

More by devilwithin999
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 Java Program Help Plz
[VIEWED 3837 TIMES]
SAVE! for ease of future access.
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

        


 
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

 
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);                      
                         

             }
               
 
}


 
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


 
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.

 
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

 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 200 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
ChatSansar.com Naya Nepal Chat
Toilet paper or water?
TPS EAD auto extended to June 2025 or just TPS?
Biden out, Trump next president, so what’s gonna happen to TPS, termination?
and it begins - on Day 1 Trump will begin operations to deport millions of undocumented immigrants
I hope all the fake Nepali refugee get deported
From Trump “I will revoke TPS, and deport them back to their country.”
Tourist Visa - Seeking Suggestions and Guidance
Anybody gotten the TPS EAD extension alert notice (i797) thing? online or via post?
advanced parole
TPS Renewal Reregistration
Sajha Poll: Who is your favorite Nepali actress?
Biden said he will issue new Employment visa for someone with college degree and job offers
Why Americans reverse park?
Nepali Passport Renew
Driver license help ASAP sathiharu
They are openly permitting undocumented immigrants to participate in federal elections in Arizona now.
ढ्याउ गर्दा दसैँको खसी गनाउच
To Sajha admin
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters