2015-02-02 18 views
13

Voglio che la mia funzione calcoli la media della mia matrice di tipo Double. L'array è chiamato "voti". Per ora, ho 10 numeri.Rendere la mia funzione calcolare la media dell'array Swift

Quando chiamo lo average function per ottenere la media dei voti dell'array, non funziona.

Ecco il mio codice:

var votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

func average(nums: Double...) -> Double { 
    var total = 0.0 

    for vote in votes { 
    total += vote 
    } 
    let votesTotal = Double(votes.count) 
    var average = total/votesTotal 
    return average 
    } 

average[votes] 

Come chiamare la media qui per ottenere la media?

+0

medi (voti). Dichiaralo come medio (num: [Double]) e assicurati che l'array di voti sia [Double]. Al momento è un [Int] – MirekE

risposta

60

si dovrebbe utilizzare il metodo di ridurre() per sommare l'array come segue:

Xcode 9 • Swift 4

extension Array where Element: Numeric { 
    /// Returns the total sum of all elements in the array 
    var total: Element { return reduce(0, +) } 
} 

extension Array where Element: BinaryInteger { 
    /// Returns the average of all elements in the array 
    var average: Double { 
     return isEmpty ? 0 : Double(Int(total))/Double(count) 
    } 
} 

extension Array where Element: FloatingPoint { 
    /// Returns the average of all elements in the array 
    var average: Element { 
     return isEmpty ? 0 : total/Element(count) 
    } 
} 

let votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
let votesTotal = votes.total  // 55 
let votesAverage = votes.average // "5.5" 

Xcode 8.3.2 • Swift 3,1

extension Array where Element == Int { 
    /// Returns the sum of all elements in the array 
    var total: Element { 
     return reduce(0, +) 
    } 
    /// Returns the average of all elements in the array 
    var average: Double { 
     return isEmpty ? 0 : Double(reduce(0, +))/Double(count) 
    } 
} 

extension Array where Element: FloatingPoint { 
    /// Returns the sum of all elements in the array 
    var total: Element { 
     return reduce(0, +) 
    } 
    /// Returns the average of all elements in the array 
    var average: Element { 
     return isEmpty ? 0 : total/Element(count) 
    } 
} 
+1

http://stackoverflow.com/a/24004899/1187415. –

+1

In Swift 2 ottengo il valore di tipo '[Double]' non ha membro 'IntegerLiteralType' – Burf2000

+0

L'utilizzo di total.hashValue non è l'idea migliore .... Usa il totale non è – user3441734

7

Hai alcuni errori nel codice:

//You have to set the array-type to Double. Because otherwise Swift thinks that you need an Int-array 
var votes:[Double] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

func average(nums: [Double]) -> Double { 

    var total = 0.0 
    //use the parameter-array instead of the global variable votes 
    for vote in nums{ 

     total += Double(vote) 

    } 

    let votesTotal = Double(nums.count) 
    var average = total/votesTotal 
    return average 
} 

var theAverage = average(votes) 
Problemi correlati