diff --git a/C++/Program-2/Readme.md b/C++/Program-2/Readme.md new file mode 100644 index 00000000..dba37502 --- /dev/null +++ b/C++/Program-2/Readme.md @@ -0,0 +1,7 @@ +Program 2: + +Write a Program to find the number of occurrence of X element present in sorted array. +Variable description--> +n = size of array/ number of element +x = Element to be counted in array +count = veriable to count the X-number in array diff --git a/C++/Program-2/number_of_occurrence.cpp b/C++/Program-2/number_of_occurrence.cpp new file mode 100644 index 00000000..d481a7bc --- /dev/null +++ b/C++/Program-2/number_of_occurrence.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +class Solution{ +public: + +/* + if x is present in arr[] then returns the count + of occurrences of x, otherwise returns 0. +*/ + +int count(int arr[], int n, int x) { + + int count = 0; + for(int i=0;i> n ; + cout<<"Enter a number to be counted: "; + cin>>x; + int arr[n]; + for (int i = 0; i < n; i++) { + cin >> arr[i]; + } + Solution ob; + auto ans = ob.count(arr, n, x); + cout<< "number "< +n = number of elements/size +k = Kth element for swaping diff --git a/C++/Program-3/swap_kth_Element.cpp b/C++/Program-3/swap_kth_Element.cpp new file mode 100644 index 00000000..d0573268 --- /dev/null +++ b/C++/Program-3/swap_kth_Element.cpp @@ -0,0 +1,45 @@ +#include + +using namespace std ; + +void takeInput(int a[], int n){ + for(int i=1;i<=n;i++){ + cin>>a[i]; + } +} + +void swap_k(int a[], int n, int k){ + + int i , count = n ; + for(i=1 ; i<=n , count >= 1 ; i++ , count--){ + + if(k == i || k == count){ + swap(a[k],a[count]); + } + + } + + for(i=1;i<=n;i++){ + cout<>n ; + + int k; + cout<<"Enter Kth element to swap:"; + cin>>k ; + + int a[1000]; + takeInput(a,n); + + swap_k(a,n,k); + cout<<"\n"; + + return 0; +}