Thread: set calculator

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    3

    set calculator

    the program is to find intersection,union and difference of two sets.
    the program take the input correctly but after it crashes with the message that some exe is not working


    Code:
    #include<iostream>
    using namespace std;
    
    
    void Input(int *A, int*B, int size1, int size2)      //input function
    {
        cout<<"enter the first set"<<endl;
        for(int i=0;i<size1;i++){
            cin>>A[i];                                  //input of first set
        }
    
    
        cout<<"enter the second set"<<endl;
        for(int j=0;j<size2;j++){
            cin>>B[j];                                  //input of second set
        }
    
    
    }
    
    
    int  *Intersection(int *A, int*B, int size1, int size2) //intersection function
    {
        int *C=0;
        int j;
        int i;
        for(i=0;i<size1+size2;i++)
        {
            for(j=0;j<size1+size2;j++)
            {
                if(A[i]==B[j])
                {
                    C[i]=A[i];
                }
            }
        }
        
        for(int k=0;k<i;k++)              //printing values after intersection
        {
            cout<<C[k]<<endl;
        }
        return C;
    }
    
    
    int *Union(int *A, int*B, int size1, int size2)    //union function
    {
        int *C=0;
        int i;
        int j;
        for(i=0;i<size1+size2;i++)
        {
            C[i]=A[i];
        }
    
    
        int h=i+1; 
        for(j=0;j<size1+size2;j++)
        {
            if(C[j]!=B[j]){
                C[h]=B[j];
                h++;
            }
        }
    
    
        for(int k=0;k<j;k++)      //printing values after union
        {
            cout<<C[k]<<endl;
        }
        return C;
    }
    
    
    int *Difference(int *A, int*B, int size1, int size2)   //difference function
    {
        int *C=0;
        int i;
        int j;
        for(i=0;i<size1+size2;i++)
        {
            for(j=0;j<size1+size2;j++)
            {
                if(A[i]!=B[j])
                {
                    C[i]=A[i];
                }
            }
        }
    
    
        for(int k=0;k<i;k++)         //printing value after (A-B) difference
        {
            cout<<C[k]<<endl;
        }
        return C;
    }
    
    
    int main()
    {
        int size1=0;
        int size2=0;
        int a;
        int *str;
        cin>>size1>>size2;
        int *A=new int(size1);   //creating dynamic array
        int *B=new int(size2);
        Input(A,B,size1,size2);
        cout<<"For insection press 1"<<endl;
        cout<<"For union press 2"<<endl;
        cout<<"For difference press 3"<<endl;
        cin>>a;
        if(a==1)
        {
            str=Intersection(A,B,size1,size2);
        }
        if(a==2)
        {
            str=Union(A,B,size1,size2);
        }
        if(a==3)
        {
            str=Difference(A,B,size1,size2);
        }
        
        delete[]A;
        delete[]B;
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
        int *C=0;
        int i;
        int j;
        for(i=0;i<size1+size2;i++)
        {
            C[i]=A[i];
        }
    C points to an illegal location; of course it crashes!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    3
    so what I do?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Allocate memory. Actually, better yet: instead of doing the manual memory management that you're doing now, use std::vector<int> containers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator in c...
    By nobita in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 09:22 AM
  2. Calculator
    By Justinjah91 in forum C Programming
    Replies: 5
    Last Post: 08-18-2009, 12:56 PM
  3. RPN calculator
    By bassist11 in forum C Programming
    Replies: 14
    Last Post: 03-02-2009, 11:37 PM
  4. calculator
    By adr in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2006, 11:24 AM
  5. Please help with this calculator
    By Asbestos in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2005, 01:00 PM