1.1P - Preparing For OOP Answer Sheet Ver Final
1.1P - Preparing For OOP Answer Sheet Ver Final
1.1P - Preparing For OOP Answer Sheet Ver Final
b) pwd:
c) mkdir:
d) cat:
e) ls:
2. Aside from the examples already provided in question 1, consider the following list and
find an example of information that could be stored as:
6 6 integer
1+2*3 7 integer
3 integer
a+b a=1
b=2
12.5 float
a*2+b a = 2.5
b=3
15 integer
a+2*b a = 2.5
b=3
9 integer
(a + b) * c a=1
b=2
c=3
Fred Flinstone string
“Fred” + “ Flintstone”
2. Explain the concept parameter. Write some code that demonstrates a simple of use of
a parameter. You should show a procedure or function that uses a parameter, and
how you would call that procedure or function.
def greet(name)
puts "Hello, #{name}!"
end
greet("BOB")
3. Using code examples, describe the term scope as it is being used in programming (not
in business or project management). In your explanation, focus on procedural
programming. Ensure that you cover as many kinds of scopes and their respective
differences as possible. Your answer must detail at least two kinds of scopes.
Global:
$cow = "moo"
Local:
def animal
dog = "woof"
end
Section E: Programming Practice:
1. Using procedural style programming, in any language you like, write a function called
Average, which accepts an array of integers and returns the average of those integers.
Do not use any libraries for calculating the average: we want to see your understanding
of algorithms. You must demonstrate appropriate use of parameters, returning and
assigning values, and the use of control statements like a loop. Note — just write the
function Average at this point. In the next question we will ask you to invoke the
Average function. You are not required to develop a complete program or even specify
code that outputs anything at this stage. Average is a pure function. Input/output and
any business logic processing is the responsibility of the (main line) calling the function
Average.
Insert your code here:
def Average(numbers)
sum = 0
numbers.each do |num|
sum += num
end
2. Using the same language, write a main function you would to set up data (i.e., declare
an array and initialize it with proper values), call the Average function, and print out the
result. You are not required to provide input processing logic; you can have an inline
instantiate of the collection of data values that the Average function needs to calculate
the average of. Please use a reasonable data set, that is, the array must contain at
least five elements of numerical type.
Insert your code here:
def main ()
data = [10, 20, 30, 40, 50]
result = Average(data)
puts "The average is: #{result}"
end
main()
3. Again using the same language, extend the main function with some output
statements. Print the message “Double digits” if the average is above or equal to 10.
Otherwise, print the message “Single digits”. And then, if the average is negative (e.g.,
the average of a week’s temperature readings at the Australian base in the Antarctic),
add an additional line of output highlighting that the “Average value is in the negative”.
Provide screenshot(s) of your program running, that is, the code and the run time
output.
Insert your code here:
if result >= 10
puts "Double digits"
if result < 0
puts "Average value is in the negative"
else
puts "Single digits"
end
end
Insert your whole program here:
def Average(numbers)
return nil if numbers.empty?
sum = 0
numbers.each do |num|
sum += num
end
def main
data = [1, 2, 3, 4, 5]
result = Average(data)
main
End of Task
All students have access to the Adobe Acrobat tools. Please print your solution to PDF and
submit via Canvas.