Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
rotate
//g++ 7.4.0 //write a function that rotates first n elements of array a , k positions to the right; the last k elements are rotated around to the beginning of array a; //this code is created by Rezaul Hoque on June 16,2021; contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> using namespace std; int n=8, k=2; void rotate(int [], int, int); void rotate(int a[], int n, int k){ //first we have to create three buffer arrays, each having the same size of a; then they are initialized with 0s; int b[n]={0,0,0,0,0,0,0,0}; int c[n]={0,0,0,0,0,0,0,0}; int d[n]={0,0,0,0,0,0,0,0}; for(int i=0;i<k;i++){//note loop continuing condition is smaller than k,number of elements that will be placed at the beginning b[i]=a[n-k+i];//array b holds the elements of array a,starting from index n-k+i;if i=0 & k=2, a[6]; when i=1,a[7];so b now looks like b[n]={16,17,0,0,0,0,0,0}; a[n-k+i]=0;//having copied the elements staring from index n-k+i, fill them with 0; so a[n]={10,11,12,13,14,15,0,0}; } for(int i=0;i<n-k;i++){ c[i+k]=a[i];//copy the elements of a to array c at the index starting from k; so c[n]={0,0,10,11,12,13,14,15}; } for(int i=0;i<n;i++){ d[i]=b[i]+c[i];//finally the resultant array d holds the sum of the elements of b and c;so d [n]={16,17,10,11,12,13,14,15}; } for(int i=0;i<n;i++){ cout<<" "<<d[i]; } } int main() { int a[n]={10,11,12,13,14,15,16,17}; rotate(a,8,2); return 0; }
run
|
edit
|
history
|
help
0
codeforces - 545D
Procesos E
Identifying polimorphic types without using RTTI or type mappings
Simulare 2022
Sort array of 0's and 1's
123
Programa 3(Creo que ya esta)
Days in month database using unordered_map
Cley
LIS