C. Instructions Code the following functions in a single lisp file named homewor
ID: 3752943 • Letter: C
Question
C. Instructions
Code the following functions in a single lisp file named homework.lisp
1) In a comment, describe what the following code do when it is executed; also, provide an equivalent method/function foo in Java or C/C++
2) In a comment, describe what the following code do when it is executed; also, provide an equivalent method/function myPrint in Java or C/C++
3) Write an iterative function that calculate the factorial of a positive integer and return the result. The function should be equivalent to this Java code.
public static double factorial1 (double n) { double sum = 1;
return sum; }
4) Write a recursive function that calculate the factorial of a positive integer and return the result. The function should be equivalent to this Java code.
public static double factorial2 (double n) { if (n<=1)
return n * factorial2 (n-1);
}
5) Write a function, named amount, that takes a list and returns the number of times thesymbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
6) Write a recursive function that returns the Fibonacci number where the Fibonacci sequence is defined as
7) Write a function, named small, that takes 2 numbers and returns the smaller of the two.
8) Write a function, named sum, that takes an integer n and returns the summation of n and all
positive integers preceding it; e.g., passing 5 will return 15, which is 1+2+3+4+5.
9) In a comment, describe what the following code do when it is executed; explain each line and whether the result is a symbol or a list
10) Define a function test that runs all of the above functions (items 4, 5, 6, 7, 8) as follows.Replace “PUT YOUR NAME HERE” for your name.
Explanation / Answer
#include<iostream>
using namespace std;
//4
double factorial1(double n)
{
//that finds factorial of n
//and returns the result
int i=1;
double p=1;
for(i=1;i<=n;i++)//iteration
p=p*i;
return p;
}
//6
double factorial2(double n){
//finds factorial of n
//and returns the result
if(n==1)
return 1;
return n*factorial2(n-1);//recursive call
}
//7
int fibonacci(int n)//returns nth fibonacci number
{
if(n==0)
return 0;
if(n==1)
return 1;
return fibonacci(n-2)+fibonacci(n-1);//recursive call
}
//8
int small(int a,int b)
{
if(a<b)
return a;//returns the small number
return b;
}
//9
int sum(int n)//finds sum of n numbers
{
int Sum =0;
int i=1;
for(i=1;i<=n;i++)
Sum=Sum+i;//finding sum
return Sum;
}
int main()
{
//testing above methods
cout<<"Factorial of 4:"<<factorial1(4)<<endl;
cout<<"Factorial of 4:"<<factorial2(4)<<endl;
cout<<"3rd fibonacci number:"<<fibonacci(3)<<endl;
cout<<"Small of 2,3 :"<<small(2,3)<<endl;
cout<<"Sum of 5 numbers:"<<sum(5);
return 0;
}
output:
Factorial of 4:24
Factorial of 4:24
3rd fibonacci number:2
Small of 2,3 :2
Sum of 5 numbers:15
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.