Go array Calculation

Source: Internet
Author: User
Tags array length

package mainimport  "FMT" Func main ()  {   /*  Array length =  5 */    var  balance = []int {100, 1, 2, 7, 4}    var avg  float32   var sum int   /*  The array is passed as a parameter to the function  */   avg = getaverage ( balance, 5 )  ;    sum = getsum (balance,5);   /*  output returns the mean &NBSP;*/&NBSP;&NBSP;&NBSP;FMT. Printf (  "average:  %.2f \n", avg );       fmt. Println ("and is:%d", sum);} Func getaverage (Arr []int, size int)  float32 {   var i,sum  int   var avg float32     for i = 0;  i < size;i++ {      sum += arr[i]    }   Avg = float32 (sum / size)    return avg ;} /* and Calculation Formula */func getsum (arr []int,size int)  int{var i,sum intfor i =  0; i < size; i++{fmt. Println (Arr[i]) Sum += arr[i]}return sum}


This article from "Welcome comments, Welcome to like" blog, please be sure to keep this source http://swq499809608.blog.51cto.com/797714/1920886

Go array Calculation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.