Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribedimage text in transcribed

C example #include void test (int a 0 int len) forward declaration int max (int a 0, int len) forward declaration void main int a10 15,9,3,9,7); int len1 size of sizeof (int) int len2 size of sizeof (int) int a20 int a30 15,-9,3 int len size of (a3) /sizeof (int) test Cal,len1); test (a2,len2); tests (a3,len3) void test (int a0, int len) int i,val; val. max(a,len) printf ("testing array: for (i-0; iklen i++) printf("%s%d",i--0? a[i]) printf ("] "); if (val 0) printf(" FAIL: error code 2dIn", val); else printf(" maximum is n", val) int max (int a0, int len) Note: need len parameter as 'a' is just a pointer and so cannot check array size if (len 0) return -1 empty array int i if (a Co]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

=+Have they changed the way employees view IP?

Answered: 1 week ago