Question

In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the...

In racket, implement a tail-recursive function called sum-pairs that creates a new list by adding the elements of an input list in pairs. That is the first element of the resulting list is the sum of the first two elements of the input, the second element of the resulting list is the sum of the 3rd and 4th elements of the input, and so on. If there is an odd number of elements, then the last element remains unchanged. As an example, (sum-pairs ’(1 2 3 4 5)) will result in ’(3 7 5). It may be helpful to create helper functions.

RACKET!!!

Homework Answers

Answer #1

>Answer

>Given

#include <iostream>
#include <vector>
#define N 100
using namespace std;
  
vector<int> tail_recursive(vector<int> vec){
int l= vec.size();
vector<int> v2;
for(int i=0;i< l - 1;i+=2){
v2.push_back(vec[i] + vec[i+1]);
}
if(l%2 !=0) v2.push_back(vec[l - 1]);
return v2;
}

int main()
{

vector<int> vec, vec2;
  
for(int i=0;i<5;i++){
vec.push_back(i+1);
}
  
cout<<"Initial Value: ";
for(int i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
cout<<endl;
  
  
vec2 = tail_recursive(vec);
int l= vec2.size();
  
cout<<"Tail Recursive Value: ";
for(int i=0;i<l;i++){
cout<<vec2[i]<<" ";
}
  
cout<<endl;
return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...