From 3f074f0d3c4bfcddbfbe1d8ceabc3db02d00e5c5 Mon Sep 17 00:00:00 2001 From: sushil-sth <45484910+sushil-sth@users.noreply.github.com> Date: Thu, 24 Oct 2019 07:35:18 +0545 Subject: [PATCH 1/4] skip some of the array elements using C++ program We are using % (modulus operator) to check every 3rd iteration. If it is, the continue will skip everything down in loop's scope and continue executing the next iteration. For all other elements it will print them. --- SkipSomeArray | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 SkipSomeArray diff --git a/SkipSomeArray b/SkipSomeArray new file mode 100644 index 0000000..9f05800 --- /dev/null +++ b/SkipSomeArray @@ -0,0 +1,17 @@ +#include +using namespace std; + +int main() +{ + int arr[10] = {1,2,3,4,5,6,7,8,9,10}; + int i; + + for(int i=0; i<10; i++) + { + if((i+1)%3 == 0) //If index is every third element + continue; //Continue + cout< Date: Thu, 24 Oct 2019 07:38:48 +0545 Subject: [PATCH 2/4] Shortest Source to Destination Path In this article, we are going to see how to find the shortest path from source to destination in a 2D maze. Given a Boolean 2D matrix (0-based index), find whether there is a path from (0,0) to (x,y) and if there is one path, print the minimum no of steps needed to reach it, else print -1 if the destination is not reachable. Moves are possible in only four directions i.e. up, down, left and right. The path can only be created out of a cell if its value is 1. --- ShortestPath | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 ShortestPath diff --git a/ShortestPath b/ShortestPath new file mode 100644 index 0000000..1bf56ba --- /dev/null +++ b/ShortestPath @@ -0,0 +1,122 @@ +#include +using namespace std; + +//checking valid node +int isvalid(int nextx,int nexty,int m,int n){ + if(nextx>=0 && nextx=0 && nexty q; + + point curr; + //set the source point (0,0) + curr.mpoint(0,0); + + node t; + //set the source node at point (0,0) + t.mnode(curr,0); + + //ENQUEUE source node + q.push(t); + + //direction matrices + int arrx[]={-1,0,1,0}; + int arry[]={0,1,0,-1}; + + point c; + node v; + while(!q.empty()){ + //DEQUEUE + v=q.front(); + + c=v.p; + //if destnation node is reached + if(x1==c.row && y1==c.col ){ + return v.d; + } + q.pop(); + //check for all four neighbours + for(int i=0;i<4;i++){ + int nextx=c.row+arrx[i]; + int nexty=c.col+arry[i]; + //if valid node && not visited yet + if(isvalid(nextx,nexty,m,n) && a[nextx][nexty] && !visited[nextx][nexty]){ + curr.mpoint(nextx,nexty); + //set neighbour node by incrementing distance value + t.mnode(curr,(v.d)+1); + q.push(t); //EnQueue + //mark as visited + visited[nextx][nexty]=true; + } + } + } + return -1; +} + + +int main() +{ + int m,n,x,y; + + cout<<"enter matrix row & column"< Date: Thu, 24 Oct 2019 07:41:46 +0545 Subject: [PATCH 3/4] Change case of a character using bit manipulation in C++ Here, we will learn how to change case of a character using bit manipulation in C++? Program will demonstrate lowercase to uppercase and uppercase to lowercase conversion. --- ChangeCharacterCase | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ChangeCharacterCase diff --git a/ChangeCharacterCase b/ChangeCharacterCase new file mode 100644 index 0000000..addedc0 --- /dev/null +++ b/ChangeCharacterCase @@ -0,0 +1,28 @@ +#include +using namespace std; + + +char lowerToUpper(char c) +{ + return (c & '_'); +} + +char upperToLower(char c) +{ + // ASCII value of space (' ') is 32 + // so it is equivalent to (1 << 5 ) + return (c | ' '); +} + +int main() +{ + char c; + cout << "Enter Character ( in lower case ) : \n"; + cin >> c; + cout << "Output : " << lowerToUpper(c); + cout << "\n\nEnter Character ( in Upper case ) : \n"; + cin >> c; + cout << "Output : " << upperToLower(c); + + return 0; +} From c275ac25de4b569e63924d5da0fb47afa97d22eb Mon Sep 17 00:00:00 2001 From: sushil-sth <45484910+sushil-sth@users.noreply.github.com> Date: Thu, 24 Oct 2019 07:45:29 +0545 Subject: [PATCH 4/4] Relative sorting algorithm we are going to learn relative sorting along with its algorithm, C++ program. Problem Statement: Given two array A and B, sort A in such a way that the relative order among the elements will be the same as those in B. For the elements not present in B, append them at last in sorted order. It is also given that the number of elements in B is smaller than or equal to the number of elements in A and B has all distinct elements. --- RealtiveSorting | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 RealtiveSorting diff --git a/RealtiveSorting b/RealtiveSorting new file mode 100644 index 0000000..da9a1f2 --- /dev/null +++ b/RealtiveSorting @@ -0,0 +1,59 @@ +#include +using namespace std; + +vector sorted(vector a,vector b,int n1,int n2){ + vector c; + // array a is sorted using labrary sorting function + sort(a.begin(),a.end()); + + for(int i=0;i :: iterator p; //iterator p + + scanf("%d %d",&n1,&n2); + + vector a; //array a + vector b;//array b + + for(int j=0;j c=sorted(a,b,n1,n2); + for(p=c.begin();p!=c.end();p++) + printf("%d ",*p); // printing the sorted array + printf("\n"); + + return 0; +}