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

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...
code in python I want to make a function that adds all the multipliers together. The...
code in python I want to make a function that adds all the multipliers together. The code is posted below and please look at wanted output section to see what I want the code to output. The last line of the code is the only addition I need. Thanks. Code: initial=int(input("Initial value : "))       #taking inputs multiplier=float(input("Multiplier : ")) compound=int(input("No of compounds : ")) print("Your values are:") mult=initial                         #initalizing to find answer for i in range(0,compound):         #multiplying by multiplier    ...
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT