Question: COMP 1 4 1 : Introduction to Programming Languages Instructions: In this lab, we are going to try functional programming in C + + .

COMP 141: Introduction to Programming Languages
Instructions: In this lab, we are going to try functional programming in C++. Rewrite the following C++
code fragments. Your modified code should obey the following rules:
No global variables
No pass-by-reference and pass-by-pointer parameters (except arrays)
No iteration
1 Fibonacci
int fib (int n){
if (n <=1) return 1;
int x =1;
int y =1;
int t;
for (int i =2; i < n; i++){
t = x;
x = x + y;
y = t;
}
return x;
}
2 Min and max in an array
void minmaxFromArray(long _array[], short _array_length, long& _min, long& _max){
short i;
long min =_array[0];
long max =_array[1];
for (i=1; i<_array_length; i++){
if (_array[i]< min) min =_array[i];
if (_array[i]> max) max =_array[i];
}
_min = min;
_max = max;
}
3 Inserting an entry to a dictionary
vector > dictionary;
void setDictionaryEntry(string _key, string _value){
unsigned short i;
for (i=0; i(_key, _value));
}
1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!