(b) Write a method public int[] [] separate(int [] a) that returns a 2D array wh
ID: 663343 • Letter: #
Question
(b) Write a method public int[] [] separate(int [] a) that returns a 2D array where the first row contains the even-indexed elements from a, and the second row contains the odd-indexed elements. For example separate({8. 1O,4,9356}) returns {{8,4,3},{10,9,56}}. (a) Write a method public boolean moreUpsThanDowns(int[] a) that returns true if and only if the elements of a increase more often than they decrease. For example, moreUpsThanDowns({1,4,4,2,3,3}) returns true, because the elements increase twice (1?4 and 2?3) but they decrease only once (4?2); but moreUpsThanDowns ({4 ,2,2, 2, 3}) returns false, because the elements increase once and decrease once.Explanation / Answer
public boolean moreUpsThanDowns( int[] a)
{
int count1=0,count2=0;
boolean flag;
//n is the length of array
for(i=0;i<n;i++)
{
if(a[i]>a[i++])
count1++;
else
count2++;
}
if(count1>count2)
flag=true;
else
flag=false;
return flag;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.