Problem: Problem Statement
Problem 1: Find the maximum element in the following finite sequence
of integers, L={7,−3,0,4,10,2}.
Pseudocode
Algorithm FindMax(L)
## Input: A list L of n integers
## Output: The maximum element in the list
## max = L[1]
## for i = 2 to length(L) do
## if L[i] > max then
## max = L[i]
## return max
1) Explanation of Pseudocode (Fill what is missing for 10
points)
1. Initialize max with the first element of the list: Assume the
first element is the largest for now.
2. Loop through the rest of the elements in the list: Start from the
second element and go to the end.
3. Compare each element with max: If the current element is greater
than max, update max.
4. Return max: -> After the loop ends, the maximum element is the
result.
2) Example Execution (Completely fill what is missing for 10
points)
Let’s go through the list using our Pseudocode L={7,−3,0,4,10,2}
step-by-step:
1. Initialize max with the first element: max = 7
2. Compare -3 with max: -3 < 7 (no change)
3. Compare 0 with max: 0 < 7 (no change)
4. Compare 4 with max: 4 < 7 (no change)
5. Compare 10 with max: 10 > 7 (update max = 10)
6. Compare 2 with max: 2 < 10 (no change)
The final value of max is 10.
write the code for 70 points.
## R code from Pseudocode. Use L={7,−3,0,4,10,2} for this HW assignment
# Initialize the list
L <- c(7, -3, 0, 4, 10, 2)
# Initialize max with the first element
max <- L[1] # sets the first value of the L list as the max, which is 7.
# Loop through the rest of the elements in the list
# note that 'i' is the index of the entry of the place value of domain.
for (i in 2:length(L)) {
if (L[i] > max) {
max <- L[i] # updates the max when detected a value greater than 7.
}
}
# Return the maximum element
max
## [1] 10
Note: Professor reserves the right to decide what answers, code, and
step-processs is correct not the student. Once the student submits the
assignment, they are not able to resubmit for a higher grade and all
grades are final when the professor inserts them in D2L.
Convert to HTML for 10 points and submit both the markdown file
(.rmd) and HTML to the assignment folder in D2L.