I want to output the unique item from specific c++ array -
i have created function count duplicate items in array .
, fine . want output unique items , , problem .
my function:
void repeatedcounter(int n){ int i, j, temp, count= 0; int *numbers = new int[n]; for(i=0;i<n;i++){ cout << "enter number (" << i+1 << "): "; cin >> *(numbers+i); } cout << "---------------------\n"; for(i=0;i<n;i++){ temp = *(numbers+i); for(j=0;j<n;j++){ if(temp == *(numbers+j)){ ++count; } } if(*(numbers+i+1) != temp) cout << *(numbers+i) << "= " << count << endl; count= 0; } delete []numbers; }
main function:
int num_of_digits= 0; cout << "how many numbers: "; cin >> num_of_digits; repeatedcounter(num_of_digits);
example:
inputs 1 5 3 5 1
wrong result (current output)
1= 2 5= 2 3= 1 5= 2 1= 2
what want
1= 2 5= 2 3= 1
try this,
for(i=0;i<n;i++){ count= 0; temp = *(numbers+i); bool found = false; for(j=0;j<n;j++){ if(temp == *(numbers+j)){ ++count; } } for(j=i+1;j<n;j++) { if(temp == *(numbers+j)){ found = true; } } if(found) continue; if(*(numbers+i+1) != temp) cout << *(numbers+i) << "= " << count << endl; }
Comments
Post a Comment