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

Write a C++ main program that calls each function listed below. Load values into

ID: 673999 • Letter: W

Question

Write a C++ main program that calls each function listed below. Load values into each array using a list to test each function. Print out results to show original array contents and the result of the contents of the array after using the functions. All printing should be done in the main programs – do not print from the functions.  

A)Include a function to find the sum of the elements of an array.

Form of function call: Sum = ArraySum(A, Size)

B) Include a function to swap the contents of two equal size arrays named A and B.

Form of function call: ArraySwap(A, B, Size)

C)Include a function to reverse the contents of an array. For example, an array that originally contained the elements 1,2,3,4 would contain 4,3,2,1 after the funCTION CALL.

Form of function call: ArrayReverse(A, Size)

Example output: The output should clearly show original array contents, new array contents (if modified), and the result. For example, the output for parts B, C, and A might look as follows:

         Contents of array A before swap:     1     2     3      4      5     6    7

         Contents of array B before swap:     8     9     10    11    12   13   14

         Contents of array A after swap:        8     9     10    11    12   13   14

         Contents of array B after swap:        1     2     3      4      5     6    7

         Contents of array C before reverse: 3     4     5      6      7     8    9

         Contents of array C after reverse:     9     8     7     6     5      4     3

         Sum of array C: 42

Explanation / Answer

#include<iostream>
using namespace std;
int ArraySum( int *arr, int n ) {
if (n < 0) {
return 0;
} else{
return arr[n] + ArraySum(arr, n-1);
}
}
void ArraySwap(int *a,int *b, int size)
{
int temp,i;
for(i=0;i<=size;i++)
{
   temp=a[i];
   a[i]=b[i];
   b[i]=temp;
   }  
}
void ArrayReverse(int *a, int size)
{
   int j = size+1;   
int i = 0,temp;   

while (i<=j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;   
j--;
}
}
int main()
{
   int a[7],b[7],c[7],s=0;
   cout<<"enter the elements for array a : ";
   for(int i=1;i<8;i++)
   cin>>a[i];
   cout<<"enter the elements for array b : ";
   for(int i=1;i<8;i++)
   cin>>b[i];
   cout<<"enter the elements for array c : ";
   for(int i=1;i<8;i++)
   cin>>c[i];
  
   cout<<" Contents of array A before swap: ";
   for(int i=1;i<8;i++)
   cout<<" "<<a[i];
   cout<<" Contents of array B before swap:";
   for(int i=1;i<8;i++)
   cout<<" "<<b[i];
  
   ArraySwap(a, b, 7);
  
   cout<<" Contents of array A after swap: ";
   for(int i=1;i<8;i++)
   cout<<" "<<a[i];
   cout<<" Contents of array B after swap: ";
   for(int i=1;i<8;i++)
   cout<<" "<<b[i];
  
   cout<<" Contents of array C before reverse: ";
   for(int i=1;i<8;i++)
   cout<<" "<<c[i];
  
   ArrayReverse(c, 7);
  
   cout<<" Contents of array C after reverse: ";
   for(int i=1;i<8;i++)
   cout<<" "<<c[i];
  
   s=ArraySum(a, 7);
   cout<<" Sum of array A is: "<<s;  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote