CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2014
    Posts
    3

    Find unequal elements in array

    Hi,
    I would like to know the algorithm or flow for finding unequal elements in an array entered by user. I tried using for loop but didn't get the expected output
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    int a[10],b[10]; //array of integer elements
    int i,j,k ; //traversing operators
    int r =0;
    cout<<"Enter Array Elements = ";
    for(i=0;i<=9;i++)
        cin>>a[i];
    
    cout<<"The common elements are ";
    //For to loop to compare all elements and find unique elements
    for(j=0;j<=9;j++)
    {
    
        for(k=j;k<9;k++)
       {if (a[k] != a[j] && k!=j)
           r++;
    
       break;
       }
    
    
    }
    cout<<"The number of unique elements in the array="<<r;
    
    return 0;
    }
    Thanks for time and consideration awaiting response.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Find unequal elements in array

    Why do you have a break statement in your inner loop?

  3. #3
    Join Date
    Jan 2014
    Posts
    3

    Re: Find unequal elements in array

    Quote Originally Posted by GCDEF View Post
    Why do you have a break statement in your inner loop?
    To increment the counter in case of unequal elements and come out of the inner loop, plz correct me if I am wrong
    Thanks.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Find unequal elements in array

    If you want to find the unique elements, it would be easier if you used a map if you want to know how many if each were entered or a set if you just want to know the unique numbers and how many unique there are.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Find unequal elements in array

    Quote Originally Posted by fighter29 View Post
    To increment the counter in case of unequal elements and come out of the inner loop, plz correct me if I am wrong
    Thanks.
    Your inner loop never runs more than once because of the break statement.

  6. #6
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: Find unequal elements in array

    The problem you are trying to solve is quite universal. You might find a solution in almost every textbook on c++, e.g.: TC++PL, 4th edition Section 32.5.2., p.937

  7. #7
    Join Date
    Jan 2014
    Posts
    3

    Re: Find unequal elements in array

    Quote Originally Posted by greve View Post
    The problem you are trying to solve is quite universal. You might find a solution in almost every textbook on c++, e.g.: TC++PL, 4th edition Section 32.5.2., p.937
    thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured