0

So I'm trying to store the average vote on images in an array (each value for each image is stored respectively.)

Here's what happens: The user votes, the average of the votes is shown to the user (from past votes), and then the user taps "next" to move onto the next movie to vote on.

How would I go about storing the value of each image so that it has its own average stored? I'm pretty new to swift...

Here's the function with which I'm calculating the average and the global variable above it which holds the array of the votes:

    var votes:[Double] = []

    func average(nums:[Double]) ->Double {    
    var total = 0.0
    for vote in nums {
        total += Double(vote)
    }
    let votesTotal = Double(nums.count)
    var average = total/votesTotal
    return average
}

I'm accessing the images through incrementing (only 10). This is in an UIButton normally.
The counter variable is outside of the scope of the IBAction where the counter increment is being performed.

var counter = 1

   (IBACTION)
   counter++
    if counter == 10 {

        counter = 1
    }

    imageOfPerson.image = UIImage(named:"image\(counter).jpg")

    resultLabel.text = ""

    enableButtons()
    }

Thanks a lot in advance.

6
  • does the no of images/movie are constant ? Commented Feb 3, 2015 at 19:29
  • I didn't understand. But yes, the array is a constant, for now. (Will probably add more in the future) I'm trying to save the average total votes to each image. Commented Feb 3, 2015 at 19:30
  • how are you actually calculating the average on any particular image? Commented Feb 3, 2015 at 19:32
  • I have a function that calculates it. Commented Feb 3, 2015 at 19:33
  • can you post your code.I am wondering how do you calculating the average.Add every user's rating / total no of user's ..That how are you calculating the average ? Commented Feb 3, 2015 at 19:35

2 Answers 2

1

Consider this code. To test this I pasted the following code into a class I have called AnimationObserver, which is instantiated with animationObserver:AnimationObserver = AnimationObserver();

struct imageVotes
{
    var imageNumber:Int = 0
    var vote:Double = 0.0
}

var votes:[imageVotes] = []
var averages:[Double] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
var imageNumber:Int = 0

var thisVote:Double = 0.0
{
    didSet
    {
        var imageVote:imageVotes = imageVotes()
        imageVote.imageNumber = imageNumber
        imageVote.vote = thisVote
        votes.append(imageVote)
        averages[imageNumber] = average(votes)
    }
}

func average(nums:[imageVotes]) ->Double
{
    var total:Double = 0.0
    var numVotes:Double = 0.0

    for var i = 0; i < nums.count; i++
    {
        if nums[i].imageNumber == imageNumber
        {
            total += nums[i].vote
            numVotes += 1.0
        }
    }
    var average = total/numVotes
    return average
}

If then from somewhere else in the code (where animationObserver is visible, probably an IBAction for you) I write:

    animationObserver.imageNumber = 0
    animationObserver.thisVote = 5.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")
    animationObserver.thisVote = 8.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")


    animationObserver.imageNumber = 1
    animationObserver.thisVote = 7.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")
    animationObserver.thisVote = 10.0
    println("Average Vote for imageNumber \(animationObserver.imageNumber) is \(animationObserver.averages[animationObserver.imageNumber)")

this prints:

Average Vote for imageNumber 0 is 5.0
Average Vote for imageNumber 0 is 6.5
Average Vote for imageNumber 1 is 7.0
Average Vote for imageNumber 1 is 8.5

Sign up to request clarification or add additional context in comments.

Comments

0

You could use a dictionary with the image filename as the key. For example:

var dictAverages = [String:Double]()
dictAverages["image\(counter).jpg"] =  average(nums) //nums is the data array corresponding to the counter

I would have helped more specifically but I am having difficulty understanding your code.

1 Comment

How would I append the votes and the average of all the total votes a movie has received to the dictionary? I created a function that defines a movie: 'func userProfile() { //creating a function that defines what each movie profile contains (putting it in a dictionary) //assuming that each profile will have a movie name, a UIImage, an Int and a Double //The UIImage is the Movie name, image#, the Int is the vote and the Double is the average var userProfiles = [Dictionary<String, UIImage,Int,Double>()] }' @Syed Tariq Thanks for the reply

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.