Question

computer science c++ 6. The union of two bags is a new bag containing the combined...

computer science c++

6. The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method’s one argument. Include suffi cient comments to fully specify the method. Note that the union of two bags might contain duplicate items. For example, if object x occurs fi ve times in one bag and twice in another, the union of these bags contains x seven times. Specifi cally, suppose that bag1 and bag2 are bags; bag1 contains the strings a , b , and c ; and bag2 contains the strings b , b , d , and e . The expression bag1.union(bag2) returns a bag containing the strings a , b , b , b , c , d , and e . Note that union does not affect the contents of bag1 and bag2

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

#include<stdio.h>

int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n)
{
   if (arr1[i] < arr2[j])
   printf(" %d ", arr1[i++]);
   else if (arr2[j] < arr1[i])
   printf(" %d ", arr2[j++]);
   else
   {
   printf(" %d ", arr2[j++]);
   printf(" %d ", arr1[i++]);
   }
}


while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}

int main()
{
int bag1[] = {1, 2, 4, 5, 6};
int bag2[] = {2, 3, 5, 7};
int m = sizeof(bag1)/sizeof(bag1[0]);
int n = sizeof(bag2)/sizeof(bag2[0]);
printUnion(bag1, bag2, m, n);
getchar();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
computer science c++ 6. The union of two bags is a new bag containing the combined...
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
  • We have two bags containing marbles: • Bag A contains 4 blue, and 3 green marbles...

    We have two bags containing marbles: • Bag A contains 4 blue, and 3 green marbles • Bag B contains 2 blue, 5 green, and 3 yellow marbles We perform this two step procedure. First, draw a marble from bag A uniformly at random. Then put this marble into bag B. Second, draw a marble from bag B uniformly at random. We keep this marble. What is the probability that the marble we keep is green?

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...

  • In this lab, we will implement the Linked Bag. The bag will contain a sequence of...

    In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to thenext Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Data Next Node Then we will design another class named LinkedBag (LinkedBag.java). LinkedBag class has an instance variable called firstNode of...

  • Part A is done, I do not know how to start these steps.. "In computer science,...

    Part A is done, I do not know how to start these steps.. "In computer science, an abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations." (Source: "Abstract Data Types." Wikipedia. Wikimedia Foundation, 2 Nov. 2016. Web. 9...

  • C++. Please leave comments explaining. Thank you! Given two unsorted STL lists X and P ,...

    C++. Please leave comments explaining. Thank you! 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...

  • PYTHON The text file motifFinding.txt contains two strings of DNA code, separated by a new-line character....

    PYTHON The text file motifFinding.txt contains two strings of DNA code, separated by a new-line character. Write a program that opens the file, and stores its contents into two strings, s and t, respectively.   Write code that will find all instances of the string t within the string s. At the end, your program should output the number of times the sub-string t occurs within s, along with the index of the starting position of each occurrence of t within...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • import java.util.*; /** * A class that implements the ADT set by using a linked bag....

    import java.util.*; /** * A class that implements the ADT set by using a linked bag. * The set is never full. * * */ public class LinkedSetWithLinkedBag> implements SetInterface { private LinkedBag setOfEntries; /** * Creates a set from a new, empty linked bag. */ public LinkedSetWithLinkedBag() { //TODO Project1 } // end default constructor public boolean add(T newEntry) { //TODO Project1 // new node is at beginning of chain if(this.setOfEntries.isEmpty()) { if (!this.setOfEntries.contains(newEntry)) this.setOfEntries.add(newEntry); } return true; //...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

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