Question

In: Computer Science

Create all necessary code to make this main function work. It is not allowed to change...

  1. Create all necessary code to make this main function work. It is not allowed to change the main function.

int main()

{

       int ListDataSample1[] = { 1, 1, 1 };

       int ListDataSample2[] = { 2, 2, 2 };

       List<int> List1 = List<int>(ListDataSample2, 3);

       List<int> List2 = List<int>(ListDataSample2, 3);

      

       cout << "List1 :" << List1 << endl;

       cout << "List2 :" << List2 << endl << endl;

       List1 += List2;

      

       cout << "List1 :" << List1 << endl;

       cout << "List2 :" << List2 << endl << endl;

       system("pause");

       return 0;

}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

using namespace std;

//templated List class

template<typename T>

class List{

                //array pointer to store data

                T* arr;

                //current number of elements

                int size;

                public:

                //constructor taking an array pointer and size

                List(T* _arr, int _size){

                                //initializing dynamic array

                                arr=new T[_size];

                                //copying size

                                size=_size;

                                //copying all elements from _arr to arr

                                for(int i=0;i<size;i++){

                                               arr[i]=_arr[i];

                                }

                }

                //destructor to deallocate memory occupied by arr

                ~List(){

                                delete[] arr;

                }

                //overloaded << operator (defined outside the class)

                template <typename T2>

                friend ostream& operator << (ostream &out, const List<T2> &list);

               

                //overloaded += operator method

                List& operator +=(const List& other){

                                //creating a new array with enough capacity to store elements of both arrays

                                T* newArr=new T[size+other.size];

                                //copying elements of this array to newarr

                                for(int i=0;i<size;i++){

                                               newArr[i]=arr[i];

                                }

                                //copying elements of other array to newarr

                                for(int i=0;i<other.size;i++){

                                               newArr[i+size]=other.arr[i];

                                }

                                //destroying old array from this object

                                delete[] arr;

                                //replacing with new array

                                arr=newArr;

                                //updating size

                                size=size+other.size;

                                //returning this object

                                return *this;

                }

};

//overloaded << operator to print the contents of a list

template<typename T2>

ostream& operator << (ostream &out, const List<T2> &list){

                //looping and printing all elements of list separated by comma and space

                //since this is a friend function of List class, it can access private elements of

                //list class

                for(int i=0;i<list.size;i++){

                                out<<list.arr[i];

                                if(i!=list.size-1){

                                               out<<", ";

                                }

                }

                return out;

}

//no change is made to main method

int main()

{

    int ListDataSample1[] = { 1, 1, 1 };

    int ListDataSample2[] = { 2, 2, 2 };

    List<int> List1 = List<int>(ListDataSample2, 3);

    List<int> List2 = List<int>(ListDataSample2, 3);

    cout << "List1 :" << List1 << endl;

    cout << "List2 :" << List2 << endl

         << endl;

    List1 += List2;

    cout << "List1 :" << List1 << endl;

    cout << "List2 :" << List2 << endl

         << endl;

    system("pause");

    return 0;

}

/*OUTPUT*/

List1 :2, 2, 2

List2 :2, 2, 2

List1 :2, 2, 2, 2, 2, 2

List2 :2, 2, 2


Related Solutions

Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: cookout.js Assume that hot dogs come in packages of 10, and hot dog buns come in packages of 8. Write a program called cookout.js, that calculates the number of...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var triangle.js Write a program that is required to use nested loops to generate a triangle as shown in the sample run below. The program should begin by prompting the user...
Make a z-matrix for alanine showing all work necessary.
Make a z-matrix for alanine showing all work necessary.
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Please make my Code working and pass the test but do NOT change anything in main...
Please make my Code working and pass the test but do NOT change anything in main function, thank you. #include <iostream> using namespace std; void sort(int *A, int n){    for(int passes = 0;passes < 2;passes++) { // shift can have only two values either 0 or 16, used for shifting purpose int shift = passes * 16; int N = 1<<(16 + 1);    // Temporary array for storing frequency of upper or lower 16 bits int temp[N];   ...
Please show all work and calculations neatly! no code allowed! --------------- Using Simpson's Rule, verify that...
Please show all work and calculations neatly! no code allowed! --------------- Using Simpson's Rule, verify that the weight w1 equals 4h/3 by integrating the appropriate Lagrange basis function
Add two lines of code in main() function to create a variable, goodStudent, whose type is...
Add two lines of code in main() function to create a variable, goodStudent, whose type is Student with name, "John" and age, 21. public class Student { private String name; private int age; public Student(){ name = ""; age= 0; } public Student(String initName){ name = initName; age = 0; } public String getName() { return name; } public void setAge(int anAge) { age = anAge; } public static void main (String[] args) { // !!!! Your two lines of...
Complete the code to make the following main work public class Time { **put the solution...
Complete the code to make the following main work public class Time { **put the solution here** public static void main(String[] args){ Time a = new Time(15,10,30); Time b = new Time(); // default to 15:00:00(the start time of our class!) System.out.println(a); // should print out: 15:10:30 System.out.println(b); // should print out: System.out.println(a.dist(b)); // print the difference in seconds between the two timestamps } }
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT