Question-5 [5 marks] Consider the definition of general summation of numbers bet
ID: 3589837 • Letter: Q
Question
Question-5 [5 marks] Consider the definition of general summation of numbers between two values as describe in lecture: (define (sum term a next b) (if (> a b) ( (term a) (sum term (next a) next b)))) Given helper fiunctions inc and identity one can, for example, define a function that sums all integers from a to b: (define (inc x) (+ x 1)) (define (identity x) x) (define (sum-integers a b) (sum identity a inc b)) The general sum procedure above generates a linear recursive process. Rewrite it as an iterative process.Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int inc(int x)
{
return x+1;
}
int identity(int x)
{
return x;
}
int sum_integers(int a, int b)
{
int sum=0;
while(a<=b)
{
sum=sum+identity(a);
a=inc(a);
}
return sum;
}
int main()
{
int a,b,sum=0;
cout<<"Enter range 'a' and 'b' : "<<endl;
cin>>a>>b;
sum=sum_integers(a,b);
cout<<"Sum = "<<sum<<endl;
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.