Question

In: Computer Science

php please // 5) isArraySorted // Given an array , return true if the element are...

php please

// 5) isArraySorted

// Given an array , return true if the element are in ascending order.

// Note: 'abe' < 'ben' and 5 > 3

// Precondition $arr has all the same type of elements

function isArraySorted($arr) {

  

}

echo PHP_EOL . 'isArraySorted(array(1, 2, 2, 99) 1: ' . isArraySorted ( array (

1,

2,

2,

99 ) ) . "\n";

assert ( isArraySorted ( array (

1,

2,

2,

99 ) ) );

echo 'isArraySorted(array(2, 2, 1) false: ' . isArraySorted ( array (

2,

2,

1 ) ) . "\n";

Solutions

Expert Solution

The solution for the above problem is given below with the screenshots for indentation and if you feel any problem then feel free to ask.

<?php

// defining a function isArraySorted and passing an array into it as argument

function isArraySorted($arr)

{

// storing the size of array in separate variable

$n = sizeof($arr);

// returning true if the size of array is 1 or 0

if($n<2)

{

return "true";

}

else {

// loop for returning if any greater value is occured before smaller value relative to it

for ($i=0; $i < $n-1 ; $i++) {

if ($arr[$i] > $arr[$i+1])

{

return "false";

}

}

//returning true if no greater value occured before smaller one

// implying the array is in ascending order

return "true";

}

}

echo PHP_EOL . 'isArraySorted(array(1, 2, 2, 99) 1: ' . isArraySorted ( array (1,2,2,99 ) ) . "\n";

assert ( isArraySorted ( array (1,2,2,99 ) ) );

echo 'isArraySorted(array(2, 2, 1) false: ' . isArraySorted ( array (2,2,1 ) ) ."\n";

?>

NOTE:- If you want to print true or false then this is for that as it is returning true/false as strings but if you want to get the boolean values then you can change the return statement simply from "true"/"false" to true/false. But in the case of bool the echo command do not produce output for false and for true it generates 1.


Related Solutions

Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
Write recursive method to return true if a given array has element equal to employee emp,...
Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp, and returns...
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm? Java Language....
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
How to print the element in the fifth row and seventh column given this array declaration...
How to print the element in the fifth row and seventh column given this array declaration in dev c: int hello[5][10];
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT