Question

This question has been answered before, but no codes given as the answer were right. please...

This question has been answered before, but no codes given as the answer were right. please help.

In the mathematical theory of sets, a set is defined as a collection of distinct items of the same type. In some programming languages, sets are built-in data types; unfortunately, this is not the case in C++. However, we can simulate a set using a one-dimensional array.

Some operations can be performed on sets. We will consider three(3) of them: union, intersection and difference. These are binary operations requiring two sets as operands. The union of two sets, A and B, is a set that contains all elements in both A and B. The intersection of two sets, A and B, is a set that contains elements common to both A and B. The difference of two sets, A and B, is a set that contains only the elements in A but not in B and excluding the common elements in A and B.  

For example, if A and B are two sets of integers defined as A = {5, 7, 8, 10} and B = {3, 9, 10}, then their union is the set {3, 5, 7, 8, 9, 10}, their intersection is the set {10}, their difference of A and B ( A - B) is the set {5, 7, 8}.  

Write a program that computes the union, intersection, and difference of two sets stored in two one-dimensional arrays. Populate the arrays from the following input files:  

inputA.dat

0 1 -3 5 -11 6 8 9 11 17 15 7 4 12

inputB.dat

0 -1 3 7 -6 16 5 11 12 4 21 13

The output should be displayed on screen and in an output file.  Prompt user for file names. No duplicates are allowed in union, intersection or difference.

The following functions must be used:

void readfile_array(ifstream& a, ifstream& b, int arraya[], int& asize, int arrayb[], int& bsize);

void printarray(int array[], int size, ofstream& o);

int diff (int a[], int b[], int dif[], int asize, int bsize);

int duplicates (int array[], int d[], int size);

int intersection (int a[], int b[], int asize, int bsize, int inter[]);

int arrayunion (int a[], int b[], int asize, int bsize, int aunions[]);

void sort (int array[], int n);  

SAMPLE OUTPUT:

Enter filenames=>inputA.dat

inputB.dat

out.dat

Array Elements in File A

0 1 -3 5 -11 6 8 9 11 17 15 7 4 12

Array Elements in File B

0 -1 3 7 -6 16 5 11 12 4 21 13

Sorted ArrayA

-11 -3 0 1 4 5 6 7 8 9 11 12 15 17

Sorted ArrayB

-6 -1 0 3 4 5 7 11 12 13 16 21

Difference from ArrayA and ArrayB

-11 -3 1 6 8 9 15 17

Number of elements in intersection = 6

0 4 5 7 11 12

Number of elements in union = 26

-6 -1 0 3 4 5 7 11 12 13 16 21 -11 -3 0 1 4 5 6 7 8 9 11 12 15 17

Sorted intersection

0 4 5 7 11 12

Sorted union

-11 -6 -3 -1 0 0 1 3 4 4 5 5 6 7 7 8 9 11 11 12 12 13 15 16 17 21

The Intersection of A and B (no duplicates)

0 4 5 7 11 12

The Union of A and B(no duplicates)

-11 -6 -3 -1 0 1 3 4 5 6 7 8 9 11 12 13 15 16 17 21

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

#include<iostream>
#include<conio.h>
#include<fstream>
#include<algorithm>
using namespace std;

int countArray(string& file_name)
{   fstream myfile(file_name.c_str(), std::ios_base::in);
   int iter;
   int count=0;
while (myfile >> iter)
{
       count++;
}
   myfile.close();
   return count;
}
int diff(int a[], int b[], int dif[], int asize, int bsize)
{
   int diffCount = 0;
   for(int i =0;i<asize;i++)
   {
       int flag = 0;
       for(int j=0;j<bsize;j++)
       {
           if(a[i] == b[j])
           {
               flag = 1;
           }  
       }
       if(flag == 0)
       {
           dif[diffCount] = a[i];
           diffCount++;
       }  
   }
   return diffCount;
}
int intersection (int a[], int b[], int asize, int bsize, int inter[])
{
   int tempValue = 0;
   int interCount = 0;
   for(int i =0;i<asize;i++)
   {
       int flag = 0;
       for(int j=0;j<bsize;j++)
       {
           if(a[i] == b[j])
           {
               flag = 1;
               tempValue = b[j];
           }  
       }
       if(flag == 1)
       {
           inter[interCount] = tempValue;
           interCount++;
       }  
   }
   return interCount;
}
void readfile_array(fstream& a, fstream& b, int arraya[], int& asize, int arrayb[], int& bsize)
{
int iter1;
   int countA=0;
while (a >> iter1)
{
       arraya[countA] = iter1;
       countA++;
}
   a.close();

int iter2;
   int countB=0;
while (b >> iter2)
{
       arrayb[countB] = iter2;
       countB++;
}
   b.close();
}
void printarray(int array[], int size, ofstream& o)
{
   if(o)
   {
           for(int i = 0;i<size;i++)
           {
               o << array[i];
               o << " ";
           }  
   }  
}
void sort (int array[], int n)
{
   int temp;
for(int i=0;i<n;i++)
   {
       for(int j=0;j<n;j++)
       {
           if(array[j]>array[j+1])
           {
               temp=array[j];
               array[j]=array[j+1];
               array[j+1]=temp;
           }
       }
   }
}
bool duplicates(int array[],int size,int n){
   int i;
   for(i=0;i<size;i++){
   if(array[i]==n)
return true;         
   }
   return false;
}

int arrayunion (int a[], int b[], int asize, int bsize, int aunions[])
{  
   int unionCount =0;
int i;
   for(int k=0;k<asize;k++)
   {
       aunions[k]=a[k];
       unionCount++;
   }
   int bIndex = 0;
   for(int k=asize;k<asize+bsize;k++)
   {
       if(!(duplicates(a,asize,b[bIndex])))
       {  
           aunions[k] =b[bIndex];
           bIndex++;
           unionCount++;
       }
   }
   return unionCount;
}
int arrayunionwithduplicates (int a[], int b[], int asize, int bsize, int aunions[])
{  
int i,unionCount=0;
   for(int k=0;k<asize;k++)
   {
       aunions[k]=a[k];
       unionCount++;
   }
   int bIndex = 0;
   for(int k=asize;k<asize+bsize;k++)
   {  
           aunions[k] =b[bIndex];
           bIndex++;
   }
}
int main()
{
cout<<"\n\n\t\tSET OPERATIONS\n\n";
string inputFileNameA = "inputA.dat";
string inputFileNameB = "inputB.dat";
string outputFileName = "out.data";
int arrayASize = countArray(inputFileNameA);
int arrayBSize = countArray(inputFileNameB);
int A[arrayASize];
int B[arrayBSize];
int dif[arrayASize];
int inter[arrayASize];
int aunionswithduplicates[arrayASize+arrayBSize];
int aunions[arrayASize+arrayBSize];
fstream myfile1(inputFileNameA.c_str(), std::ios_base::in);
fstream myfile2(inputFileNameB.c_str(), std::ios_base::in);
readfile_array(myfile1,myfile2,A,arrayASize,B,arrayBSize);
ofstream f(outputFileName.c_str());

f <<"\nArray Elements in File A:\n ";
printarray(A,arrayASize,f);

f << "\nArray Elements in File B:\n ";
printarray(B,arrayBSize,f);

f << "\n Sorted ArrayA\n";
sort(A,arrayASize);
printarray(A,arrayASize,f);

f << "\n Sorted ArrayB\n";
sort(B,arrayBSize);
printarray(B,arrayBSize,f);

f << "\n Difference from ArrayA and ArrayB\n";
int difCount = diff(A,B,dif,arrayASize,arrayBSize);
printarray(dif,difCount,f);

int interCount = intersection(A,B,arrayASize,arrayBSize,inter);
f << "\n Number of elements in intersection: "<<interCount<< "\n";
printarray(inter,interCount,f);

arrayunionwithduplicates(A,B,arrayASize,arrayBSize,aunionswithduplicates);
f << "\n Number of elements in Union: "<<arrayASize+arrayBSize<< "\n";
printarray(aunionswithduplicates,arrayASize+arrayBSize,f);

f << "\n Sorted Intersection \n";
sort(inter,interCount);
printarray(inter,interCount,f);

f << "\n Sorted Union \n";
sort(aunionswithduplicates,arrayASize+arrayBSize);
printarray(aunionswithduplicates,arrayASize+arrayBSize,f);

intersection(A,B,arrayASize,arrayBSize,inter);
f << "\n The Intersection of A and B(no duplicates) \n";
printarray(inter,interCount,f);

int unionCount = arrayunion(A,B,arrayASize,arrayBSize,aunions);
f << "\n The Union of A and B(no duplicates) \n";
sort(aunions,unionCount);
printarray(aunions,unionCount,f);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
This question has been answered before, but no codes given as the answer were right. please...
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
  • Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize...

    Write a class called RandomIntegerArrayCreator that: upon its object instantiation: will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, create a random integer array of size arraySize (15 OR LESS) with elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear multiple times in this array, has two accessor methods: public int getArraySize() that will...

  • PLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS...

    PLEASE ANSWER THE FOLLOWING IN C++!! PLEASE READ THE QUESTION CAREFULLY!!! AS WELL AS WHOEVER ANSWERS THIS CORRECTLY I WILL UPVOTE!!! In this project you will design, implement and test the ADT set using both Arrays and Linked Lists and implement all the operations described in the following definitions in addition to the add and remove operations. Sets are one of the basic building blocks for the types of objects considered in discrete mathematics. A set is an unordered collection...

  • Give a set with 13 elements, show the final result of executing the following instructions with...

    Give a set with 13 elements, show the final result of executing the following instructions with UF WeightedQuickUnion: union(7, 8), union(9, 10), union(11, 12), union(9, 11), union(0, 1), union(0, 2), union(6, 4), union(0, 6). Assuming initially there are 13 components. a) Show the final contents of idl array b) Draw the final forest of trees. union(3,5), union(4, 3).

  • ==> Please In C Language <== Insert a value in a sorted array: Given an integer...

    ==> Please In C Language <== Insert a value in a sorted array: Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion. For example, when k=12 , the...

  • JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

    JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; ​ // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...

  • Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment...

    Use C++ programing language Please don't use other Chegg solved solutions, make it new thanks. Assignment requirements Pre-requisite: Please read zyBook Chapter 6 Sets and understand the three operations of Sets (union, intersection, and difference) . You will be creating your custom Set (think a list of unique elements) class in C++ or a language of your choice. Please do NOT use STL, or any pre-defined library for this assignment. 1. The data type of the set collection: array (or...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • Let us consider 2 sets A = {1,2,3,4} and B = { 5,6}. 1. What is...

    Let us consider 2 sets A = {1,2,3,4} and B = { 5,6}. 1. What is {}? 2. What is |A|? 3. What is A union B? 4. What is A intersection B? 5. What is {{}}? 6. What is |{{}}|? 7. What is A x B? 8. What is BxA? 9. If A has 2^5 elements and B has 2^6 elements, how many elements are there in AxB?

  • Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array...

    Consider the following mergeSortHelper method, which is part of an algorithm to recursively sort an array of integers. /**  Precondition: (arr.length == 0 or 0 <= from <= to <= arr.length) * arr.length == temp.length */ public static void mergeSortHelper(int[] arr, int from, int to, int[] temp) { if (from < to) { int middle = (from + to) / 2; mergeSortHelper(arr, from, middle, temp); mergeSortHelper(arr, middle + 1, to, temp); merge(arr, from, middle, to, temp); } } The merge method...

  • I have a table that looks like this. 1234 5678 9101112 I need to use this...

    I have a table that looks like this. 1234 5678 9101112 I need to use this code public class Lab8 { public static void main(String argv[]) { int ar[][] = new int[3][4]; int n = 1; for (int i=0; i < ar.length; i++) { for (int j=0; j< ar[i].length; j++) { ar[i][j] = n; n++; } } // print out the elements of the array // left to right, top to bottom for (int i=0; i < ar.length; i++) {...

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