Create a method called roundAllUp(), which takes in an array of doubles and retu
ID: 3736159 • Letter: C
Question
Create a method called roundAllUp(), which takes in an array of doubles and returns a new array of integers. This returned array contains all the numbers from the array of doubles, but rounded up. Below are some example input-output pairs:
Write a method isPalindrome() that takes in a String and returns true if it is a palindrome, meaning it is the same string if it is reversed.
Write a method named minGap that takes in an array of integers and returns the minimum ‘gap’ between 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 we have the following array:
And finally, you need to call these methods from your main method. The sequence of the method calls are something like below:
Explanation / Answer
I am writing the code in Java(since template is given in Java).
Please write the logic in Java editor, you will be able to get the output as needed.
int[] roundAllUp(double[] da)
{
int[] ia; //int array
for(int i = 0; i< da.length; i++)
ia[i] = Math.ceil(da[i]); // stores the upper bound into ia[i]
return ia;
}
bool isPalindrome(char[] arrData)
{
bool isPalindrome = true;
for(int i=0, j=arrData.length()-1; i!=j ; i++, j--)
{
if(arrData[i] != arrData[j])
{
isPalindrome = false;
break;
}
i++;
j--;
}
}
//Utility to get the large and small value between 2 values
int getLarger(int a, int b)
{
if(a >= b)
return a;
return b;
}
int getSmaller(int a, int b)
{
if(a <= b)
return a;
return b;
}
int minGap(int[] a)
{
int minGap = 100000;//Assume Some max int value
for(int i=0, i < arrData.length() ; i ++ )
{
int large = getLarger(arrData[i], arrData[i+1]);
int small = getSmaller(arrData[i], arrData[i+1]);
if(minGap > (large-small))
{
minGap = (large-small);
}
}
return minGap
}
main()
{
double [] doubleArray = {1.8, 2.3, 3.9, 5.0, 6.01};
int [] intArray = {1, 23, 53, 58, 85, 100, 122};
String str = "Madam";
int [] roundUpArray = roundAllUp(doubleArray) ;
System.out.println("After rounding up: ");
//print every element of the roundUpArray
System.out.println(str + " is Palindrome: "+ isPalindrome(str) );
System.out.println("Min gap array : ");
//print every element of the intArray
System.out.println("Min gap is: " + minGap(intArray) );
}
Please let me know if yoou dont get anything in the logic of the program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.