data structures - FIFO list (moving elements) [C++] -


Good evening, people!

I am trying to solve a simple problem, but .. well, it seems that I can not do it :)

The idea is that I have n elements Has a FIFO list (FIFO queue) and it has been given a value, Kashmir (K & lt; n). My small program has to move the elements with the edges of the left. (Like n = 4, k = 3, a [] = (1, 2, 3, 4), result 4 1 2 3).

But okay, I have not got anywhere near that.

This is what I have written so far:

  #include & lt; Iostream & gt; using namespace std; Zero step (one a [100], unsigned n, unsigned k) {int t [100]; Unsigned i; (I = 0; i & lt; = n-1; i ++) for t [i] = a [i]; For (i = 0; i & lt; = k-1; i ++) a [i] = a [i + k-1]; For (i = k; i & lt; = n-1; i ++) a [i] = t [i + 1]; } Int main () {int a [100]; Unsigned Kashmir, N, I; Cout & lt; & Lt; "N; k ="; CIN & gt; & Gt; N & gt; & Gt; K; (I = 0; i & lt; = n-1; i ++) cin & gt; & Gt; A [i]; Move (A, N, K); (I = 0; i & lt; = n-1; i ++) for COAT & LT; & Lt; A [i] & lt; & Lt; ""; }  

Any help would be greatly appreciated. Thank you in advance.

I'm not sure if I have fully understood your question. But it seems that you want to rotate the array contents effectively.

To rotate array contents in the left K bar, you can do the following:

  • Reverse the first elements.
  • Invert the remaining NK elements.
  • Reverse the whole array. Example:

    N = 5, K = 3, and array = [1 2 3 4 5]

  • Ul>
  • Step 1: Reverse the first 3 elements: [3 2 1 4 5]
  • Step 2: Reverse the remaining 2 elements: [3 2 1 5 4]
  • Step 3: Reverse the entire array: [4 5 1 2 3]

To do this for the C ++ function:

  Zero step (int a [100], int n, int k) {int t [100]; Int i, j; For (i = k-1, j = 0; i> = 0; i -, j ++) t [j] = a [i]; (I = N-1; I> = K; I -, J ++) T [J] = A [I] for; For (I = N-1, J = 0; I> = 0; I -, J ++) A [J] = T [I]; }  

One better way to do this in continuous space is to put the mutation in place:

  zero arr_rev (int a [100], int Start, end end) {int temp; For (; start & lt; end; start ++, end -) {temp = a [start]; A [start] = a [end]; A [end] = temporary; }} Zero step 2 (int a [100], int n, int a) {arr_rev (a, 0, k-1); Arr_rev (one, k, n-1); Arr_rev (a, 0, n-1); }  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -