Write a small program in the ‘Go’ language that implements a simple linear searc
ID: 3738581 • Letter: W
Question
Write a small program in the ‘Go’ language that implements a simple linear search function and tests it. Two example implementations in C and JavaScript are provided. Your solution should mimic these programs, and produce very similar output.1 In particular: • Implement 3 functions: main, test, and search. (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 test array (values 5,3,9,11,71) and test strategy (try to find each value in the array, and one value that is not in the array). • Note that the C version passes an explicit array length value to test() and search(), while the JavaScript does not. This is because JavaScript can check array sizes while C can’t. Go, like JavaScript, can check array sizes, so you will not need the explicit size parameter to these two functions. Your Go search function should be different from the two provided examples on one key aspect: it must return two values. The C/JavaScript search function returns a single integer that is the index of the located value; if the value is not found then -1 is returned. This is ‘bad’ programming as the single return value has two meanings: a ‘value found’ indicator and an array index. Go (like Haskell) can return multiple values. You should use a Go function declaration like func search(a []int, val int) (found bool, key int) that returns a found value (true or false) as well as the index (key) value. If the index is not found you can return any value for key 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, so don’t 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)
C example2
#include <stdio.h>
void test(int a[], int len, int v); // forward declaration
int search(int a[], int len, int val); // forward declaration
void main() {
int i;
int a[] = {5,3,9,11,71};
int len = sizeof(a)/sizeof(int);
printf("array a: ");
for (i=0; i<len; i++) printf("%s%d",i==0?"":",",a[i]);
printf(" ");
for (i=0; i<len; i++) test(a,len,a[i]);
test(a,len,1);
}
void test(int a[], int len, int v) {
int key;
key = search(a,len,v);
if (key < 0) {
printf("value %d not found ", v);
} else {
printf("value %d found at key=%d ", v, key);
}
}
int search(int a[], int len, int val) {
// Note: need len parameter as ’a’ is just a pointer
// and so cannot check array size within this array
int maxkey = len -1;
int i = 0;
while(i <= maxkey) {
if (a[i] == val) return i;
i++;
}
return -1 ;
}
JavaScript example3
<html>
<body>
<script>
var a = new Array(5,3,9,11,71);
document.write("array a: " + a + "<br> ");
for (i=0; i < a.length; i++){
test(a,a[i]);
}
test(a,1);
function test(a,v) {
var key;
key = search(a,v);
if (key < 0) {
document.write("value " + v + " not found<br/> ");
} else {
document.write("value " + v + " found at key=" + key + "<br/> ");
}
}
function search(a,val) {
var maxkey = a.length -1;
var i = 0;
while(i <= maxkey) {
if (a[i] == val) return i;
i++;
}
return -1 ;
}
</script>
</body>
</html>
Explanation / Answer
package main
import (
"fmt"
)
func main() {
a := []int{5, 3, 9, 11, 71}
fmt.Print("array a: ")
for i := 0; i < len(a); i++ {
if i == 0 {
fmt.Print(a[i])
} else {
fmt.Print(",", a[i])
}
}
fmt.Println()
for i := 0; i < len(a); i++ {
test(a, a[i])
}
test(a, 1)
}
func test(a []int, val int) {
found, key := search(a, val)
if found {
fmt.Println("value ", val, " found at key=", key)
} else {
fmt.Println("value ", val, " not found")
}
}
func search(a []int, val int) (bool, int) {
maxkey := len(a) - 1
i := 0
for i <= maxkey {
if a[i] == val {
return true, i
}
i++
}
return false, -1
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.