Results 1 to 9 of 9
- 05-03-2012, 04:18 AM #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.
- 05-03-2012, 04:22 AM #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?
- 05-03-2012, 04:35 AM #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);
}
}
- 05-03-2012, 04:55 AM #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
- 05-03-2012, 05:06 AM #5
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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;
}
}
}
- 05-03-2012, 05:07 AM #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?
- 05-03-2012, 03:36 PM #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?
- 05-03-2012, 04:20 PM #8
Re: guys....im having problem with this coding question.do help me
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-03-2012, 06:37 PM #9
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Huffman coding algorithm question?
By knguye88 in forum New To JavaReplies: 1Last Post: 03-12-2012, 09:06 AM -
Hi guys! Noob question...
By moominboy in forum New To JavaReplies: 1Last Post: 09-26-2011, 04:43 PM -
Hey guys quick question on my project
By Phyxed in forum New To JavaReplies: 1Last Post: 03-04-2011, 06:35 PM -
question about coding conventions
By gib65 in forum New To JavaReplies: 8Last Post: 08-05-2010, 04:24 AM -
Hi guys, 1 QUESTION please!..
By nimd4 in forum IntroductionsReplies: 2Last Post: 01-26-2010, 03:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks