c - Shifting elements in an array to the right by 1 -


i having trouble figuring out how shift elements in array right 1. array inialized [1 ,2, 3, 4, 5]. when shift, result should [2, 3, 4, 5, 1], comes out [2, 3, 4, 5, 0], , not sure why. here have far -

   for(k = 0; k <= n - 1; k++){    array[k] = array[k+1];    }     printf("array now:\n");    k = 0;    while(k < n) {        printf("x[%d] = %f\n", k, array[k]);        k++;    } 

the result prints vertically.

this basic code right shift 1 only. if want general code can work index right shift, suggest use variable.

//array elements index 0 n-1  int tempdata  = array[0]; // if right shift 1 for(k = 0; k < n-1; k++){    array[k] = array[k+1]; }  array[n-1] = tempdata; //reinstall value of first index last index printf("array now:\n"); k = 0; while(k < n) {     printf("x[%i] = %d\n", k, array[k]);     k++; } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -