-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomMathFunctions_Lists.kt
78 lines (67 loc) · 1.95 KB
/
CustomMathFunctions_Lists.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
67
68
69
70
71
72
73
74
75
76
77
// Write your code below
//GET NUMBERS LIST - create a function that takes in a list of numbers
fun getListOfNumbers(): List<Int>{
var myList = mutableListOf<Int>()
for(i in 1..7){
println("Please enter a number:")
val userInput = Integer.valueOf(readLine())
myList.add(userInput)
}
return myList
}
//FIND MAX. - create a function that takes in a list of integers, and then returns the max value
fun findMax(myList: List<Int>):Int{
var largestNumber = myList[0]
for(i in myList){
if(i > largestNumber){
largestNumber = i
}
}
return largestNumber
}
//FIND MIN. - create a function that takes in a list of integers, and then returns the min value
fun findMin(myList: List<Int>):Int{
var smallestNumber = myList[0]
for(i in myList){
if(i < smallestNumber){
smallestNumber = i
}
}
return smallestNumber
}
//FIND AVERAGE - create a function that takes in a list of integers, finds the average, and returns that value
fun findAverage(myList: List<Int>): Int{
var sum = 0
for(i in myList){
sum += i
}
return sum / myList.size
}
//CHECK FOR NUMBER IN LIST - create a function that reviews a list and checks whether a specified number is in a given list
fun checkIfListContains(myList: List<Int>, num1:Int): Boolean{
for(i in myList){
if(i == num1){
return true
}
}
return false
}
fun main() {
// Write more code below
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("Please enter a number: ")
val numToFind = Integer.valueOf(readLine())
val containsValue = checkIfListContains(values, numToFind)
if(containsValue){
println("$numToFind exists within the list.")
}else{
println("$numToFind not found in list.")
}
}