U
utab
Dear all,
I would like to find the occurence of numbers in an array(I solved this
problem before now I could not see the solution) but not with iterators
or vectors I want to apply them later, I tried sth like this
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
int main(){
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
int count;
int p,f; //previous and following element in the array
int b[13];
for(int i=0;i!=11;i++){ // upto the 13-2=11
count = 0;
if(i==0){
for (int j=i;j!=12;j++){ // upto 13-1=12
if(a[j]==a)
++count;
}
cout << a << "\t" << count << endl;
}
else{
p=i; // preceeding
f=i+1; // following
if(a[f]!=a[p]){ // compare following and preceding elements
for (int k=f;k!=12;k++){
if(a[k]==a[f])
++count;
}
cout << a[f] << "\t" << count << endl;
}
}
}
return 0;
}
the output is
1 2
3 1
4 2
5 2
6 1
7 2
8 1
9 1
7 1
4 1
the problem is that lets say if 4 follows 4 then no problem but if 4 4
6 4 then problem I could not find the way to fix that(I only compare
the preceeding and following so if the same element appears and
different from the previous one then it again counts that element. SO I
KNOW MY PROBLEM but NO WAY).
thx.
I would like to find the occurence of numbers in an array(I solved this
problem before now I could not see the solution) but not with iterators
or vectors I want to apply them later, I tried sth like this
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::endl;
int main(){
int a[]={1,1,3,4,5,5,6,7,8,9,7,4,1}; // 13 elements
int count;
int p,f; //previous and following element in the array
int b[13];
for(int i=0;i!=11;i++){ // upto the 13-2=11
count = 0;
if(i==0){
for (int j=i;j!=12;j++){ // upto 13-1=12
if(a[j]==a)
++count;
}
cout << a << "\t" << count << endl;
}
else{
p=i; // preceeding
f=i+1; // following
if(a[f]!=a[p]){ // compare following and preceding elements
for (int k=f;k!=12;k++){
if(a[k]==a[f])
++count;
}
cout << a[f] << "\t" << count << endl;
}
}
}
return 0;
}
the output is
1 2
3 1
4 2
5 2
6 1
7 2
8 1
9 1
7 1
4 1
the problem is that lets say if 4 follows 4 then no problem but if 4 4
6 4 then problem I could not find the way to fix that(I only compare
the preceeding and following so if the same element appears and
different from the previous one then it again counts that element. SO I
KNOW MY PROBLEM but NO WAY).
thx.