Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: a more efficent way?

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default a more efficent way?

    I am trying to find a more efficent/proper way to solve this problem:

    Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.


    ****1 - 10 | *****
    ***11 - 20 | ****
    ***21 - 30 | *********
    ***31 - 40 | **
    ***41 - 50 | **************
    ***51 - 60 | ******
    ***61 - 70 | ****
    ***71 - 80 | ********
    ***81 - 90 | *
    **91 - 100 | ***

    and here is what i have. i am using two ways to solve the problem. by the way, is there any way you can integrate range in with the switch statement? (and maybe you can help me figure out how to get the second method of solving this, to work! =D )

    Thanks in advance.

    Java Code:
    Scanner scan = new Scanner (System.in);
    int[] numbersArray;
    int[] histogram = new int[10];
    String[] groups = {"1 - 10 | ", "11 - 20 | ", "21 - 30 | ", "31 - 40 | ", "41 - 50 | ", "51 - 60 | ", "61 - 70 | ", "71 - 80 | ", "81 - 90 | ", "91 - 100 | "};
    String msg;
    
    System.out.println("Please enter lines of text (or 'done'):");
    
    do {
    	msg = scan.nextLine();
    
    	if (!msg.equals("done")) {
    
    		int number = Integer.parseInt(msg);
    
    // here is one way to solve, with lots of code built inside of the main method
    		if (number > -1 && number < 101)
    		{
    			if(number <= 10) {
    	histogram[0] += 1;
    }
    if(number <= 20 && number >=11) {
    	histogram[1] += 1;
    }
    if(number <= 30 && number >=21) {
    	histogram[2] += 1;
    }
    if(number <= 40 && number >=31) {
    	histogram[3] += 1;
    }
    if(number <= 50 && number >= 41) {
    	histogram[4] += 1;
    }
    if(number <= 51 && number >=60) {
    	histogram[5] += 1;
    }
    if(number <= 70 && number >=61) {
    	histogram[6] += 1;
    }
    if(number <= 80 && number >=71) {
    	histogram[7] += 1;
    }
    if(number <= 90 && number >=81) {
    	histogram[8] += 1;
    }
    if(number <= 100 && number >=91) {
    	histogram[9] += 1;
    }
    		}
    		
    	}
    
    	if (msg.equalsIgnoreCase("done")) {
    		break;
    	} 
    	
    }
    	while (true);
    
    for (int i = 0; i<histogram.length; i++)
    {
    	System.out.print(groups[i]);
    	
    	int count = histogram[i];
    	
    	for(int k = 0; k < count; k++)
    	{
    		System.out.print("*");
    	}
    	System.out.print("\n");
    }
    
    // here is another way to solve this, with a method
    int number = 6;
    numbersArray = valueRange(number, histogram);
    System.out.println("\n");
    for (int i=0; i<numbersArray.length; i++) {
    	System.out.println(numbersArray[i]);
    }
    
    }
    
    
    public static int[] valueRange (int number, int[] histogram) {
    for (int i=0; i<histogram.length; i++) {
    if(number <= 10) {
    	histogram[0] += 1;
    }
    if(number <= 20 && number >=11) {
    	histogram[1] += 1;
    }
    if(number <= 30 && number >=21) {
    	histogram[2] += 1;
    }
    if(number <= 40 && number >=31) {
    	histogram[3] += 1;
    }
    if(number <= 50 && number >= 41) {
    	histogram[4] += 1;
    }
    if(number <= 51 && number >=60) {
    	histogram[5] += 1;
    }
    if(number <= 70 && number >=61) {
    	histogram[6] += 1;
    }
    if(number <= 80 && number >=71) {
    	histogram[7] += 1;
    }
    if(number <= 90 && number >=81) {
    	histogram[8] += 1;
    }
    if(number <= 100 && number >=91) {
    	histogram[9] += 1;
    }
    }
    return histogram;
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,416
    Rep Power
    20

    Default Re: a more efficent way?

    is there any way you can integrate range in with the switch statement?
    Not in general, but for your requirement you could
    Java Code:
    switch ((n -1) / 10)
    Also, +=1 is equivalent to the post increment operator ++
    db
    sunde887 likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •