Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the following functions in Scheme using fold-left and map. DO NOT use

ID: 3886848 • Letter: I

Question

Implement the following functions in Scheme using fold-left and map. DO NOT use recursive definition for this problem.

1.Define a function addOne, which takes a list of numbers and returns a list where each number in the input list is increased by one. For example, (addOne (1 2 3 4)) should return (2 3 4 5), and (addOne (2 4 6 8)) should return (3 5 7 9).

2. Define a function lstOR, which takes a list of Booleans and returns #f if and only if all of the Booleans are false. For example, (lstOR (#t #f)) should return #t, and (lstOR (#f #f)) should return #f. For your convenience, (lstOR ()) is defined as #f (functions and, or implement logical , in Scheme).

3. Define a function removeDup, which takes a list of ordered numbers, and returns a list that is identical to the input except that duplicated numbers are removed. For example, (removeDup (1 1 2)) should return (1 2); (removeDup (3 5 5 7 7 7 9)) should return (3 5 7 9).

Explanation / Answer

#include<iostream>
using namespace std;
#define SIZE 100
//To store user defined size
int n;
//Function to accept a list of integer and add one to each element
void addOne(int arr[])
{
//Loops till n
for(int c = 0; c < n; c++)
//Adds one to each element of the array
arr[c] = arr[c] + 1;
}//End of function

//Overloading function to accept integer data
void accept(int arr[])
{
//Accept the size of the array
cout<<" Enter how many numbers you want? ";
cin>>n;
//Accept data
cout<<" Enter the numbers: ";
//Loops till n
for(int c = 0; c < n; c++)
cin>>arr[c];
}//End of function

//Overloading function to accept boolean data
void accept(bool arr[])
{
//Accept the size of the array
cout<<" Enter how many numbers you want? ";
cin>>n;
//Accept data
cout<<" Enter the numbers: ";
//Loops till n
for(int c = 0; c < n; c++)
cin>>arr[c];
}//End of function

//Overloading function to display integer data
void display(int arr[])
{
//Loops till n
for(int c = 0; c < n; c++)
cout<<arr[c]<<" ";
}//End of function

//Overloading function to display boolean data
void display(bool arr[])
{
//Loops till n
for(int c = 0; c < n; c++)
cout<<arr[c]<<" ";
}//End of function

//Function to remove duplicates
void removeDup(int arr[])
{
//Loops till n
for(int i = 0; i < n; ++i)
{
//Loops from outer loop value plus one to n
for(int j = i + 1; j < n;)
{
//Checks if array i position value is equal to array j position value
if(arr[i] == arr[j])
{
//Loops from j to number of elements in array minus one
for(int k = j; k < n - 1; ++k)
//Assign array k plus one index position value to array k position
arr[k] = arr[k+1];
//Decrease the size of the array by one
--n;
}//End of if
//Otherwise
else
//Increase the j value by one
++j;
}//End of inner for loop
}//End of outer for loop
}//End of function

//Function to return #f if all the elements of the array is 0 otherwise return #t
string lstOR(bool arr[])
{
//Initialize flag to one
int flag = 1;
//Loops till n
for(int c = 0; c < n; c++)
{
//Checks whether the current index position value is not zero
if(arr[c] != 0)
{
//Set the flag value to 2 for value one in the array
flag = 2;
//Come out of the loop
break;
}//End of if
}//End of for loop
//Checks flag value is equal to one then all the elements of the array is zero
if(flag == 1)
return "#f";
//Otherwise one of the element or all the elements of the array is one
else
return "#t";
}//End of function

//Main method definition
int main()
{
//Creates an integer array
int number[SIZE];
//Creates an boolean array
bool data[SIZE];
cout<<" Operation add one ";
accept(number);
cout<<" Before add one: ";
display(number);
addOne(number);
cout<<" After add one: ";
display(number);

cout<<" Operation remove duplicate ";
accept(number);
cout<<" Before removing duplicates: ";
display(number);
removeDup(number);
cout<<" After removing duplicates: ";
display(number);

cout<<" Operation list or ";
cout<<" Enter either 0 or 1";
accept(data);
cout<<" Before list or: ";
display(data);
cout<<" After list or: "<<lstOR(data);
}//End of function main

Sample Run:


Operation add one

Enter how many numbers you want? 4

Enter the numbers:
12
25
65
2

Before add one: 12 25 65 2
After add one: 13 26 66 3

Operation remove duplicate

Enter how many numbers you want? 8

Enter the numbers:
1
5
1
6
5
4
3
5

Before removing duplicates: 1 5 1 6 5 4 3 5
After removing duplicates: 1 5 6 4 3

Operation list or

Enter either 0 or 1
Enter how many numbers you want? 3

Enter the numbers:
0
0
0

Before list or: 0 0 0
After list or: #f