-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathListFunctions.kt
66 lines (60 loc) · 1.57 KB
/
ListFunctions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fun getListOfNumbers(): List<Int> {
var myList = mutableListOf<Int>()
for (i in 1..7) {
println("Enter a number")
var numberInput = Integer.valueOf(readLine())
myList.add(numberInput)
}
return myList
}
fun findMax(myList: List<Int>): Int {
var largestNumber = myList[0]
for (i in myList) {
if (i > largestNumber) {
largestNumber = i
}
}
return largestNumber
}
fun findMin(myList: List<Int>): Int {
var smallestNumber = myList[0]
for (i in myList) {
if (i < smallestNumber) {
smallestNumber = i
}
}
return smallestNumber
}
fun findAverage(myList: List<Int>): Int {
var sum = 0
for (i in myList) {
sum += i
}
return sum / myList.size
}
fun checkForValue(myList: List<Int>, numToFind: Int): Boolean {
for (i in myList) {
if (i == numToFind) {
return true
}
}
return false
}
fun main() {
var values = getListOfNumbers()
println(values)
var largestValue = findMax(values)
println("The largest number is $largestValue.")
var smallestValue = findMin(values)
println("The smallest number is $smallestValue.")
var average = findAverage(values)
println("The average is $average.")
println("What number should I check for?")
var numToFind = Integer.valueOf(readLine())
var containsValue = checkForValue(values, numToFind)
if (containsValue) {
println("$numToFind exists in the list.")
} else {
println("$numToFind does not exist in the list.")
}
}