| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with array union and intersection
I am trying to write a program that reads in two arrays of integers representing the elements of two sets from two different user-specified input files and calculates both the union and the intersection of the two sets. Use arrays to contain the input sets and build both the union and intersection. Both union and intersection must not contain duplicates. Here are the 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 Here is what I have so far. Am I on the right track? Or not even close? Any help is appreciated, thank you. #include <iostream> #include <ifstream> #include <string> using namespace std; int main() { int i(0), j(0), num, num2, arraya[i], arrayb[j];//declare and initialize variables string filename, filename1; ifstream input ("inputA.dat"); cout<<"Enter the name of the first input file"<<endl;//first input file from user cin>>filename; input.open(filename.c_str()); if (input.fail()) { cerr<<"Could not open file";//if file cannot be opened exit(1); } input>>num; while(!input.eof()) {//read numbers from file arraya[i]=num; i++; input>>num; } cout<<"Array 1: "<<arraya[i]<<endl;//first array output ifstream input1 ("inputB.dat"); cout<<"Enter the name of the second input file"<<endl;//second input from user cin>>filename1; input1.open(filename1.c_str()); if (input1.fail()) { cerr<<"Could not open file";//if file cannot be opened exit(1); } input1>>num2; while(!input1.eof()) {//read numbers from file arrayb[j]=num2; j++; input1>>num2; } cout<<"Array 2: "<<arrayb[j]<<endl;//second array output for (int startindex=0; startindex<i; startindex++) {//step through each number of array int smallestindex=startindex;//smallest number for (int currentindex=startindex+1; currentindex<i; currentindex++) {//begin at startindex+1 if(arraya[currentindex]<arraya[smallestindex]) smallestindex=currentindex;//if new small number is found } swap(arraya[startindex], arraya[smallestindex]);//swap beginning and smallest numbers } cout<<"Sorted Array 1: "<<arraya[startindex]<<endl;//output sorted array for (int startindex1=0; startindex1<j; startindex1++) {//step through each number of array int smallestindex1=startindex1;//smallest number for (int currentindex1=startindex1+1; currentindex1<j; currentindex1++) {//begin at startindex+1 if(arrayb[currentindex1]<arrayb[smallestindex1]) smallestindex1=currentindex1;//if new small number is found } swap(arrayb[startindex1], arrayb[smallestindex1]);//swap beginning and smallest numbers } cout<<"Sorted Array 2: "<<arrayb[startingindex1]<<endl;//output sorted array //find union of arrays int h(0), arrayu[]; for(int k=0; k<i; k++){ arrayu[h] = arraya[i]; h++; } for(int k=0; k<j; k++){ arrayu[h] = arrayb[j]; h++; } for (int startindex2=0; startindex2<h; startindex2++) {//step through each number of array int smallestindex2=startindex2;//smallest number for (int currentindex2=startindex2+1; currentindex2<h; currentindex2++) {//begin at startindex+1 if(arrayu[currentindex2]<arrayu[smallestindex2]) smallestindex2=currentindex2;//if new small number is found } swap(arrayu[startindex2], arrayu[smallestindex2]);//swap beginning and smallest numbers } return 0; } |
|
#2
|
||||
|
||||
|
Re: Help with array union and intersection
With 10 posts, I would assume you have seen a hint that code should be formatted in code tags.
__________________
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf Premature optimization is the root of all evil --Donald E. Knuth Computer programming requires a great deal of attention to detail and an ability to read and understand abstract instructions. Posting a question to this forum in a way that it will get answered, requires the same, though too far smaller degree. Thus, if you don't get your question answered here, maybe it's time to accept that programming is not your thing. --Yours truly Please read Information on posting before posting, especially the info on using [code] tags. |
|
#3
|
|||
|
|||
|
Re: Help with array union and intersection
Quote:
First, the "real-world" C++ program to do this is around 4 lines using set_intersection() and set_union(), and they work with arrays of numbers. Your assignment didn't say not to use these functions. Secondly, if you can't use those functions, you should learn to write your own functions, and not shove everything into main(). Writing programs as you've done makes the program much harder to maintain and debug. In addition, how you get the data is not important when you initially write the code. The "you must read from an input file" instructions that you get from teachers does nothing but wastes a students time in getting the important part of the assignment done. Believe me when I say that. Here is a sample of how you should have started writing the assignment. Code:
void get_union(int *set1, int size_set1, int *set2, int size_set2, int *output)
{
//... fill in output with the union of set1 and set2
}
void get_intersection(int *set1, int size_set1, int *set2, int size_set2, int *output)
{
//... fill in output with the intersection of set1 and set2
}
int main()
{
int array1[] = {-1, 10, 20};
int array2[] = {10, 20};
int output[3];
get_union(array1, 3, array2, 2, output);
}
Regards, Paul McKenzie Last edited by Paul McKenzie; November 22nd, 2009 at 02:07 AM. |
|
#4
|
|||
|
|||
|
Re: Help with array union and intersection
Quote:
__________________
C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way Kindly rate my posts if you found them useful
|
|
#5
|
|||
|
|||
|
Re: Help with array union and intersection
You don't need to sort the arrays. You just scan the arrays repeatedly and consider what to do with each element.
|
![]() |
| Bookmarks |
| Tags |
| arraya[i] , j(0) , num , num2 |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|