Question

I need help implementing these functions in c++. One finds the intersection of two SortedType lists,...

I need help implementing these functions in c++. One finds the intersection of two SortedType lists, and the other finds the union.

SortedType Intersection(SortedType & list1, SortedType & list2);

SortedType Union(SortedType & list1, SortedType & list2);

Any help is appreciated

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to the above question. The explanation is done in the code itself in the form of comments (see image) for better line by line explanation and better understanding.

Also the code is complete. Copy and past the code and replace "SortedType" with the type of list you want and pass it to these function.

Code:

SortedType Intersection(SortedType & list1, SortedType & list2){ // function to print intersaction
   cout<<"Printing Intersaction: ";
   int l1=list1.size();                // getting the length of the list1.
   int l2=list2.size();                // getting the length of the list2.
   for(int i=0;i<l1;i++){                //loop for the index number of list1
       for(int i=0;i<l2;i++){            // loop for the index number of the list2
           if(list1[j]==list2[i]){    // checking the condition for the same character
               cout<<list2[i]<<" ";    //if the character is same then print it.
           }
       }
   }
   cout<<"\n";
}

SortedType Union(SortedType & list1, SortedType & list2){ // function to print union.
   cout<<"Printing Union: ";
   int m=list1.size();                // getting the length of the list1
    int n=list1.size();                // getting the length of the list2
   int i=0,j=0;
   while(i<m&&j<n)                    // loop to check the condition till the last index of both the list
   {
    if(list1[i]<list2[j])                // condition to chcek if the list1 element is lessthen list2
       cout<<list1[i++]<<" ";
    else if(list2[j]<list1[i])            // condition to chcek if the list2 element is lessthen list1
       cout<<list2[j++]<<" ";
    else
    {
       cout<<list2[j++]<<" ";
       i++;
    }}
   while(i<m)                         // printing the remaining element if exist. In case of the list are not of same size.
       cout<<list1[i++]<<" ";

   while(j< n)                        // printing the remaining element if exist. In case of the list are not of same size.
       cout<<list2[j++]<< " ";
}

Note: If you are still unable to understand, then comment with it and I will be happy to help.

Add a comment
Know the answer?
Add Answer to:
I need help implementing these functions in c++. One finds the intersection of two SortedType lists,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help implementing a few functions in c++. Any help would be appreciated 1. a...

    I need help implementing a few functions in c++. Any help would be appreciated 1. a function void PairsAddUpToK(int a[], int a_len, int k) that finds all pairs of numbers in an array that add up to a given number k 2. A function void TriplesAddUpToK(int a[], int a_len, int k) that finds all sets of three numbers in an array that ad up to a given number k 3. A function void SubsetsAddUpToK((int a[], int a_len, int k) that...

  • Questions Write a method that returns the union of two array lists of integers using the...

    Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program. Enter five integers for list1: 5...

  • I need help with this problem this is what I put and these are the errors...

    I need help with this problem this is what I put and these are the errors I got Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of listi followed by the last element of list2, followed by the second to last element of listi, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements...

  • TITle Addition of two lists ; Proj1.asm ; This program add elements of two lists separately, disp...

    TITle Addition of two lists ; Proj1.asm ; This program add elements of two lists separately, display their sums, then exchange two sums, and display then againe INCLUDE Irvine32.inc data list1 WORD 1eh, 20h, 30h list2 NORD 10h, 20h, 38h, 48h suml WoRD? sun2 WORD? code main PROc mov ax, [list1] add ax, [list1+2] add ax, [list1+4] mov sumi, ax call DumpRegs nov bx, [list2] add bx, [list2+2 add bx, [list2+4) add bx, [list2.6] nov sum2, bx call DumpRegs xchg...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • c++ please no global varaible write a value returning function input data to dynamically allocate a...

    c++ please no global varaible write a value returning function input data to dynamically allocate a short array of size elements and store the input data entered from the disk file into array.Return the base address of the array allocated.Read the data from the text file Hello.txt and store the data in reverse order in the array.the size of the data set will be in the file size 10 and the data was 1 2 3 4 5 6 7...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • 2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P)...

    2. Given two unsorted STL lists X and P, write a valid C++ function intersection(X, P) that returns a new list that contains the elements common to both X and P. For example, if X = 5, 2, 1, 4 and P = 4, 5, 7; your algorithm should return a new list containing 5 and 4 (order is not important). Also specify the running time of your algorithm using Big-Oh notation. Hint: As the lists are unsorted, a straightforward...

  • f(x)=2log2x Please help, I need two points on the graph. Its Logarithmic functions.

    f(x)=2log2x Please help, I need two points on the graph. Its Logarithmic functions.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT