Question

Write a defining table and a function that returns the value of the middle element in...

Write a defining table and a function that returns the value of the middle element in an array. If the array has an even number of elements, then this function must return the average of the two middle elements. The function must have this header:

function getMiddle(list)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// /*Source code of the above program is given below*/

<html>

<body>

<script>

var list = prompt("Enter integer numbers seperated by comma (,) :").split(",")

/*Write a defining table and a function that returns the value of the middle element in an array. If the array has an even number of elements, then this function must return the average of the two middle elements.The function must have this header:function getMiddle(list)*/

function getMiddle(list) {

if(list.length % 2 === 0){

var middle = list.length /2;

//var sum = parseInt(list[middle]) + parseInt(list[middle -1]);

var sum = Number(list[middle]) + Number(list[middle -1]);

var avg = sum/2;

return avg;

}

else{

return Number(list[Math.round((list.length - 1) / 2)]);

}

}

document.write("List elements : " );

for(var i = 0; i < list.length; i++)

document.write(list[i]+" ");

document.write("<br>Middle element : " + getMiddle(list));

</script>

</body>

</html>

/*For better understanding output screenshot with code image is attached here*/

1 <html> 4 5 <script> 6 List elements: 1358 Middle element : 4 var listprompt(Enter integer numbers seperated by comma (,):

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

Add a comment
Know the answer?
Add Answer to:
Write a defining table and a function that returns the value of the middle element in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • Write a function that returns the index of the smallest element in an array of integers....

    Write a function that returns the index of the smallest element in an array of integers. If there are more such elements than one, return the smallest index. C programming

  • Write a function called median that returns the median entry in a 1-dimensional array of integers....

    Write a function called median that returns the median entry in a 1-dimensional array of integers. For example, if we have an array a={3,4,5,1,6}; then the median entry of this array a is 4. You need to sort the array a first, to get {1,3,4,5,6}, then find the entry in the middle position. If the array size is even, it means that array has even number of elements. For example, a={3,2,4,1}, then first sort it, a becomes {1,2,3,4}, then the...

  • Write a value-returning C++ function returns the average length of an array of strings. Name the...

    Write a value-returning C++ function returns the average length of an array of strings. Name the function stringsAverageLength and use the following header: double stringsAverageLength(string array [], int n) { } where the parameter 'array' has 'n' strings and the return value is the average length of all of the strings in the array. For example, if the function is called like this: string cars[3] = { "Toyota", "Ford", "Tesla" }; cout << fixed << setprecision(2) << stringsAverageLength(cars, 3) <<...

  • 1. Write code for a function that receives two parameters (a,and b) by value and two...

    1. Write code for a function that receives two parameters (a,and b) by value and two more parameters (c and d) by reference. All parameters are double. The function works by assigning c to (a/b) and assigning d to (a*b). The function has no return value. From main, use scanf to get two numbers, then call the function, and then display both outcome values to the output in a printf statement. 2. After part 1 is completed, write code to...

  • Write a function with two input parameters: an array of numbers and the size of the...

    Write a function with two input parameters: an array of numbers and the size of the array (the number of elements). The function should return the count of even numbers in the array. For example, if the input to the function is the array [3, 2, 45, 56, 12], the function would return the integer 3. Use the following function header: int countEvens(int nums[], int size) { }

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • (Find the smallest element) use pointers to write a function that finds the smallest element in...

    (Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.

  • Write a C function named isSymmetric, the prototype of which is given below, that returns 1...

    Write a C function named isSymmetric, the prototype of which is given below, that returns 1 if the elements of an array of integers named myArray of size n are symmetric around the middle. If the array elements are not symmetric, the function should return 0. Both the array and its size are specified as parameters.

  • Write a function with header [d] = myIEEE2Dec(IEEE) Write a function with header [d]=myIEEE2Dec (IEEE), where...

    Write a function with header [d] = myIEEE2Dec(IEEE) Write a function with header [d]=myIEEE2Dec (IEEE), where IEEE is a 1 * 32 array of ones and zeros representing a 32-bit IEEE754 number. The output should be d, the equivalent decimal representation of IEEE. The input variable IEEE will always be a 32-element array of ones and zeros defining a 32-bit single precision float.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT