Sum and product of vectors

Report a typo

Wow! This problem is kind of tricky; it requires some knowledge about for...range loops. If you're ready to put your thinking cap on, brace yourself, and good luck! Otherwise, you can skip it for now and return any time later.

A vector is a slice that is filled with numbers. In this task, you are going to work with integers.

Your task is to write the additional required code to create two functions that interact with a vector:

  • getSumOfVectorElements() int { ... } — that returns the sum of all elements in the vector.
  • getProductOfVectorElements() int { ... } — that returns the product of all elements in the vector.
Write a program in Go
package main

import "fmt"

// getSumOfVectorElements() returns the sum of all elements in the vector
func getSumOfVectorElements(vector []int) int {
var sum = 0

for _, ? := range vector {
sum += ?
}
return sum
}

// getProductOfVectorElements() returns the product of all elements in the vector
func getProductOfVectorElements(vector []int) int {
var product = 1

for _, ? := range ? {
product *= ?
}
return product
}

// DO NOT delete or modify the contents within the main function!
func main() {
var vc []int = []int{1, 2, 3, 4, 5, 6}

var sum, prod int

sum = getSumOfVectorElements(vc)
prod = getProductOfVectorElements(vc)

fmt.Println(sum, prod)
}
___

Create a free account to access the full topic