Find unequal elements in array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Find unequal elements in array

  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
    Posts
    12,095

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,379

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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
    86

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center