Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions C++/Program-2/Readme.md
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions C++/Program-2/number_of_occurrence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<bits/stdc++.h>

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;i++){
if(arr[i] == x){
count++;
}
}
return count;
}
};



int main() {

int n, x;
cout<<"Enter the size of an Array: ";
cin >> 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 "<<x<<" counted in array for " << ans <<" times."<< "\n";
return 0;
}
4 changes: 4 additions & 0 deletions C++/Program-3/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Program to Swap the Kth Elements of a series.
variable description -->
n = number of elements/size
k = Kth element for swaping
45 changes: 45 additions & 0 deletions C++/Program-3/swap_kth_Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<iostream>

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<<a[i]<<" " ;
}
}

int main()
{

int n;
cout<<"Enter size of an Array:";
cin>>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;
}