-
January 12th, 2014, 12:43 PM
#1
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.
-
January 12th, 2014, 01:13 PM
#2
Re: Find unequal elements in array
Why do you have a break statement in your inner loop?
-
January 12th, 2014, 01:53 PM
#3
Re: Find unequal elements in array
Originally Posted by GCDEF
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.
-
January 12th, 2014, 02:19 PM
#4
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)
-
January 12th, 2014, 02:41 PM
#5
Re: Find unequal elements in array
Originally Posted by fighter29
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.
-
January 13th, 2014, 03:54 AM
#6
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
-
January 15th, 2014, 01:43 AM
#7
Re: Find unequal elements in array
Originally Posted by greve
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|