-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
70 lines (61 loc) · 3.25 KB
/
Copy pathmain.swift
File metadata and controls
70 lines (61 loc) · 3.25 KB
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
/*
Quinn Morrison
Compile Instructions:
1.) Make sure Swift is installed
Mac: https://www.swift.org/install/macos/
Windows: https://www.swift.org/install/windows/
2.) Run the compile command "swiftc main.swift -o Program" in your terminal in the folder that this file is located
3.) Run the program file we just created using the command line
Mac: "./Program"
Windows: "Program.exe"
No required dependencies
Output is static and in the console
*/
import Foundation //this is needed for foundation programming in swift (strings, arrays, etc)
func RecursiveMaxFind(to arrNums: Array<Int>, to intLen: Int) -> Int{
//Purpose: Find the max element of a given array
//Input: an array arrNums of size intLen
//Output: max element of array arrNums
print("\nCurrently evaluating the array at length: ", intLen)
if intLen == 1 {
//we've reached the end of the array, and can now start checking all elements, this is our base case
//the base case is where a recursive algorithm "breaks" or starts returning elements other than the function call
print("\nreached end of array")
return arrNums[0]
}
else {
//here, we send recursive calls to our function that break the array down into equal parts
//by decreasing the amount of array that we can access
//each of these equal parts is stored in an iteration of intMaxRest
print("\nAlgorithm is now going into a recursive call")
let intMaxRest: Int = RecursiveMaxFind(to: arrNums, to: intLen - 1)
//now, a we can find the highest num
//we first check if our intMaxRest is higher than the number stored in the array before it
//if yes, we send it on to the next iteration of the array to be checked
//this means we just send it to be checked against the next element in the array
//else (if no), we simply move on to the next element in the array
if intMaxRest > arrNums[intLen - 1]{
print("\nGreatest element at index ", intLen, ": ", intMaxRest)
return intMaxRest
}
else {
print("\nOur current element isn't bigger than the next one, moving on")
return arrNums[intLen - 1]
}
}
}
//comment block that explains this program to user
print("\nThis program uses an algorithm called a decrease and conquer algorithm")
print("It takes in user input and turns it into an array, which is then processed by the algorithm")
print("The algorithm uses recursion and the decrease and conquer method to find the highest number!")
print("\nPlease enter in numbers with space between them for our array: ")
//we only let the function continue if we can successfully read input
if let line: String = readLine() {
//we first take in the parameters as strings and then parse them into an array of integers
//this code is swift specfic in order to accomplish this, and does not hold any importance to our algorithm
let parts = line.split(separator: " ")
let arr = parts.compactMap{ Int($0) }
//this is where we call our algorithm
let highestNum: Int = RecursiveMaxFind(to: arr, to: arr.count)
print("\nthe highest num in the array is: ", highestNum)
}