In: Computer Science
Flow control is one of the main building blocks that determines
how a program should run.
Ghana Cocoa Growing Company wishes to compare the impart of two
newly acquired
fertilizers on cocoa. They will do this by applying the same
quantity of the two fertilizers on
two similar but different cocoa plant under the same conditions
(humidity, sunlight, soil
moisture etc.). They will then choose the fertilizer whose crop
produces the best harvest
(highest quantity per square feet). As a programmer,
a. Suggest a flow control method that would be used to do the
comparison and explain
the reason why you chose it.
b. Write a flow control statement that would do the comparison in
“a” above. (5
marks)
c. The Ghana Statistical Services (GSS) is interested in your
statement in “b” above but
it is of the view that it is too specific. They want you to modify
it by creating a
function that takes four arguments i.e. (the names of the two
fertilizer and their
individual yields). The function should return the name of the
fertilizer with the
greatest yield.
a) The flow control method that will be most suitable for this case will be if flow control statement.
The reason why I choose is because the aim of the program is to get the most worthy fertilizer for crops , and so good fertilizer produces more yield.
Here the if flow control method can be used to compare the harvest produced by the two fertilizers and return the fertilizer name which produced more yield.
b)Let us first initialize the variables:
fertilzer1="name_of_fertilizer_1" //it will be a string
harvest1="yield generated by fertilizer1" //It will be an integer value
fertilzer2="name_of_fertilizer_2" //it will be a string
harvest2="yield generated by fertilizer2" //It will be an integer value
Now the if condition:
if(harvest1>harvest2)
then print the variable fertilizer1
else
print the variable fertilizer2
c)The method looks like this:
char function_name(fertilizer1,fertilizer2,harvest1,harvest2){
if(harvest1>harvest2){
return fertilizer1;}
else{
return fertilizer2;}
}
In this function it accepts four parameters and using the comparison of values of harvest, the required and best fertilizer name is returned.
Hope this helps!!!