Kamis, 31 Juli 2025

C++ SAMPLE CODE 16d

 #include<iostream>

#include<algorithm>

using namespace std;


int main()

{

   int S[12] = {10, 24, 12, 22, 34, 51, 171, 13, 16, 176, 56, 42};


   partial_sort(S, S+6, S+12); // mengurutkan sampai s+6


   for ( int i =0; i<12; i++)

      cout << S[i] <<" ";

   cout<<"\n";


   return 0; 

}

=====================

#include<iostream>

# include<algorithm>

using namespace std;


int main()

   int S[12] = {10, 24, 12, 22, 34, 51, 171, 115, 20, 176, 56, 42 };

   int A [6];


   partial_sort_copy (S, S+12, A, A+6);


   cout<<"S = ";

   for ( int i =0; i<12; i++)

      cout << S[i] <<" ";


   cout<<"\nA = ";

   for (int k =0; k<6; k++)

      cout << A[k] <<" ";


   cout<<"\n";

   return 0 ;

}

=======================

#include<iostream>

#include<numeric>

using namespace std;


int main()

{

   int S[8] = {2, 3, 4, 5, 6, 7, 8, 9};

   int A[8];


   cout<< "S = ";

   for (int i = 0; i<8; i++)

      cout << S[i] <<" ";


   partial_sum(S, S+8, A);


   cout<<"\nA = ";

   for (int k =0; k<8; k++)

      cout << A[k] <<" ";


   cout<<"\n";

   return 0 ;

}

==============================

#include<iostream>

#include<algorithm>

using namespace std;


bool Genap(int m)  //definisi dari fungsi untuk angka genap

{

   return !(m%2) ? true : false;

}


int main()

{

   int S[] = {5, 6, 8, 7, 4, 3, 8, 10, 11, 12};


   partition(S, S+10, Genap);


   //mempartisi dan menempatkan angka-angka genap di depan.

   for ( int i =0; i<10 ; i++)

      cout<< S[i]<<" ";


   return 0;

}

==========================

#include <iostream>

#include<algorithm>

using namespace std;


void main()

{

   char A[4] = "XYZ";


   for( int k =0; k<6;k++)

   { 

      next_permutation (A, A+3);

      cout<<A<<" ";

   }

   cout<<"\n";


   for( int i =0; i<6;i++)

   {

      prev_permutation (A, A+3);

      cout<<A<<" ";

   }

   cout<<"\n";

}

====================

#include <iostream>

#include<algorithm>

using namespace std;


void main()

{   

   char A[7] = "ABCDEF";


   for(int k = 0; k<5; k++)

   {

      random_shuffle(A, A+6);

      cout<<A<<" ";

   }

   cout<<"\n";

}

======================

#include<iostream>

#include<algorithm>

#include<functional>

#include <vector>

using namespace std;


vector <int> V;


int main()

{

   int S[] = {5, 6, 8, 7, 8, 3, 8, 10, 8, 12};


   for (int i = 0; i<10; i++)

      V.push_back (S[i]); //menciptakan sebuah vektor V dengan elemen-elemen dari S


   cout<<"V = ";

   vector<int>::iterator iter; //deklarasi iterator

   for (iter= V.begin(); iter <V.end(); iter++)

      cout<< *iter<<" ";  //Menampilkan elemen-elemen dari V

   cout<<endl;


   int Hitung = count(V.begin(), V.end(), 8); //Menghitung jumlah elemen dengan nilai 8

   cout<<"Jumlah elemen dengan nilai 8 = "<<Hitung<<endl;


   remove (V.begin(), V.end() , 8); //Menghapus elemen-elemen dengan nilai 8

   for ( int j = 0; j<Hitung; j++)

      V.pop_back( ) ;

   

   cout<<"Sekarang V = ";

   for (iter= V.begin(); iter <V.end(); iter++)

      cout<< *iter<<" "; //Menampilkan elemen-elemen setelah penghapusan

   cout<<endl;


   Hitung = count_if(V.begin(), V.end(), bind2nd(less<int>(),6));

   cout<<"Jumlah elemen yang bernilai kurang dari 6 = "<<Hitung;


   remove_if(V.begin(), V.end(), bind2nd(less<int>(),6));

   //Menghapus elemen-elemen yang bernilai kurang dari 6

   for (int k = 0; k<Hitung; k++)

      V.pop_back( ) ;


   cout<<"\nVektor baru setelah dua penghapusan:"<<endl;

   cout<<"V = ";

   for (iter= V.begin(); iter < V.end(); iter++)

      cout<<*iter<<" ";


   cout<<endl;

   return 0;

}

=======================

#include<iostream>

#include<algorithm>

#include<functional>

#include <vector>

using namespace std;


vector <int> V;


int main()

{

   int S[] = {5, 6, 8, 7, 8, 3, 8, 10, 8, 12};


   for (int i =0 ; i<10;i++)

      V.push_back (S[i]);


   vector<int>:: iterator iter;

   for (iter= V.begin(); iter <V.end(); iter++)

      cout<< *iter<<" ";

   cout<<endl;


   replace (V.begin(), V.end(), 8, 9); // mengganti 8 dengan 9


   for (iter= V.begin(); iter <V.end(); iter++)

      cout<< *iter<<" ";

   cout<<endl;


   replace_if(V.begin(), V.end(), bind2nd(less<int>(),9), 5);

   //mengganti elemen-elemen yang kurang dari 9 dengan 5

   

   cout<<"Vektor baru setelah dua kali penggantian:"<<endl;

   for (iter= V.begin(); iter <V.end(); iter++)

      cout<< *iter<<" ";

   cout<<endl;

   

   return 0;

}

=====================

#include <iostream>

#include<algorithm>

using namespace std;


void main()

{

   char A[] = "ABCDEFGH";


   int n = strlen(A);

   reverse(A, A+n);

   

   cout<<A<<endl;

}

==========================

#include<iostream>

#include<algorithm>

using namespace std;


void main()

{

   int S1[] = {11, 12, 13, 15, 16, 17, 18, 19 ,20};


   cout<<"S1= ";

   rotate ( S1, S1+4,S1+9);

   for (int j =0; j<9; j++)

      cout << S1[j]<<" ";

}

=======================

#include<iostream>

#include<algorithm>

using namespace std;


int main()

{

   char Set1[] = {"ACDGHIJKTUVZ"};

   char Set2[] = {"BCDKMNPUV"};

   char Setx[12];

   char Setxy[15];

   char SetA[20];

   char SetU[25];


   cout<<"Set1 = "<<Set1<<endl;

   cout<<"Set2 = "<<Set2<<endl;


   char* Sety = set_difference(Set1, Set1+12, Set2, Set2+9, Setx);

   *Sety =0;

   cout<<"set_difference = "<<Setx<<endl;


   char* Setz = set_intersection(Set1, Set1+12, Set2, Set2+9,Setxy);

   *Setz =0;

   cout <<"set_intersection = "<<Setxy<<endl;


   char* Setm = set_symmetric_difference(Set1, Set1+12, Set2,Set2+9, SetA);

   *Setm = 0;

   cout <<"Set_symmetric_difference = "<<SetA<<endl;


   char* Setn = set_union(Set1, Set1+12, Set2, Set2+9, SetU);

   *Setn =0;

   cout <<"Union dari Set1 dan Set2 = "<<SetU<<endl;


   return 0 ;

}

========================

#include<iostream>

# include<algorithm>

using namespace std;


int main()

{

   int S1[] = {11, 12, 19, 13, 16, 20, 24, 22};


   sort(S1, S1+8);


   for (int i =0; i<8; i++)

      cout <<S1[i]<<" ";

   cout<<"\n";


   return 0 ;

}

=======================

#include<iostream>

#include<algorithm>

using namespace std;


void main()

{

   int S1[] = {11, 12, 19, 13, 16, 20, 24, 22};

   int S2[] = {1, 2, 3, 4, 5, 6, 7, 8};


   cout<<"S1= ";

   swap_ranges(S1, S1+4, S2+4);

   for ( int j =0; j<8; j++)

      cout<<S1[j]<<" ";


   cout<<"\nS2= ";

   for ( int k =0; k<8; k++)

      cout<<S2[k]<<" ";

   cout<<endl;

}

======================

#include<iostream>

#include<list>

using namespace std;


list<char> L1;


void main()

   for (int i=0; i<6;i++)

   {

      L1.push_back(65 + i);

      L1.push_back(65 + i);

   }


   cout<<"List awal adalah berikut:"<<endl;

   list<char>::iterator T1;

   for ( T1 = L1.begin() ; T1!=L1.end() ; T1++ )

      cout <<" "<<*T1 ;

   cout<<endl;


   L1.unique();

   cout<<"Setelah penghapusan duplikat, list menjadi: "<<endl;

   for (T1 = L1.begin() ; T1!=L1.end() ; T1++ )

      cout <<" "<<*T1 ;


   L1.reverse();

   cout<<"\nSetelah pembalikan list: \n";

   for ( T1 = L1.begin() ; T1!=L1.end() ; T1++ )

      cout <<" "<<*T1 ;


   cout<<"\nSetelah penghapusan F dari list: \n";

   L1.remove ('F');

   for ( T1 = L1.begin() ; T1!=L1.end() ; T1++ )

      cout <<" "<<*T1 ;

   cout << endl;

}

======================

#include<iostream>

# include<algorithm>

using namespace std;


int main()

{

   int S1[] = {11, 121, 19, 122, 260, 203, 240, 22};

   int* ptr = upper_bound (S1, S1+8, 122);


   cout<<"Batas atas adalah "<<*ptr<< endl;

   cout<<"Indeksnya adalah "<< ptr - S1 <<" di dalam array."<<endl;


   return 0 ;

}

Tidak ada komentar: