void appendVector(vector & v, const vector & w); Provide an implementation of th
ID: 3843801 • Letter: V
Question
void appendVector(vector & v, const vector & w);
Provide an implementation of the appendVector function whose declaration is shown above. Both arguments of the function are vectors of int. The function should modify vector v by appending to it all the elements of w. For example, if v = (4, 2, 5) and w = (11, 3), then v will become (4, 2, 5, 11, 3) as a result of calling the function. Hint: the vector class has a function called push_back that appends values passed into it. Write test code that thoroughly tests the function. The test code should use assertions.
Explanation / Answer
import java.util.*;
public class appendVector {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter initial size of vector 'v'");
int vsize=sc.nextInt();
System.out.println("enter initial size of vector 'w'");
int wsize=sc.nextInt();
Vector v=new Vector(vsize,wsize);
Vector w=new Vector(wsize);
for(int i=0;i<v.capacity();i++)
{
System.out.println("enter elements in vector v:");
int data=sc.nextInt();
v.add(data);
}
for(int i=0;i<w.capacity();i++)
{
System.out.println("enter elements in vector w:");
int data2=sc.nextInt();
w.add(data2);
}
pushbackappend(v,w);
}
private static void pushbackappend(Vector v, Vector w) {
v.addAll(w);
for(int i=0;i<v.capacity();i++)
System.out.print(v.get(i)+" ");
}
}
output:
enter initial size of vector 'v'
3
enter initial size of vector 'w'
2
enter elements in vector v:
4
enter elements in vector v:
2
enter elements in vector v:
5
enter elements in vector w:
11
enter elements in vector w:
3
4 2 5 11 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.