A wave array is defined to an array which does not contain two even numbers or two odd numbers in adjacent
locations. So {7, 2, 9, 10, 5}, {4, 11, 12, 1, 6}, {1, 0, 5} and {2} are all wave arrays. But {2, 6,
3, 4} is not a wave array because the even numbers 2 and 6
are adjacent to each other. Also {3, 4, 9, 11, 8} is not a wave array because the odd numbers 9 and 11 are
adjacent to each other. You can
assume array has at least one element.
Write a function named isWave that returns 1 if its array argument is a Wave array, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isWave (int [ ] a)
If you are programming in C or C++, the function signature is
int isWave (int a[ ], int len) where len is the number of elements in the array.
Solution:
}
Write a function named isWave that returns 1 if its array argument is a Wave array, otherwise it returns 0.
If you are programming in Java or C#, the function signature is
int isWave (int [ ] a)
If you are programming in C or C++, the function signature is
int isWave (int a[ ], int len) where len is the number of elements in the array.
Solution:
public int isWaveArray(int [] a)
{
int rtnVal
= 1;
for(int i=0;i<a.Length-1;i++)
{
if(a[i]%2==0)
{
if(a[i+1]%2==0)
{
rtnVal = 0;
break;
}
}
else
{
if(a[i+1]%2!=0)
{
rtnVal = 0;
break;
}
}
}
return rtnVal;
}
0 comments:
Post a Comment