Continue to Site

# [SOLVED]How do i count the duplicate number for the last one... (codeblock)

Status
Not open for further replies.

#### Ashieboy

##### Member level 1

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
int x[] = {20, 23, 45, 11, 62, 34, 23, 47, 11, 20, 15, 17, 36, 29, 30, 23, 14, 56, 20, 11, 62, 11};

int main(){
int t=22,count;
cout<<"Total number in the array is " <<t<<endl;
int  highest, smallest;
highest=x[0];
for (count=1; count<t; count++){
if (x[count]>highest)
highest=x[count]; }
cout<<"The highest number is "<<highest<<endl;

smallest=x[0];
for (count=1; count<t; count++){
if (x[count]<smallest)
smallest=x[count]; }
cout <<"The smallest number is "<<smallest<<endl;

int average=0,a;
float d=22,sum;
for (a=0; a<22; a++){
average+=x[a];}
sum=(average/d);
cout<<"Average of all the numbers in the array is "<<sum<<endl;

cout<<"\nNumbers in the array that is duplicated"<<endl;
int k=0, l, m, n=0, p=0;
int h;
for (int i=1; i<t; i++)
{
bool matching = false;
for (int j=0; (j<i) && (matching == false); j++)
if (x[i] == x[j]) matching = true;
if (!matching) cout<<x[i]<<"="<<endl;
l=x[k];
while (p<t)
{
m=x[p];
if (l==m)
{
n=n+1;
}
p++;
}
k++;
p=0;
n=0;
}
}

#### Attachments

• 1.PNG
12.6 KB · Views: 123
Last edited by a moderator:

assuming you are counting the occurances of a value in the array - something along the lines of?
Code:
for (int i=0; i<t; i++)
{
bool matching = false;
for (int j=0; (j<i) && (matching == false); j++)
if (x[i] == x[j]) matching = true;
if (!matching)
{
int count=1;
cout<<x[i]<<"=";
for(int k=i+1; k<t; k++)
if(x[i]==x[k]) count++;
cout << count << endl;
}

Ashieboy

### Ashieboy

Points: 2
You are certainly not evaluating the bounds of the array; for instance, at line 31 of the code above you are starting evaluation from 1, not 0. The same at line 10.

oh, thank you horace1 and andre
no wonder 20 r not there I forgot to start from 0. lol.

Another point that don't make any difference at this case, but in order to make the code more self consistent it is advisable you do not manually determine the size of array by constant, but using something like sizeof (int x[]) instead of set it to 22.

Ashieboy

### Ashieboy

Points: 2
when run with CodeBlocks the program
Code:
for (int i=0; i<t; i++)
{
bool matching = false;
for (int j=0; (j<i) && (matching == false); j++)
if (x[i] == x[j]) matching = true;
if (!matching)
{
int count=1;
cout<<x[i]<<"=";
for(int k=i+1; k<t; k++)
if(x[i]==x[k]) count++;
cout << count << endl;
}
gives
Code:
Total number in the array is 22
The highest number is 62
The smallest number is 11
Average of all the numbers in the array is 28.1818

Numbers in the array that is duplicated
20=3
23=3
45=1
11=4
62=2
34=1
47=1
15=1
17=1
36=1
29=1
30=1
14=1
56=1

is there any other way to count duplicate without using "bool"? and how can i only make the cout for duplicate more than 1?

try
Code:
if (!matching)
{
int count=1;
for(int k=i+1; k<t; k++)
if(x[i]==x[k]) count++;
if(count>1) cout<<x[i]<<"=" << count << endl;
}

not sure what you mean by "bool"

Ashieboy

### Ashieboy

Points: 2
oh what i mean by "bool" is the command
Code:
bool matching = false;
for (int j=0; (j<i) && (matching == false); j++)
if (x[i] == x[j]) matching = true;
if (!matching)
since i only find this method on the internet so i just modify it
just curious if there is more simple method thats all :thinker:

oh what i mean by "bool" is the command
since i only find this method on the internet so i just modify it
just curious if there is more simple method thats all :thinker:
you could replace the duplicates with a number you are not intested in, e.g. INT_MAX
Code:
for (int i=0; i<t; i++)
{
int count=0;
if(x[i]!=INT_MAX)
for(int j=i+1;j<t;j++)
if (x[i] == x[j])
{
count++; x[j]=INT_MAX;
}
if(count>0) cout<<x[i]<<"=" << count << endl;
}
or when you find a duplicate you could delete it from the array

Ashieboy

Points: 2