C++ need help

This is a discussion on C++ need help within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; main() { int A[5]; int B[5]; cout<<"Enter First 5 elements: "<<endl; for(int i=0; i<5; i++){ ...

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    Cool C++ need help

    Code:
    #include<iostream>
    using namespace std;
    
    main()
    {
          int A[5];
          int B[5];
          
          cout<<"Enter First 5 elements: "<<endl;
          
          for(int i=0; i<5; i++){
                  cin>>A[i];
                  }
          
          cout<<"Enter Second 5 elements: "<<endl;
          
          for(int i=0; i<5; i++){
                  cin>>B[i];
                  }
          
          cout<<"Union of Set A and Set B: "<<endl;
          
          cout<<"\nIntersection of Set A and Set B: "<<endl;
          
          for(int i=0; i<5; i++){
                  for(int t=0; t<5; t++){
                          if(A[i] == B[t]){
                                  cout<<A[i]<<" ";
                                  }
                                  }
                                  }
                                  cout<<endl;
                                  system("pause");
                                  return 0;
                                  }

    hey can someone help me[need help!!!].....on how to solve the union of set a and set b like in ascending order....

    Output:

    like in
    Set A i enter:
    1 2 3 4 5
    Set B i enter:
    1 2 3 5 6
    Union of Set A and B
    1 2 3 4 5 6
    Intersection of Set A and B
    1 2 3 4 5

    plz need help!.....

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Union:
    Code:
    std::vector<int> setunion;
    for(int c1=0;c1<5;c1++)
        setunion.push_back(A[c1]);
    for(int c2=0;c2<5;c2++)
    {
        if(!contain_p(B[c2],setunion)
        {
             setunion.push_back(B[c2]);
        }
    }
    As I'd not give you the whole code..
    you need to:
    1. Define contain_p() yourself such that it returns a non-zero if setunion contains the given number.
    2.Write the code for intersection.
    3.Sort the result in ascending order.

    This algorithm is rather poor but is sufficient if your input sets are small; also sets are natively supported in c++ to some extent.
    Last edited by manasij7479; 07-06-2011 at 08:12 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    There are many ways. A simple way is to create a third array, copy the content of both arrays into it, sort it in ascending order, remove duplicates. [And, no, I'm not going to code that - the point of the exercise if for you to learn by doing, not by copying].

    Incidentally, your code for printing out the intersection does not sort the values - the values will be out of order if they are input out of order. It will also report duplicates if either array contains duplicates.

    You also need to specify that main() returns int (there is no implicit return type in C++).
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    Quote Originally Posted by manasij7479 View Post
    Union:
    Code:
    std::vector<int> setunion;
    for(int c1=0;c1<5;c1++)
        setunion.push_back(A[c1]);
    for(int c2=0;c2<5;c2++)
    {
        if(!contain_p(B[c2],setunion)
        {
             setunion.push_back(B[c2]);
        }
    }
    As I'd not give you the whole code..
    you need to:
    1. Define contain_p() yourself such that it returns a non-zero if setunion contains the given number.
    2.Write the code for intersection.
    3.Sort the result in ascending order.

    This algorithm is rather poor but is sufficient if your input sets are small.
    how do i print it?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    If you could work out how to print values in your original code, you can work out how to print out another lot of values.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    I find it funny that you've posted invalid C++ code that shouldn't compile.
    Either you are using a very old compiler (eg Turbo C++), or you are not posting your real code.
    So which is it?

    Oh, and that indentation is awful. You should spent some time learning how to properly indent™.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    how do i print it?
    You can forget that it is a std::vector and print it like a normal array..(querying the size with size()).
    Or if you actually want to learn ...read up on STL iterators ...
    Last edited by manasij7479; 07-06-2011 at 08:45 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21