Write a C++ main program that calls each function listed below. Load values into
ID: 3708881 • 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 contents after using the functions. All printing should be done in the main programs do not print from the functions. Turn in copies of the program and all results. Output: The output should clearly show original array contents, new array contents (if modified), and the result. For example, the output for part B 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 A) Include a function to find the length of a vector stored in an array where the length of an N-dimensional vector L is: Length = sqrt L 0^2 + L 1^2 + L 2^ + L n-1^2 B) Form of function call: Result = Length(A, Size) B) Include a function to swap the contents of two equal size arrays named A and B. Form of function call: Swap(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: Reverse(A, Size)
Explanation / Answer
Hi Dear,
Please find my implementation.
######### Part A #########
#include<iostream>
#include<math.h>
using namespace std;
double lengthVector(double[], int);
int main()
{
int size=5;
double vector[]={1.0,2.0,3.0,4.0,5.0};
double length=lengthVector(vector,size);
cout<<"Elements of vector, V : "<<endl;
for(int index=0;index<size;index++)
cout<<vector[index]<<" ";
//print the length of vector
cout<<" Legnth of the vector,V="<<length<<endl;
system("pause");
return 0;
}
//The method lengthVector that takes a double array and
size of the array
//and returns the lenght of the vector
double lengthVector(double vectorElements[],int size)
{
int index;
double length=0;
for(index=0;index<size;index++)
{
//find square root of each element and
add to the length
length+=sqrt(vectorElements[index]);
}
//return legnth
return length;
}
######### Part B #########
#include<iostream>
#include<math.h>
using namespace std;
//function prototype
void Swap(int A[],int B[],int Size) ;
int main()
{
//size of the array
int size=7;
//integer array values for A and B vectors
int A[]={1,2,3,4,5,6,7};
int B[]={8,9,10,11,12,13,14};
cout<<"Contents of array A before swap: ";
for(int index=0;index<size;index++)
cout<<A[index]<<" ";
cout<<endl;
cout<<"Contents of array B before swap: ";
for(int index=0;index<size;index++)
cout<<B[index]<<" ";
//call the Swap function with integer array A, B
and size of the array
Swap(A,B,size);
cout<<" Contents of array A after swap: ";
for(int index=0;index<size;index++)
cout<<A[index]<<" ";
cout<<endl;
cout<<"Contents of array B after swap: ";
for(int index=0;index<size;index++)
cout<<B[index]<<" ";
system("pause");
return 0;
}
//The method Swap that takes two integer arrays A and B,
size of the array
//swaps the elements of A and B arrays
void Swap(int A[],int B[],int Size)
{
int index;
//temporary variable to store the element of A
array
int temp;
for(index=0;index<Size;index++)
{
//store A array element in temp
temp=A[index];
//store the element of B array at index
into A array
A[index]=B[index];
//copy temp element to B array
B[index]=temp;
}
}
######### Part -C #########
#include<iostream>
#include<math.h>
using namespace std;
//function prototype
void Reverse(int A[],const int Size);
int main()
{
//size of the array
int size=4;
//integer array values for A and B vectors
int A[]={1,2,3,4};
cout<<"Contents of array A before swap: ";
for(int index=0;index<size;index++)
cout<<A[index]<<" ";
cout<<endl;
//call the Reverse function with integer array A and size of the array
Reverse(A,size);
cout<<" Contents of array A after Reverse: ";
for(int index=0;index<size;index++)
cout<<A[index]<<" ";
cout<<endl;
system("pause");
return 0;
}
//The method Reverse that takes integer arrays A and size of the array
//and reverse the elements of the array ,A
void Reverse(int A[],const int Size)
{
int start=0;
int end=Size-1;
//temporary variable
int temp;
while(start < end)
{
temp = A[start];
A[start] = A[end];
A[end] = temp;
start++;
end--;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.