-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session 3 (2.1): Trying Functions
142 lines (96 loc) · 3.17 KB
/
Session 3 (2.1): Trying Functions
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
###### Problem 1 starts here
print("Problem 1")
# myLength() function
myLength <- function(a)
{
length(a)
}
test_vector1 = c(7, 8, 9, 10, 11, 12, 100)
y1 = myLength(test_vector1)
print(y1) #should return 7 for test_vector1, is what occurs.
# Be sure to test it with vectors of known length!
######## Problem 2 starts here ######
print("Problem 2")
# myMean() function
myMean <- function(b)
{
sum = 0
for (value in b)
{sum = sum + value}
average = sum / length(b)
}
test_vector2 = c(70, 80, 9, 10, 11, 12, 100)
y2 = myMean(test_vector2)
print(y2)
######## Problem 3 starts here ######
print("Problem 3")
# The following is test code for me to test your myMean function.
# You can comment it out while you're writing your code, but make sure you
# leave it intact in your final submission.
# This should output the correct mean of TestDataSet.
TestDataSet <- c(200, 3.5, 4.00567, 82, 0, 900, 3.14159, 2.72)
myVariance = function(vector)
{
n = length(vector)
mean = myMean(vector)
index = 1:n
sum = 0
for(position in index) #changed from index in vector
{
y = (vector[position] - mean)^2 #may need to iterate over this
sum = sum + y
}
variance = (sum) / (n-1) #changed the square to up above, at y
return(variance)
}
# Test code for grading
print(myVariance(TestDataSet)) #should print 96879.53
######## Problem 4 starts here ########
print("Problem 4")
# declare your myMin() function here
# make sure you call it "myMin"
myMin <- function(vector) ####ALSO FIND A WAY TO PRINT POSITION OF THE VECTOR####
{
min = Inf # Needed to pre-define. highest possible value, should be replaced with any value in TestDataSet
length_of_vector = length(vector)
for (n in length_of_vector)
{
value = vector[n]
if (value < min) #as we go through this vector, if value is lower than min, set value to the new minimum
{min = value}
}
return(min)
}
TestDataSet = c(55, 999, 10, 2, 3, 1)
# Test code for grading
print(myMin(TestDataSet)) #should print 0 5
######## Problem 5 starts here ########
print("Problem 5")
# declare your myMax() function here
# make sure you call it "myMax"
myMax <- function(vector) ####Issues with obtaining maximum -- still fetches minimum value.####
{
max = 0 # Needed to pre-define. lowest possible value, should be replaced with any value in TestDataSet
length_of_vector = length(vector)
for (n in length_of_vector)
{
value = vector[n]
if (value > max) #as we go through this vector, if value is lower than min, set value to the new minimum
{max = value}
}
return(max)
}
TestDataSet = c(55, 999, 10, 2, 3, 1)
# Test code for grading
print(myMax(TestDataSet)) #should print 900 6
########## Problem 6 starts here ######
print("Problem 6")
# Declare your mySecondSmallest() function here
# Test code
print(mySecondSmallest(TestDataSet)) #should print 2.72 8
######### Problem 7 starts here ######
print("Problem 7")
# Declare your functions here
TestDataSet_withRepeats <- c(200, 3.5, 4.00567, 82, 0, 900, 3.14159, 2.72,0,0,2.72,4)
print(myMin_withRepeats(TestDataSet_withRepeats)) #should print 0 5 9 10
print(mySecondSmallest_withRepeats(TestDataSet_withRepeats)) #should print 2.72 8 11