In: Computer Science
#include <string>
using namespace std;
//using recursion no loops allowed
int main()
{
double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };
//Challenge #2: print the list from given range
printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6
cout << endl;
//Challenge #3: print the list, but backwards
printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
cout << endl;
//Challenge #4: reverse order of items in list
reverse(nums, 0, 12);
printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
}
c++
#include <iostream>
#include <string>
using namespace std;
//using recursion no loops allowed
// this function prints the elements of array in forward direction
void printList(double nums[],int st,int en)
{
for(int i=st;i<=en;i++)
cout<<nums[i]<<" ";
cout<<"\n";
}
// this function prints the elements of array in reverse direction
void printReverse(double nums[],int st,int en)
{
for(int i=en;i>=st;i--)
cout<<nums[i]<<" ";
cout<<"\n";
}
// this function reverses the array
void reverse(double nums[],int st,int en)
{
// we swap all the elements from begin to mid position with the elements at the same position from the end
int mid=(st+en)/2;
for(int i=st;i<=mid;i++)
{
swap(nums[i],nums[en-i]);
}
}
int main()
{
double nums[] = { 13.8, 2.14, 51, 82, 3.14, 1.7, 4.89, 18, 5, 23.6, 17, 48, 5.6 };
//Challenge #2: print the list from given range
printList(nums, 0, 12); //13.8 2.14 51 .... 48 5.6
cout << endl;
//Challenge #3: print the list, but backwards
printReverse(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
cout << endl;
//Challenge #4: reverse order of items in list
reverse(nums, 0, 12);
printList(nums, 0, 12); //5.6 48 17 ... 2.14 13.8
}
PLEASE LIKE THE SOLUTION :))
IF YOU HAVE ANY DOUBTS PLEASE MENTION IN THE COMMENT