Home > Back-end >  How to Calculate the average of Array Kotlin Android
How to Calculate the average of Array Kotlin Android

Time:11-14

I am very new to Kotlin and Android. I am trying to calculate the average of the values in my Array in the function "array" but i'm not very sure how to do it. I have created a function called "average" which I want to calculate the average of the array within. Any help would be very appreciated. Below is all of my source code.

class MainActivity2 : AppCompatActivity() {
    private lateinit var addnumber: EditText
    private lateinit var storednumber: TextView
    private lateinit var output: TextView
    private lateinit var addbutton: Button
    private lateinit var clearbutton: Button
    private lateinit var averagebutton: Button
    private lateinit var minmaxbutton: Button


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        addnumber = findViewById(R.id.et_addNum)
        storednumber = findViewById(R.id.stored_tv)
        output = findViewById(R.id.answer2_tv)
        addbutton = findViewById(R.id.addNum_btn)
        clearbutton = findViewById(R.id.clear_btn)
        averagebutton = findViewById(R.id.average_btn)
        minmaxbutton = findViewById(R.id.minMax_btn)


        addbutton.setOnClickListener {
            array()

        }

        clearbutton.setOnClickListener {
            clear()

        }

        clearbutton.setOnClickListener {
            average()

        } 
    }
    private fun array() {
        val ed = findViewById<View>(R.id.et_addNum) as EditText
        var text = ed.text.toString()
        val array: List<String> = text.split(",")
        
        for (element in array) {
            Log.e("elements", element)

            storednumber.setText(array.toString())
        }
    }

    
    private fun clear() {
        storednumber.setText(null)
    }

    
    private fun average() {
        
    }
} 

CodePudding user response:

As you're using Kotlin, you should consider using the Extension function, average(), like below:

arrayOf(4, 34, 5, 44).average()

OR

You can create your custom average() method, like below:

fun average(arr: Array<Int>): Double {
        return if (arr.isNullOrEmpty()) 0.0
        else {
            var sum = 0.0
            for (i in arr) {
                sum  = i
            }
            sum / arr.size
        }
    }

CodePudding user response:

Here is your answer:

    private fun array() {
        val ed = findViewById<View>(R.id.et_addNum) as EditText
        var text = ed.text.toString()
        val array: List<String> = text.split(",")
        var intArray = mutableListOf<Int>()
        for (element in array) {
            Log.e("elements", element)
            intArray.add(element.toInt())
        }
        average(intArray)
    }
    
    private fun average(list: List<Int>) {
        val myAverage = list.average() // here is the average you can use it.
        Log.e("average", myAverage.toString()) // here we print it to log
    }
  • Related