Results 1 to 9 of 9
  1. #1
    ah choy is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Post guys....im having problem with this coding question.do help me

    Write a method named percentEven that accepts an array of integers as a parameter and returns the percentage of even numbers in the array as a real number. For example, if a variable named nums refers to an array of the elements {6, 2, 9, 11, 3}, then the call of percentEven(nums) should return 40.0. If the array contains no even elements or no elements at all, return 0.0.

  2. #2
    delion19 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    what have you done so far? What does your code look like?

  3. #3
    ah choy is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    public static void main(String[] args) {
    // TODO code application logic here
    int[] nums = new int[5];
    Scanner in = new Scanner (System.in);

    // Prompt the user to enter no
    System.out.println("Enter 5 numbers");
    for(int s = 0; s < nums.length ; s++){
    nums[s] = in.nextInt();
    }


    // calculate even percent in method
    percentEven(nums);
    }
    // method to calculate even
    public static void percentEven(int[] no){
    // declare variable
    double percent = 0 ;
    double even = 0;
    //Loop to detact no of even,
    for(int e = 0; e < no.length; e++){
    if(no[e] % 2 == 0){
    even++;
    }
    }//end loop

    // percent of even no
    percent = (even / no.length) *100;

    //Print the percentage in even no
    System.out.println("The even percent in this array is " + percent);

    }

    }

  4. #4
    delion19 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    What exactly is the problem then? Is it the return statement for if there are no even numbers in the list? If that is the case, you are going to need to surround

    percent of even no
    percent = (even / no.length) *100;

    with an 'if' and 'else' statement so that the '0.0' can be returned if there are no even numbers

  5. #5
    ah choy is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    ok..how about this one........

    Write a method named minGap that accepts an integer array as a parameter and returns the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in an array is defined as the second value minus the first value. For example, suppose a variable called array is an array of integers that stores the following sequence of values:
    int[] array = {1, 3, 6, 7, 12};
    The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. If you are passed an array with fewer than 2 elements, you should return 0.


    this is my coding

    public static void main(String[] args) {
    // TODO code application logic here

    int[] array = {1, 3, 6, 7, 12};
    for(int i=1;i<array.length; i++){
    int gap = array[i] - array[i-1];
    System.out.printf("Gap is : %d ( %d - %d ) = %d\n" ,
    i,array[i],array[i-1],gap);


    }
    }
    public static int minGap(int[] list) {
    if (list.length < 2) {
    return 0;
    } else {
    int min = list[1] - list[0];
    for (int i = 2; i < list.length; i++) {
    int gap = list[i] - list[i - 1];
    if (gap < min) {
    min = gap;
    }
    }
    return min;
    }
    }
    }

  6. #6
    ah choy is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    how to return the minimum gap between the number?

  7. #7
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    I don't see any issues with your code, beyond that you never call minGap(int[]). What seems to be the issue?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,243
    Rep Power
    19

    Default Re: guys....im having problem with this coding question.do help me

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    delion19 is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: guys....im having problem with this coding question.do help me

    you're gonna have to add some additional information (2 or so lines) in your main method that will compare the gap to some variable containing the min gap and update it as necessary.

Similar Threads

  1. Huffman coding algorithm question?
    By knguye88 in forum New To Java
    Replies: 1
    Last Post: 03-12-2012, 09:06 AM
  2. Hi guys! Noob question...
    By moominboy in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 04:43 PM
  3. Hey guys quick question on my project
    By Phyxed in forum New To Java
    Replies: 1
    Last Post: 03-04-2011, 06:35 PM
  4. question about coding conventions
    By gib65 in forum New To Java
    Replies: 8
    Last Post: 08-05-2010, 04:24 AM
  5. Hi guys, 1 QUESTION please!..
    By nimd4 in forum Introductions
    Replies: 2
    Last Post: 01-26-2010, 03:32 AM

Posting Permissions

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