15.5k views
4 votes
In Coral language

When we decide on a Function we want to write, we would like to be able to reuse that Function in future programs. Thus, we want the Function to do one thing and only one thing, and to do that one thing well. We would like the thing the Function does to be something we will likely need in the future, so we can reuse the code and don't have to keep rewriting similar code. In this LAB, you will write a Utility Function named DisplayArrayOfIntegers that displays the contents of an Array of integers which is passed as an argument to the Function. In this LAB, the Array will be an integer array. We would have to write another (very similar function) if we wanted to have a Function that displays the values in an array with floating points. The Function header will be: Function DisplayArrayOfIntegers(integer array(?) userVals) returns nothing Even though the function header shows a variable sized array with a array(?), the array that is passed to the function will have a length. The variable userVals.size will be the length of the array. The Main() function is given to you. The Main() function you are given reads in a length of an array followed by the contents of the array from input, and then calls the function you are to write. Important: Do NOT modify or write any code other than in Function DisplayArrayOfIntegers(). Do NOT Put any information to output in any other part of the program other than Function DisplayArrayOfIntegers() Do NOT read any input in Function DisplayArrayOfIntegers() The one purpose of DisplayArrayOfIntegers() is to display the elements in an array of integers. For example, if the input is: 5 1 2 3 4 5 The function you write should put the following to output: 1 2 3 4 5

Here is the code given

Function DisplayArrayOfIntegers(integer array(?) userVals) returns nothing
DisplayArrayOfIntegers()
// Display the elements in an array to output
// Put your code here, and do not change any other part of this program

Function Main() returns nothing
integer i
integer len
integer array(?) myArray

// Read the length of the array from input
len = Get next input

// Set the length of the array
myArray.size = len

// Populate the values in an array
for i = 0; i < myArray.size; i = i + 1
myArray[i] = Get next input

// Call the Function to Display Array Contents
DisplayArrayOfIntegers(myArray)

// DO NOT Put anything to output in the Main() function

1 Answer

1 vote

Final answer:

The task is to implement a Coral language function, DisplayArrayOfIntegers, that outputs the elements of an integer array. It must adhere to clean code principles, focusing only on the functionality of displaying array elements.

Step-by-step explanation:

The question involves writing a function in the Coral programming language. The function DisplayArrayOfIntegers should take an array of integers as an argument and display the contents of this array. The key aspects of this function are that it should abide by the principles of reusability and single responsibility by focusing only on the task of outputting the array's elements to the display.

To write the DisplayArrayOfIntegers function, you will need to iterate over the array using a loop and print each element. Here is an example of how the implementation might look in pseudocode:

  • Start the loop from zero up to userVals.size - 1.
  • Inside the loop, use a print statement to display each element followed by a space.
  • End the loop.

Remember not to add any code outside the designated function as per the LAB instructions, nor read any input or produce output from any other function.

User Mikejohnstn
by
7.9k points