Question
Write a small program in the Go language that implements and tests a simple function to find the maximum value in an array of non-negative
Write a small program in the Go language that implements and tests a simple function to find the maximum value in an array of non-negative (i.e. 0) integers. The function returns an error code if either the array of empty (code is -1) or if negative values appear in the array (code is -2).
Two example implementations in C and JavaScript are provided. Your solution should mimic these programs, and produce very similar output. Links are provided to the example solutions so that you can compile and run the C code, or run the JavaScript code in your browser.
Implement 3 functions: main, test, and max. (Note that the JavaScript program does not use a separate main function, but the C program does and your Go programs will.)
Use the same three test arrays as shown in the example programs.
Note that the C version passes an explicit array length value to test and max, while the JavaScript does not. This is because JavaScript can check array sizes while C cant. Go, like JavaScript, can check array sizes, so you will not need the explicit size parameter to these two functions.
Your Go max function should be different from the two provided examples on one key aspect: it must return two values. The C/JavaScript max function returns a single integer that is the maximum value found in the array; if the array is empty or contains negative values a negative values error code is returned. This is bad programming as the single return value has two meanings: either an error indicator or a maximum value. Go (like Haskell) can return multiple values. You should use a Go function declaration like func max(a []int) (err int, val int)
that returns an error code as well as the maximum value. If an error occurs you can return any value for val as it will not be used. Include a listing of the program in the assignment submission. I will not be compiling and executing the code on my machine, so dont submit a separate source file. All you need to know about Go is at the language web site https://golang.org/. If you wish you can download and install the Go system as documented there. (Linux users can simply install the Go package: golang.) If you dont wish to install Go on your own computer, you can use the Go Playground facility at https://golang.org/ to develop and run your code. If you do this, then you can use the Share link to create a link to your saved code. Include this link in your assignment submission so that I can view/run your code.
C example #includeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started