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

work4.pdf Homework #4 (8 points) 1. Complete sum.cpp using an iterative approach

ID: 3799331 • Letter: W

Question

work4.pdf Homework #4 (8 points) 1. Complete sum.cpp using an iterative approach and a corresponding recursive solution 2. Login to linux, notice the new directory called sum, type in the following commands: ls -ald sum cd sum s -al 3. After the last command you should see a file named sum.cpp 4. Implement the methods described sum.cpp. 5. Complete the Kinsert portion of the comments that describes the characteristics of the summation problem that allows for a recursive solution 6. Complete the test drive main() portion of sum.cpp that tests your methods 7. Compile sum.cpp with the following command: g sum.cpp 8. Run your application: la out Note: Homework 4 demonstrates students' ability to: Complete a test driver program to method specs. Implement an iterative solution. Identify the properties of an algorithm that allows for a recursive solution. Implement a recursive solution. ASK QUESTIONS IN LECTURE

Explanation / Answer

Find the Source here :- http://pastebin.com/TYir5Vcz

I am also pasting my solution Below :-

#include<iostream>
using namespace std;


int sumIterative(const int a[], int n)
{
   int sum = 0;
   for(int i=0; i<n; i++)
   {
       sum += a[i];
   }
   return sum;
}

int sumRecursiveUtil(const int a[], int idx, int n)
{
   if (idx == n)
       return 0;
   return a[idx] + sumRecursiveUtil(a, idx+1, n);
}

int sumRecursive(const int a[], int n)
{
   return sumRecursiveUtil(a, 0, n);
}

int main()
{
   int n;

   int sumArray[] = {1, 2, 3, 4, 12, -22};

   n = sizeof(sumArray)/sizeof(sumArray[0]);

   int sumIter = sumIterative(sumArray, n);

   int sumRecur = sumRecursive(sumArray, n);


   cout << "Sum Iterative : "<<sumIter<<endl;

   cout << "Sum Recursive : "<<sumRecur<<endl;


   //Custom User Input
  
   cout<<"Provide Array Input"<<endl<<"Enter number of elements :"<<endl;
   cin>>n;
   int userArray[n];
   cout<<"Provide Array Elements"<<endl;
   for(int i=0; i<n; i++)
   {
       cin>>userArray[i];
   }

   sumIter = sumIterative(userArray, n);

   sumRecur = sumRecursive(userArray, n);

   cout << "Sum Iterative : "<<sumIter<<endl;

   cout << "Sum Recursive : "<<sumRecur<<endl;

   return 0;

}