CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 21st, 2009, 01:42 PM
    tw02 tw02 is offline
    Junior Member
     
    Join Date: Sep 2009
    Posts: 10
    tw02 is an unknown quantity at this point (<10)
    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;
    }
    Reply With Quote
      #2    
    Old November 21st, 2009, 03:14 PM
    treuss's Avatar
    treuss treuss is offline
    Elite Member
     
    Join Date: Jan 2004
    Location: Düsseldorf, Germany
    Posts: 2,332
    treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+) treuss is a splendid one to behold (750+)
    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.
    Reply With Quote
      #3    
    Old November 22nd, 2009, 01:53 AM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,960
    Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Re: Help with array union and intersection

    Quote:
    Originally Posted by tw02 View Post
    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.
    As treuss says, please use code tags when posting code.

    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);
    }
    Note that there is no fancy I/O, reading from a file, etc. The only thing you see is a function to do the real work of the assignment given to you. You need to write these functions and test to see if they work. Once they work, then you spend your remaining time doing input from a file -- it makes no sense wasting time on the file reading stuff, and you have no idea if the set intersection/union code even works or not.

    Regards,

    Paul McKenzie

    Last edited by Paul McKenzie; November 22nd, 2009 at 02:07 AM.
    Reply With Quote
      #4    
    Old November 22nd, 2009, 03:02 AM
    laserlight laserlight is online now
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: Help with array union and intersection

    Quote:
    Originally Posted by Paul McKenzie
    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.
    You may also need to use std::sort and std::unique since those functions work on sorted ranges, and your requirements require distinct elements in the final result.
    __________________
    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
    Reply With Quote
      #5    
    Old November 22nd, 2009, 06:55 PM
    nuzzle nuzzle is offline
    Member +
     
    Join Date: May 2009
    Posts: 738
    nuzzle has a spectacular aura about (150+) nuzzle has a spectacular aura about (150+)
    Re: Help with array union and intersection

    Quote:
    Originally Posted by tw02 View Post
    //output sorted array
    You don't need to sort the arrays. You just scan the arrays repeatedly and consider what to do with each element.
    Reply With Quote
    Reply

    Bookmarks

    Tags
    arraya[i] , j(0) , num , num2
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:29 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.