In an array of sometimes there are a lot of the same data or twins. The program this time is to calculate many of the same data or twins. Suppose there is no data as follows: 3 5 6 4 5 3 6 8 ..
The data in search: 3, 5 and 0.
The result is:
Data 3 = 2.
Data 5 = 5.
Data 0 = 0.
For more details, then note the source code for the following courses:
#include <cstdlib>
#include <iostream>
using namespace std;
class cek{
public:
cek();
void masuk();
void hitung();
private:
int x[10],y[10],z[10],n,m,p;
char a[10],b[10];
};
cek::cek(){
cout<<"PROGRAM MENGHITUNG BANYAK SUATU ANGKA/HURUF YANG DI CARI DALAM SUATU INPUTAN"<<endl;
}
void cek::masuk(){
cout<<"Pilih :"<<endl;
cout<<" 1. Angka "<<endl;
cout<<" 2. Huruf "<<endl;
cout<<"Masukkan Pilihan :";
cin>>p;
if (p==1){
cout<<"Masukkan Banyak digit Inputan :"; cin>>n;
for(int i=0; i<n; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>x[i];
}
cout<<"Inputan :";
for(int i=0; i<n; i++){
cout<<x[i]<<",";
}
cout<<endl;
cout<<"Masukkan Banyak digit Pencarian :"; cin>>m;
for(int i=0; i<m; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>y[i];
}
cout<<"Pencarian :";
for(int i=0; i<m; i++){
cout<<y[i]<<",";
}
cout<<endl;
}
else{
cout<<"Masukkan Banyak digit Inputan :"; cin>>n;
for(int i=0; i<n; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>a[i];
}
cout<<"Inputan :";
for(int i=0; i<n; i++){
cout<<a[i]<<",";
}
cout<<endl;
cout<<"Masukkan Banyak digit Pencarian :"; cin>>m;
for(int i=0; i<m; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>b[i];
}
cout<<"Pencarian :";
for(int i=0; i<m; i++){
cout<<b[i]<<",";
}
cout<<endl;
}
}
void cek::hitung(){
if(p==1){
for(int i=0; i<m; i++){
z[i]=0;
for(int j=0; j<n; j++){
if(y[i]==x[j]) z[i]=z[i]+1;}}
for(int i=0; i<m; i++){
cout<<"Banyak"<<y[i]<<"="<<z[i]<<",";
cout<<endl;}
}
else{
for(int i=0; i<m; i++){
z[i]=0;
for(int j=0; j<n; j++){
if(b[i]==a[j]) z[i]=z[i]+1;}}
for(int i=0; i<m; i++){
cout<<"Banyak"<<b[i]<<"="<<z[i]<<",";
cout<<endl;}
}
}
int main(int argc, char *argv[])
{
cek test;
test.masuk();
test.hitung();
system("PAUSE");
return EXIT_SUCCESS;
}
Running Result :
The data in search: 3, 5 and 0.
The result is:
Data 3 = 2.
Data 5 = 5.
Data 0 = 0.
For more details, then note the source code for the following courses:
#include <cstdlib>
#include <iostream>
using namespace std;
class cek{
public:
cek();
void masuk();
void hitung();
private:
int x[10],y[10],z[10],n,m,p;
char a[10],b[10];
};
cek::cek(){
cout<<"PROGRAM MENGHITUNG BANYAK SUATU ANGKA/HURUF YANG DI CARI DALAM SUATU INPUTAN"<<endl;
}
void cek::masuk(){
cout<<"Pilih :"<<endl;
cout<<" 1. Angka "<<endl;
cout<<" 2. Huruf "<<endl;
cout<<"Masukkan Pilihan :";
cin>>p;
if (p==1){
cout<<"Masukkan Banyak digit Inputan :"; cin>>n;
for(int i=0; i<n; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>x[i];
}
cout<<"Inputan :";
for(int i=0; i<n; i++){
cout<<x[i]<<",";
}
cout<<endl;
cout<<"Masukkan Banyak digit Pencarian :"; cin>>m;
for(int i=0; i<m; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>y[i];
}
cout<<"Pencarian :";
for(int i=0; i<m; i++){
cout<<y[i]<<",";
}
cout<<endl;
}
else{
cout<<"Masukkan Banyak digit Inputan :"; cin>>n;
for(int i=0; i<n; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>a[i];
}
cout<<"Inputan :";
for(int i=0; i<n; i++){
cout<<a[i]<<",";
}
cout<<endl;
cout<<"Masukkan Banyak digit Pencarian :"; cin>>m;
for(int i=0; i<m; i++){
cout<<"Data ke-"<<i+1<<":"; cin>>b[i];
}
cout<<"Pencarian :";
for(int i=0; i<m; i++){
cout<<b[i]<<",";
}
cout<<endl;
}
}
void cek::hitung(){
if(p==1){
for(int i=0; i<m; i++){
z[i]=0;
for(int j=0; j<n; j++){
if(y[i]==x[j]) z[i]=z[i]+1;}}
for(int i=0; i<m; i++){
cout<<"Banyak"<<y[i]<<"="<<z[i]<<",";
cout<<endl;}
}
else{
for(int i=0; i<m; i++){
z[i]=0;
for(int j=0; j<n; j++){
if(b[i]==a[j]) z[i]=z[i]+1;}}
for(int i=0; i<m; i++){
cout<<"Banyak"<<b[i]<<"="<<z[i]<<",";
cout<<endl;}
}
}
int main(int argc, char *argv[])
{
cek test;
test.masuk();
test.hitung();
system("PAUSE");
return EXIT_SUCCESS;
}
Running Result :