# Thread: guys....im having problem with this coding question.do help me

1. Member
Join Date
May 2012
Posts
4
Rep Power
0

## 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. Member
Join Date
May 2012
Posts
5
Rep Power
0

## 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. Member
Join Date
May 2012
Posts
4
Rep Power
0

## 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. Member
Join Date
May 2012
Posts
5
Rep Power
0

## 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. Member
Join Date
May 2012
Posts
4
Rep Power
0

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

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. Member
Join Date
May 2012
Posts
4
Rep Power
0

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

how to return the minimum gap between the number?

7. Senior Member
Join Date
Feb 2012
Posts
117
Rep Power
0

## 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?

9. Member
Join Date
May 2012
Posts
5
Rep Power
0

## 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.

#### Posting Permissions

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