how can i count and display the distinct values in this array? void sorting(int
ID: 641505 • Letter: H
Question
how can i count and display the distinct values in this array?
void sorting(int a[], int j)
{
for(int i = 0; i < j; i++)
{
for(int k = 0; k < j; k++)
{
if(a[i] > a[k])
{
int temp;
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
}
void count(int a[], int j)
{
int occurence = 0;
cout << "N" << " Count" << endl;
for(int i = 1; i < j; i++)
{
cout << a[i] << " ";
for(int k = 1; k < j; k++)
{
if(a[i] == a[k])
{
occurence++;
}
else
{
occurence = 1;
}
}
cout << occurence << endl;
}
}
Explanation / Answer
void count(int a[], int j)
{
int occurence = 0;
cout << "N" << " Count" << endl;
for(int i = 1; i < j; i++)
{
cout << a[i] << " ";
for(int k = 1; k < j; k++)
{
if(a[i] == a[k])
{
occurence++;
}
}
cout << occurence << endl;
}
void distinct(int a[] , int j){
int distinct=0;
for(int i=1;i<j;i++){
for(int k=1;k<j;k++){
if(a[i]==a[k]){
distinct++;
}
}
if(distinct==1){
cout<<a[i];
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.