Would anybody mind telling me how to do these questions? Exercises 6: 1) Simplif
ID: 643788 • Letter: W
Question
Would anybody mind telling me how to do these questions?
Exercises 6: 1) Simplify the code below using an array. There shouldn't be any if/else if statements left in your solution. Assume that the integer e passed to the method is between 0 and 4. 2) Write a method that takes an array of doubles and that returns the largest element in the array. 3) Write a method isSorted (double[] a) that takes an array of doubles a and that returns true if the array is sorted and false if not. An array a is sorted if a[0]Explanation / Answer
1)
public static int monetaryValue(int c)
{
int[] value = {1,5,25,100};
return value[c];
}
2)
double maxArray(double dar[], int size) {
double maxV = 0;
for (int i = 0; i < dar.length; i++) {
if (dar[i] > maxV ) {
maxV = dar[i];
}
}
return maxV;
}
3)
public static boolean isSorted(double[] a){
//assume is sorted, attempt to prove otherwise
for(int i = 0; i < a.length-1; i ++){
/*because we are always comparing to the next one and the last one doesn't have a next one we end the loop 1 earlier than usual
*/
if (a[i] > a[i+1]) {
return false; //proven not sorted
}
}
return true; //got to the end, must be sorted
}
4)
void flipArray(String[] a)
{
String temp;
for (int i = 0; i < a.length/2; i++)
{
temp = a[i];
a[i] = a[a.length-1 - i];
a[a.length-1 - i] = temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.